0% found this document useful (0 votes)
17 views11 pages

Advanced Java Programming Lab File

Uploaded by

Yash Raj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views11 pages

Advanced Java Programming Lab File

Uploaded by

Yash Raj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SCHOOL OF COMPUTER APPLICATION &

TECHNOLOGY (SCAT)
LAB FILE
SUBJECT: ADVANCED JAVA PROGRAMMING
SUBJECT CODE: E1PY306B
SUBMITED TO: DR AKHILENDRA KHARE
SUBMITED BY: DIVYANSHU MISHRA
ADMISSION NO: 24SCSE2240059
TABLE OF CONTENTS
No Practical Signature
1 A java program to find out whether entered integer is
prime or not using command line arguments.
2 A java program to find addition and multiplication of
two 2D Matrices.
3 A java program to create file using File Class and copy
data from on file to other file
4 A java program to read object and write into file using
Serializable Interface.
5 Write down Five Basic steps to establish JDBC
connection from Java Application. Also mention sample
code for each step
6 Write Java program to demonstrate how to connect to
a MYSQL database, execute a Query, retrieve data and
display it.
7 Java program to implement a simple JDBC application
8 Write down the Program for testing the Servlet and
study deployment descriptor.
9 Implement the shopping cart for users for the online
shopping. Apply the concept of session
10 Implement student registration form with enrolment
number, first name, last name, semester, contact
number. Store the details in database. Also implement
search, delete and modify facility for student records.
11 Write a Servlet program to print system date and time.
12 Implement Authentication filter using filter API.
13 Write down the Program for testing the servlet context
interface
14 Write down the Program which displays the simple JSP
file
15 Write down the program in which input the two
numbers in an html file and then display the addition in
JSP file.
Program 1: A java program to find out whether entered integer is
prime or not using command line arguments.
----------------------------------------------------------------------------------------------
public class PrimeCheck {
public static void main(String[] args)
{ if ([Link] == 0) {
[Link]("Please provide a number.");
return;
}
int num =
[Link](args[0]); boolean
isPrime = num > 1;
for (int i = 2; i <= [Link](num); i++)
{ if (num % i == 0) {
isPrime = false;
break;
}
}
[Link](num + (isPrime ? " is Prime." : " is Not Prime."));
[Link](“Rohit (24SCSE2140027)”);
}
}

PS C:\Users\kumar\onedrive\desktop> java PrimeCheck 25


25 is Not Prime.
Rohit (24SCSE2140027)
PS C:\Users\kumar\onedrive\desktop>
Program 2: A java program to find addition and multiplication of two
2D Matrices.
----------------------------------------------------------------------------------------------
public class MatrixOperations {
public static void main(String[] args) {

[Link]("Rohit(24SCSE214
0027)");

int[][] A = {{1, 2}, {3, 4}};


int[][] B = {{5, 6}, {7, 8}};
int[][] sum = new int[2][2];
int[][] product = new int[2][2];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++) sum[i]
[j] = A[i][j] + B[i][j];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++) product[i]
[j] += A[i][k] * B[k][j];
[Link]("Matrix Addition:");
printMatrix(sum);
[Link]("Matrix Multiplication:");
printMatrix(product); }
static void printMatrix(int[][] M)
{ for (int[] row : M) {
for (int val : row)
[Link](val + " ");
[Link]();
}}}

PS C:\Users\kumar\onedrive\desktop> javac [Link]


PS C:\Users\kumar\onedrive\desktop> java MatrixOperations
Rohit (24SCSE2140027)
Matrix Addition:
68
10 12
Matrix Multiplication:
19 22
43 50
Program 3: A java program to create file using File Class and copy data
from on file to other file
----------------------------------------------------------------------------------------------
import [Link].*;

public class FileCopy {


public static void main(String[] args) {
[Link]("Rohit(24SCSE2140027)");
try {
File inputFile = new File("[Link]");
FileWriter writer = new FileWriter(inputFile);
[Link]("Hello, this is test content.");
[Link]();
File outputFile = new File("[Link]");
FileReader fr = new FileReader(inputFile);
FileWriter fw = new FileWriter(outputFile);
int ch;
while ((ch = [Link]()) != -1) {
[Link](ch);
}
[Link]();
[Link]();
[Link]("File copied successfully.");
} catch (IOException e) {
[Link]();
}
}
}

PS C:\Users\kumar\onedrive\desktop> javac [Link]


PS C:\Users\kumar\onedrive\desktop> java FileCopy
Rohit(24SCSE2140027)
File copied successfully.
PS C:\Users\kumar\onedrive\desktop>
Program 4: A java program to read object and write into file using
Serializable Interface.
----------------------------------------------------------------------------------------------
import [Link].*;
class Student implements Serializable
{ int id;
String name;
Student(int id, String name)
{ [Link] = id;
[Link] = name; } }
public class SerializeDemo
{
public static void main(String[] args)
{ try {
Student s1 = new Student(101, "Divyanshu");
ObjectOutputStream out = new ObjectOutputStream(new
FileOutputStream("[Link]"));
[Link](s1);
[Link]();
ObjectInputStream in = new ObjectInputStream(new
FileInputStream("[Link]"));
Student s2 = (Student) [Link]();
[Link]();
[Link]("Student Data: " + [Link] + " " + [Link]);
} catch (Exception e) {
[Link]();
}
} }
[Running] cd "c:\Users\kumar\OneDrive\Desktop\" && javac [Link] && java SerializeDemo
Rohit (24SCSE2140027)
Student Data: 101 Divyanshu
Program 5: Write down Five Basic steps to establish JDBC connection
from Java Application. Also mention sample code for each step.

Step 1: Import JDBC packages


import [Link].*;
Step 2: Load and Register Driver
[Link]("[Link]");
Step 3: Establish Connection
Connection con =
[Link]("jdbc:mysql://localhost:3306/testdb","root","pas
sword");
Step 4: Create Statement
Statement stmt = [Link]();
Step 5: Execute Query and Process Results
ResultSet rs = [Link]("SELECT * FROM users");
while([Link]()) {
[Link]([Link](1) + " " + [Link](2));
}
[Link]();
Program 6: Write Java program to demonstrate how to connect to a
MYSQL database, execute a Query, retrieve data and display it.
----------------------------------------------------------------------------------------------
import [Link].*;
public class MySQLDemo {
public static void main(String[] args)
{ try {
// 1. Load Driver
[Link]("[Link]");
// 2. Establish Connection
Connection con =
[Link]("jdbc:mysql://localhost:3306/testdb", "root",
"password");
// 3. Create Statement
Statement stmt = [Link]();
// 4. Execute Query
ResultSet rs = [Link]("SELECT * FROM users");
// 5. Process Results
while ([Link]()) {
[Link]([Link]("id") + " - " + [Link]("name"));
}
[Link]();
} catch (Exception e) {
[Link]();
}
}
}

You might also like