THE YENEPOYA INSTITUTE OF ARTS,
SCIENCE, COMMERCE AND MANAGEMENT
A Constituent Unit of YENEPOYA (Deemed to be University)
DEPARTMENT OF COMPUTER SCIENCE
Student Name :_____________________________________________________________
Reg.No :_____________________________________________________________
Campus Id :_____________________________________________________________
Specialization :_____________________________________________________________
Subject : _____________________________________________________________
Year : _____________________________________________________________
Semester : _____________________________________________________________
THE YENEPOYA INSTITUTE OF ARTS,
SCIENCE, COMMERCE AND MANAGEMENT
A Constituent Unit of YENEPOYA (Deemed to be University)
DEPARTMENT OF COMPUTER SCIENCE
Student Name :_____________________________________________________________
Reg.No :_____________________________________________________________
Campus Id :_____________________________________________________________
Specialization :_____________________________________________________________
Subject : _____________________________________________________________
Year : _____________________________________________________________
Semester : _____________________________________________________________
THE YENEPOYA INSTITUTE OF ARTS,
SCIENCE, COMMERCE AND MANAGEMENT
A Constituent Unit of YENEPYA (Deemed to be University)
This is to certify that Mr/Ms____________________________________________,
a student of BCA, First Year Second Semester with
___________________________________________________ specialization bearing
Reg.No______________________________________ has satisfactorily completed
the Practical Record in Object Oriented Programming with Java Lab in
partial fulfilment of the requirements for the BCA Programme of the
Yenepoya ( Deemed to be University).
________________________________ ______________________________
Faculty In- Charge HOD
Submitted for the practical examination held on _____________________ by
the Department of Computer Science.
________________________________ ______________________________
External Examiner Internal Examiner
INDEX
S.No Date List of Programs Page No Signature
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
1. Java program to find the nCr and nPr
I BCA-Object Oriented Programming with Java Lab Page 1
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
1. Java program to find the nCr and nPr
import java.util.Scanner;
public class A1
{
public static int fact(int num)
{
int fact=1;
for(int i=1; i<=num; i++)
fact = fact*i;
return fact;
}
public static void main(String[] args)
{
int n, r, ncr, npr;
Scanner s = new Scanner(System.in);
System.out.print("Enter the Value of n: ");
n = s.nextInt();
System.out.print("Enter the Value of r: ");
r = s.nextInt();
if (n<r){
System.out.println("n should be greater than or equal to r");
}
else {
npr = (fact(n)) / (fact(n-r));
ncr = npr/fact(r);
System.out.println("\nnCr = " +ncr);
System.out.println("nPr = " +npr);
}
}
}
Output
I BCA-Object Oriented Programming with Java Lab Page 2
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
2. Java program to arrange the strings in alphabetical order
I BCA-Object Oriented Programming with Java Lab Page 3
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
2. Java program to arrange the strings in alphabetical order
import java.io.*;
class AlphabeticalOrder{
public static void main(String[] args)
{
// storing input in variable
int n = 4;
// create string array called names
String names[]
= { "Cpp", "Java", "Python", "C" };
String temp;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// to compare one string with other strings
if (names[i].compareTo(names[j]) > 0) {
// swapping
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
// print output array
System.out.println(
"The names in alphabetical order are: ");
for (int i = 0; i < n; i++) {
System.out.println(names[i]);
}
}
}
Output
I BCA-Object Oriented Programming with Java Lab Page 4
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
3. Program to print pyramid pattern
I BCA-Object Oriented Programming with Java Lab Page 5
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
3. Program to print pyramid pattern
public class PyramidPattern
{
public static void main(String args[])
{
//i for rows and j for columns
//row denotes the number of rows you want to print
int i, j, row = 5;
//Outer loop work for rows
for (i=0; i<row; i++)
{
//inner loop work for space
for (j=row-i; j>1; j--)
{
//prints space between two stars
System.out.print(" ");
}
//inner loop for columns
for (j=0; j<=i; j++ )
{
//prints star
System.out.print("* ");
}
//throws the cursor in a new line after printing each line
System.out.println();
}
}
}
Output
I BCA-Object Oriented Programming with Java Lab Page 6
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
4. Program to multiply matrices
I BCA-Object Oriented Programming with Java Lab Page 7
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
4. Program to multiply matrices
public class MatrixMultiplication{
public static void main(String args[])
{
int a[][]={{1,2,-3},{4,5,0},{-7,2,9}};
int b[][]={{9,1,7},{6,-5,7},{-3,5,11}};
int c[][]=new int[3][3];
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
Output
I BCA-Object Oriented Programming with Java Lab Page 8
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
5. Binary search demonstration
I BCA-Object Oriented Programming with Java Lab Page 9
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
5. Binary search demonstration
import java.util.Arrays;
import java.util.Scanner;
public class BinarySearchDemo {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size = s.nextInt();
int[] array = new int[size];
System.out.println("Enter the sorted array elements:");
for (int i = 0; i < size; i++) {
array[i] = s.nextInt();
}
System.out.println("Enter the number to search:");
int key = s.nextInt();
int result = Arrays.binarySearch(array, key);
if (result < 0) {
System.out.println("Element is not found!");
} else {
System.out.println("Element is found at index: " + result);
}
}
}
Output
I BCA-Object Oriented Programming with Java Lab Page 10
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
6. Bubble Sort Program
I BCA-Object Oriented Programming with Java Lab Page 11
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
6. Bubble sort Program
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
Scanner S= new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = S.nextInt();
int[] array = new int[size];
System.out.println("Enter " + size + " elements:");
for (int i = 0; i < size; i++) {
array[i] = S.nextInt();
}
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
System.out.println("Sorted Array:");
for (int element : array) {
System.out.print(element + " ");
}
S.close();
}
}
Output
I BCA-Object Oriented Programming with Java Lab Page 12
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
7. Java program to create a class representing a Circle with attributes radius
and methods to calculate area and circumference
I BCA-Object Oriented Programming with Java Lab Page 13
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
7. Java program to create a class representing a Circle with attributes radius
and methods to calculate area and circumference
import java.util.Scanner;
class Circle {
double radius;
public Circle(double radius) {
this.radius = radius;
}
public double Area() {
return Math.PI * Math.pow(radius, 2);
}
public double Circumference() {
return 2 * Math.PI * radius;
}
}
public class CircleEx {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter the radius of the circle: ");
double userRadius = s.nextDouble();
Circle myCircle = new Circle(userRadius);
System.out.println("\nCircle with radius "
+String.format("%.2f",myCircle.radius));
System.out.println("Area: " +String.format("%.3f",myCircle.Area()));
System.out.println("Circumference: " +String.format("%.3f"
,myCircle.Circumference()));
s.close();
}
}
Output
I BCA-Object Oriented Programming with Java Lab Page 14
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
8. Type conversions
I BCA-Object Oriented Programming with Java Lab Page 15
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
8. Type conversions
public class TypeCast {
public static void main(String[] args) {
// Explicit casting from double to int
double doubleValue = 15.75;
int intValue = (int) doubleValue;
System.out.println("1. Explicit casting of "+doubleValue+" from double to int: " +
intValue);
// Automatic type promotion in expressions
int num1 = 5;
double num2 = 7.25;
double result = num1 + num2;
System.out.println("2. Automatic type promotion of "+num1+" + " +num2+" in
expressions: " + result);
// Converting int to char
int asciiValue = 65;
char charValue = (char) asciiValue;
System.out.println("3. Converting int "+asciiValue+" to char: " + charValue);
// Converting char to int
char anotherCharValue = 'B';
int anotherAsciiValue = (int) anotherCharValue;
System.out.println("4. Converting char "+anotherCharValue+" to int: " +
anotherAsciiValue);
// Converting int to long
int originalIntValue = 123;
long longValue = originalIntValue;
System.out.println("5. Converting int "+originalIntValue+" to long: " + longValue);
// Converting long to float
long originalLongValue = 9876543210L;
float floatValue = (float) originalLongValue;
System.out.println("6. Converting long "+originalLongValue+" to float: " +
floatValue);
// Converting float to double
float originalFloatValue = 3.14f;
double doubleFromFloat = originalFloatValue;
System.out.println("7. Converting float "+originalFloatValue+" to double: " +
doubleFromFloat);
// Implicit casting from int to double
int intValueForImplicit = 42;
double doubleValueForImplicit = intValueForImplicit;
System.out.println("8. Implicit casting from int "+intValueForImplicit+" to double:
" + doubleValueForImplicit);
// Converting double to String
double valueForStringConversion = 123.456;
I BCA-Object Oriented Programming with Java Lab Page 16
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
String stringValue = String.valueOf(valueForStringConversion);
System.out.println("9. Converting double "+valueForStringConversion+" to String:
" + stringValue);
}
}
Output
I BCA-Object Oriented Programming with Java Lab Page 17
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
9. Java program to demonstrate parameterized constructor.
I BCA-Object Oriented Programming with Java Lab Page 18
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
9. Java program to demonstrate parameterized constructor
class Perimeter
{
int length;
int breadth;
Perimeter(int l,int b)
{
length=l;
breadth=b;
}
void calculatePerimeter()
{
int per;
per=2*(length+breadth);
System.out.println("Perimeter: "+per);
}
void calculateArea()
{
int area;
area=length*breadth;
System.out.println("Area: "+area);
}
}
public class ParaConstructor
{
public static void main(String[] args)
{
Perimeter p1=new Perimeter(6,8);
Perimeter p2=new Perimeter(20,10);
Perimeter p3=new Perimeter(10,5);
System.out.println("Details of rectangle 01: ");
System.out.println("Length: " + p1.length);
System.out.println("Breadth: " + p1.breadth);
p1.calculatePerimeter();
p1.calculateArea();
System.out.println();
System.out.println("Details of rectangle 02: ");
System.out.println("Length: " + p2.length);
System.out.println("Breadth: " + p2.breadth);
p2.calculatePerimeter();
p2.calculateArea();
System.out.println();
System.out.println("Details of rectangle 03: ");
System.out.println("Length: " + p3.length);
I BCA-Object Oriented Programming with Java Lab Page 19
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
System.out.println("Breadth: " + p3.breadth);
p3.calculatePerimeter();
p3.calculateArea();
}
}
Output
I BCA-Object Oriented Programming with Java Lab Page 20
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
10. Java program to implement the various exceptions and handling
exceptions
I BCA-Object Oriented Programming with Java Lab Page 21
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
10. Java program to implement the various exceptions and handling
exceptions
import java.util.*;
public class ExceptionExample {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
try {
System.out.println("Enter two numbers to be divided:");
int num1 = s.nextInt();
int num2 = s.nextInt();
if (num2 == 0) {
throw new IllegalArgumentException("Division by zero is not allowed");
}
int result = num1 / num2;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception occurred: " + e.getMessage());
} catch (InputMismatchException e) {
System.out.println("InputMismatchException occurred: " + e.getMessage());
} catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException occurred: " + e.getMessage());
}
try {
System.out.println("Enter the size of the array:");
int size = s.nextInt();
if (size <= 0) {
throw new IllegalArgumentException("Array size must be a positive integer");
}
int[] array = new int[size];
System.out.println("Enter " + size + " elements:");
for (int i = 0; i < size; i++) {
array[i] = s.nextInt();
}
System.out.println("Enter an index to get the element from the array:");
int index = s.nextInt();
if (index < 0 || index >= size) {
throw new ArrayIndexOutOfBoundsException("Index is out of bounds");
}
System.out.println("Array element at index " + index + ": " + array[index]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException occurred: " +
e.getMessage());
} catch (InputMismatchException e) {
System.out.println("InputMismatchException occurred: " + e.getMessage());
} catch (IllegalArgumentException e) {
I BCA-Object Oriented Programming with Java Lab Page 22
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
System.out.println("IllegalArgumentException occurred: " + e.getMessage());
} finally{
System.out.println();
System.out.println("Exception handling methods like try.. catch, throw and finally
are demonstrated.");
}
s.close();
}
}
Output
I BCA-Object Oriented Programming with Java Lab Page 23
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
11. Java program to implement a simple calculator
I BCA-Object Oriented Programming with Java Lab Page 24
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
11. Java program to implement a simple calculator
import java.util.*;
public class Calculator {
public static void main(String[] args) {
float[] operands;
float res = 0;
int choice, op;
Scanner s = new Scanner(System.in);
while (true) {
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.println("5. Exit");
System.out.print("Enter Your Choice (1-5): ");
choice = s.nextInt();
switch (choice) {
case 1:
case 2:
case 3:
System.out.print("Enter the number of operands: ");
op = s.nextInt();
operands = new float[op];
System.out.print("\nEnter " + op + " Numbers: ");
for (int i = 0; i < op; i++) {
operands[i] = s.nextFloat();
}
res = calculate(choice, operands);
break;
case 4:
operands = new float[2]; // Division always uses 2 operands
System.out.print("Enter the first operand: ");
operands[0] = s.nextFloat();
System.out.print("Enter the second operand: ");
operands[1] = s.nextFloat();
res = calculate(choice, operands);
break;
case 5:
return;
default:
System.out.println("\nInvalid choice!");
break;
}
System.out.println("\nResult = " + res + "\n");
I BCA-Object Oriented Programming with Java Lab Page 25
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
}
}
public static float calculate(int choice, float[] operands) {
float result = 0; // Initialize result with 0
switch (choice) {
case 1: // Addition
for (int i = 0; i < operands.length; i++) {
result += operands[i];
}
break;
case 2: // Subtraction
result = operands[0]; // Initialize result with the first operand
for (int i = 1; i < operands.length; i++) {
result -= operands[i];
}
break;
case 3: // Multiplication
result = 1; // Initialize result with 1 for multiplication
for (float operand : operands) {
result *= operand;
}
break;
case 4: // Division
if (operands[1] != 0) {
result = operands[0] / operands[1];
} else {
System.out.println("Error: Division by zero!");
return 0;
}
break;
}
return result;
}
}
Output
I BCA-Object Oriented Programming with Java Lab Page 26
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
I BCA-Object Oriented Programming with Java Lab Page 27
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
12. Java program to implement single, hierarchical and multilevel
inheritance
I BCA-Object Oriented Programming with Java Lab Page 28
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
12. Java program to implement single, hierarchical and multilevel
inheritance
// Parent class
class Person {
String name;
void setName(String name) {
this.name = name;
}
void display() {
System.out.println("Name: " + name);
}
}
class Student extends Person { // Single Inheritance
int id;
void setid(int id) {
this.id = id;
}
void displayStudent() {
System.out.println("Student Class");
display(); // Inherited from Person
System.out.println("Student ID: " + id);
System.out.println("Single Inheritance. Student class is a child of Person class");
}
}
class Faculty extends Person { // Hierarchical Inheritance
String sub;
void setsub(String sub) {
this.sub = sub;
}
void displayFaculty() {
System.out.println("Faculty Class");
display(); // Inherited from Person
System.out.println("Subject: " + sub);
System.out.println("Hierarchical Inheritance. Faculty class is also a child of
Person class");
}
}
class Hod extends Faculty { // Multilevel Inheritance
String dept;
void setdept(String dept) {
this.dept = dept;
}
void displayHod() {
System.out.println("Hod Class");
I BCA-Object Oriented Programming with Java Lab Page 29
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
displayFaculty();
System.out.println("Department: " + dept);
System.out.println("Multilevel Inheritance. Hod class is a child of Faculty class,
which is a child of Person class");
}
}
public class Inheritance {
public static void main(String[] args) {
// Single Inheritance
Student stu = new Student();
stu.setName("Thor");
stu.setid(12345);
stu.displayStudent();
System.out.println();
// Hierarchical Inheritance
Faculty fac = new Faculty();
fac.setName("Natasha");
fac.setsub("OOPs with Java");
fac.displayFaculty();
System.out.println();
// Multilevel Inheritance
Hod h = new Hod();
h.setName("Loki");
h.setsub("Computer Science");
h.setdept("CS Department");
h.displayHod();
}
}
Output
I BCA-Object Oriented Programming with Java Lab Page 30
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
I BCA-Object Oriented Programming with Java Lab Page 31
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
13. Java program for method overloading and overriding
I BCA-Object Oriented Programming with Java Lab Page 32
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
13. Java program for method overloading and overriding
//Method Overloading
import java.io.*;
class MethodOverloadingEx
{
static int add(int a, int b)
{
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
public static void main(String args[])
{
System.out.print(" a+b = ");
System.out.println(add(4, 6));
System.out.print(" a+b+c = ");
System.out.println(add(4, 6, 7));
}
}
Output
I BCA-Object Oriented Programming with Java Lab Page 33
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
//Method Overriding
class Vehicle{
//defining a method
void run(){System.out.println("Vehicle is running");}
}
//Creating a child class
class Bike2 extends Vehicle{
//defining the same method as in the parent class
void run(){System.out.println("Bike is running safely");}
public static void main(String args[]){
Bike2 obj = new Bike2();//creating object
obj.run();//calling method
}
}
Output
I BCA-Object Oriented Programming with Java Lab Page 34
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
14. Write a java program to implement multithreading
I BCA-Object Oriented Programming with Java Lab Page 35
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
14. Write a java program to implement multithreading
class Hello extends Thread {
public void run() {
for(int i=1;i<=5;i++) {
System.out.println("Hello");
}
}
}
class Hi extends Thread{
public void run() {
for(int i=1;i<=5;i++) {
System.out.println("Hi");
}
}
}
public class Main {
public static void main(String[] args) {
Hello t1 = new Hello();
Hi t2 = new Hi();
t1.start();
t2.start();
}
}
Output
I BCA-Object Oriented Programming with Java Lab Page 36
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
15. Servlet Program
I BCA-Object Oriented Programming with Java Lab Page 37
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
DemoServlet.java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class DemoServlet extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
res.setContentType("text/html");//setting the content type
PrintWriter pw=res.getWriter();//get the stream to write the data
//writing html in the stream
pw.println("<html><body>");
pw.println("Welcome to servlet");
pw.println("</body></html>");
pw.close();//closing the stream
} }
web.xml file
<web-app>
<servlet>
<servlet-name>sonoojaiswal</servlet-name>
<servlet-class>DemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sonoojaiswal</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
I BCA-Object Oriented Programming with Java Lab Page 38
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
Output
I BCA-Object Oriented Programming with Java Lab Page 39
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
16. JDBC
I BCA-Object Oriented Programming with Java Lab Page 40
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
16. JDBC
import java.sql.*;
public class FirstExample
static final String DB_URL = "jdbc:mysql://localhost/TUTORIALSPOINT";
static final String USER = "guest";
static final String PASS = "guest123";
static final String QUERY = "SELECT id, first, last, age FROM Employees";
public static void main(String[] args) {
// Open a connection
try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(QUERY);) {
// Extract data from result set
while (rs.next()) {
// Retrieve by column name
System.out.print("ID: " + rs.getInt("id"));
System.out.print(", Age: " + rs.getInt("age"));
System.out.print(", First: " + rs.getString("first"));
System.out.println(", Last: " + rs.getString("last"));
} catch (SQLException e) {
e.printStackTrace();
I BCA-Object Oriented Programming with Java Lab Page 41
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
Output
Connecting to database...
Creating statement...
ID: 100, Age: 18, First: Zara, Last: Ali
ID: 101, Age: 25, First: Mahnaz, Last: Fatma
ID: 102, Age: 30, First: Zaid, Last: Khan
ID: 103, Age: 28, First: Sumit, Last: Mittal
I BCA-Object Oriented Programming with Java Lab Page 42
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
17. Java Swings
I BCA-Object Oriented Programming with Java Lab Page 43
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
17. Java Swings
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Login extends JFrame implements ActionListener
JButton SUBMIT;
JPanel panel;
JLabel label1,label2;
final JTextField text1,text2;
Login()
label1 = new JLabel();
label1.setText("Username:");
text1 = new JTextField(15);
label2 = new JLabel();
label2.setText("Password:");
text2 = new JPasswordField(15);
SUBMIT=new JButton("SUBMIT");
panel=new JPanel(new GridLayout(3,1));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
I BCA-Object Oriented Programming with Java Lab Page 44
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
panel.add(SUBMIT);
add(panel,BorderLayout.CENTER);
SUBMIT.addActionListener(this);
setTitle("LOGIN FORM");
public void actionPerformed(ActionEvent ae)
String value1=text1.getText();
String value2=text2.getText();
if (value1.equals("roseindia") && value2.equals("roseindia")) {
NextPage page=new NextPage();
page.setVisible(true);
JLabel label = new JLabel("Welcome:"+value1);
page.getContentPane().add(label);
else{
System.out.println("enter the valid username and password");
JOptionPane.showMessageDialog(this,"Incorrect login or password",
"Error",JOptionPane.ERROR_MESSAGE);
class LoginDemo
public static void main(String arg[])
try
I BCA-Object Oriented Programming with Java Lab Page 45
The Yenepoya Institute of Arts, Science, Commerce and Management
(YIASCM), Yenepoya (Deemed be University), Bangalore
Login frame=new Login();
frame.setSize(300,100);
frame.setVisible(true);
catch(Exception e)
{JOptionPane.showMessageDialog(null, e.getMessage());}
Output
I BCA-Object Oriented Programming with Java Lab Page 46