Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

S.Y.BSc(C.S.

) Java Advanced

Exam Seat No:

Satish Pradhan Dnyanasadhana College


Thane

Certificate

This is to certify that Mr. : PRATHAMESH P. SONKAMBLE of S. Y. C. S. B.


Sc. Computer Science (Semester-IV) Class has successfully completed all the
practical work in subject Advance Java, under the guidance of Asst. Prof.
Pragati Ubale (subject in charge) during Year 2020-21 in partial fulfillment of
Computer Science PracticalExamination conducted by University of Mumbai.

Subject in charge

Date Head of the Department

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

Satish Pradhan Dnyanasadhana College


(Arts, Science And Commerce)
Department of Computer Science
Class: S. Y. CS Subject: Advance JAVA

INDEX

Pr. No Name of the Practical Date Signature

1. Develop the presentation layer of Library


Management software application with suitable
menus.
2. Design suitable database for Library Management
System.

3. Develop business logic layer for Library


Management System.

4. Develop Java application to store image in a


database as well as retrieve image from database.

5. Write a Java application to demonstrate servlet life


cycle.

6. Design database for student administration.


Develop servlet(s) to perform CRUD operations.

7. Create Employees table in EMP database. Perform


select, insert, update, and delete operations on
Employee table using JSP.
8. Write a Student class with three properties. The
useBean action declares a JavaBean for use in a
JSP. Write Java application to access JavaBeans
Properties.

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

Practical 1

Aim: Develop the presentation layer of Library Management software application with
suitable menus.

Source Code:

import java.awt.*;

import javax.swing.*;

public class Menu1 extends JFrame

JMenu m1,m2,m3,m4;

JMenuBar mb1;

JMenuItem i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11;

public Menu1()

mb1=new JMenuBar();

setJMenuBar(mb1);

m1=new JMenu("File");

m2=new JMenu("Edit");

m3=new JMenu("View");

m4=new JMenu("Help");

i1=new JMenuItem("New");

i2=new JMenuItem("Open");

i3=new JMenuItem("Save");

i4=new JMenuItem("Save As");

i5=new JMenuItem("Undo");

i6=new JMenuItem("Cut");

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

i7=new JMenuItem("Copy");

i8=new JMenuItem("Zoom");

i9=new JMenuItem("Menu Bar");

i10=new JMenuItem("About us");

i11=new JMenuItem("Exit");

m1.add(i1);

m1.add(i2);

m1.add(i3);

m1.add(i4);

m2.add(i5);

m2.add(i6);

m2.add(i7);

m3.add(i8);

m3.add(i9);

m4.add(i10);

m4.add(i11);

mb1.add(m1);

mb1.add(m2);

mb1.add(m3);

mb1.add(m4);

mb1.add(m5);

setSize(800,400);

setVisible(true);

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

public static void main(String[] args)

new Menu1();

Output:-

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

Practical 2
Aim: Design suitable database for Library Management System.
Source Code:

import java.sql.*;
public class BookDetails {
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/library";
Connection con=DriverManager.getConnection(url,"root","12345");
Statement st=con.createStatement();
String sql="insert into book values('10','java','balguru',300,'IBZ1001','3rd')";
String sql1="insert into book values('20','dbms','balguru',200,'IBZ1002','1st')";
String sql2="insert into book values('30','net','balguru',100,'IBZ1003','2nd')";
String sql4 = "update book set BName='Michael Sam' where BookId=10";
st.executeUpdate(sql);
st.executeUpdate(sql1);
st.executeUpdate(sql2);
st.executeUpdate(sql4);
st.close();
con.close();
}
catch(Exception e)
{
System.out.println("error:"+e.getMessage());

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

}
}

Output:-

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

Practical 3
Aim: Develop business logic layer for Library Management System.
Source Code:
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
public class book extends JFrame implements ActionListener {
JLabel l1,l2,l3,l4,l5,l6,l7;
JTextField t1,t2,t3,t4,t5,t6;
JButton b1,b2,b3,b4;
book(){
l1=new JLabel("Book Id");
t1=new JTextField(10);
l2=new JLabel("Book Name");
t2=new JTextField(10);
l3=new JLabel("Book Author");
t3=new JTextField(10);
l4=new JLabel("Price");
t4=new JTextField(10);
l5=new JLabel("ISBN");
t5=new JTextField(10);
l6=new JLabel("Edition");
t6=new JTextField(10);
b1=new JButton("ADD");
b2=new JButton("Clear");

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

b3=new JButton("Update");
b4=new JButton("Delete");
l7=new JLabel(" ");
add(l1);add(t1);

add(l2);add(t2);
add(l3);add(t3);
add(l4);add(t4);
add(l5);add(t5);
add(l6);add(t6);
add(b1);add(b2);
add(b3);add(b4);
add(l7);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
setVisible(true);
setSize(500,500);
setLayout(new GridLayout(9,2));

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
new book();

@Override
public void actionPerformed(ActionEvent ae) {

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

if(ae.getSource()==b1){
int id=Integer.parseInt(t1.getText());
String n=t2.getText();
String a=t3.getText();
String p=t4.getText();
String isbn=t4.getText();
String ed=t6.getText();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/library","root","12345");
String sql="insert into book values(?,?,?,?,?,?)";
PreparedStatement ps=con.prepareStatement(sql);
ps.setInt(1,id);
ps.setString(2,n);
ps.setString(3,a);
ps.setString(4,p);
ps.setString(5,isbn);
ps.setString(6,ed);

int i=ps.executeUpdate();
if(i>0)

l7.setText("Record Added Successfully");

}catch(Exception e){
System.out.print(e.getMessage());
}

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

}
if(ae.getSource()==b2)
{
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
t6.setText("");
}
if(ae.getSource()==b3){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/library","root","12345");
String sql="update book set BName=? where BookId=?";
PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1,t2.getText());
ps.setString(2,t1.getText());
int i=ps.executeUpdate();
if(i>0)
l7.setText("Record Updated Successfully");
}catch(Exception e){
System.out.print(e.getMessage());

}
}
if(ae.getSource()==b4){

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

try{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/library","root","12345");
String sql="delete from book where BookId=?";
PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1,t1.getText());

int i=ps.executeUpdate();
if(i>0)
l7.setText("Record Delete Successfully");
}catch(Exception e){
System.out.print(e.getMessage());
}
}
}
}

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

Output:-

Insert Record:-

Update Record:-

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

Delete Record:-

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

Practical 4
Aim: Develop Java application to store image in a database as well as retrieve image from
database.
Source Code:
i) Insert Image:-
import java.io.*;
import static java.lang.Class.forName;
import java.sql.*;
public class InsertImg {
public static void main(String args[]) throws Exception{
Class.forName("com.mysql.jdbc.Driver");

String url="jdbc:mysql://localhost:3306/library";
Connection con=DriverManager.getConnection(url,"root","12345");
System.out.println("Connection established...");
String sql="Insert into imagetable values(?)";
PreparedStatement ps=con.prepareStatement(sql);
InputStream in=new FileInputStream("C:\\Users\\Computer-
lab\\Desktop\\sahha\\download.png");
ps.setBlob(1,in);

ps.execute();

System.out.println("Record Inserted");

}
}

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

Output:-

ii) Retrieve Image:-


import java.io.*;
import java.sql.*;
public class retrieve {
public static void main(String args[]){
try{
String url="jdbc:mysql://localhost:3306/library";
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(url,"root","12345");
String sql="select * from imagetable";
PreparedStatement ps=con.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
if(rs.next()){

Blob b=rs.getBlob(1);
byte barr[]=b.getBytes(1,(int)b.length());
FileOutputStream fout=new FileOutputStream("C:\\Users\\Computer-
lab\\Desktop\\sahha\\retrieve1.jpg");

fout.write(barr);
fout.close();
}

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

System.out.println("Okk");
con.close();
}catch(Exception e){
e.printStackTrace();

}
Output:-

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

Practical 5
Aim: Write a Java application to demonstrate servlet life cycle.
Source Code:
index.html

<html> <head>
<title>CALCULATOR</title>
</head>
<body>
<form action="calculator" method="post">
<div align="center">
<input type="text" name="txt1" >
<select name="operation">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" name="txt2" ><br>
<input type="submit">
</div>
</form>
</body>
</html>

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

Servlet Code:

calculator.java
import java.io.*; import javax.servlet.*;
import javax.servlet.http.*; public class
calculator extends HttpServlet

{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException
{
PrintWriter out = response.getWriter(); try

{
String s3=request.getParameter("operation"); int

a,b;

a=Integer.parseInt(request.getParameter("txt1"));

b=Integer.parseInt(request.getParameter("txt2")); if(s3.equals("+"))

{
out.println("addition="+(a+b));
}
if(s3.equals("-"))
{
out.println("subtraction="+(a-b));
}
if(s3.equals("*"))
{
out.println("multiplication="+(a*b));

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

if(s3.equals("/"))

{
out.println("division="+(a/b));
}
}
finally {

out.clos

e();

}
}
}
Output:-

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

Practical 6
Aim: Design database for student administration. Develop servlet(s) to perform CRUD
operations.
Source Code:
index.html

<html>

<body>

<form action="Student" method="get" id="myform">

stud id: <input type="text" name="id"><br>

stud name: <input type="text" name="name"><br>

address: <input type="text" name="address"><br>

class: <input type="text" name="class"><br>

<input type="submit" value="add" name="add">

<input type="submit" value="delete" name="del">

<input type="submit" value="update" name="update">

<input type="submit" value="Retrieve" name="retrieve">

</form>

</body>

</html>

Student.java

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.*;

import javax.servlet.http.*;

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

import java.sql.*;

public class Student extends HttpServlet

@Override

public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException

PrintWriter out=res.getWriter();

String b1=req.getParameter("add");

String b2=req.getParameter("del");

String b3=req.getParameter("update");

String b4=req.getParameter("retrieve");

try{

if(b1.equals("add"))

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/library","root","12345");

PreparedStatement stmt=con.prepareStatement("insert into student values(?,?,?,?)");

stmt.setString(1,req.getParameter("id"));

stmt.setString(2,req.getParameter("name"));

stmt.setString(3, req.getParameter("address"));

stmt.setString(4, req.getParameter("class"));

int i=stmt.executeUpdate();

out.println(i+" records inserted");

con.close();

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

}catch(Exception e)

try{

if(b2.equals("delete"))

Class.forName("com.mysql.jdbc.Driver");

Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/library","root","12345");

PreparedStatement stmt=con.prepareStatement("delete from student where ID=?");

stmt.setString(1,req.getParameter("id"));

int i=stmt.executeUpdate();

out.println(i+" records deleted");

con.close();

}catch(Exception e)

try

if(b3.equals("update"))

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/library","root","12345");

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

PreparedStatement stmt=con.prepareStatement("update student set NAME=? where ID=?");

stmt.setString(1,req.getParameter("name"));

stmt.setString(2,req.getParameter("id"));

int i=stmt.executeUpdate();

out.println(i+" records updated");

con.close();

}catch(Exception e)

try

if(b4.equals("retrieve"))

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/library","root","12345");

PreparedStatement stmt=con.prepareStatement("select * from student ");

ResultSet rs=stmt.executeQuery();

while(rs.next())

out.print(rs.getString(1) +" " + rs.getString(2)+" " + rs.getString(3)+" " + rs.getString(4));

con.close();

}catch(Exception e)

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

}
}

Output:-

1) Inserting Student Data:-

2) Udating Student Data:-

Enter existing student id and name

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

3) Deleting Student data:-

4) Retrieving Student Data:-

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

Practical 7
Aim: Create Employees table in EMP database. Perform select, insert, update, and delete
operations on Employee table using JSP.
Source Code:
index.html

<html>

<body>

<form action="employee.jsp" method="get" id="myform">

emp id: <input type="text" name="id"><br>

emp name: <input type="text" name="name"><br>

salary: <input type="text" name="salary"><br>

department: <input type="text" name="dept"><br>

<input type="submit" value="add" name="add">

<input type="submit" value="delete" name="del">

<input type="submit" value="update" name="update">

<input type="submit" value="Retrieve" name="retrieve">

</form>

</body>

</html>

employee.jsp

<%@page import="java.sql.*;"%>

<%

String b1=request.getParameter("add");

String b2=request.getParameter("del");

String b3=request.getParameter("update");

String b4=request.getParameter("retrieve");

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

try{

if(b1.equals("add"))

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/library","root","12345");

PreparedStatement stmt=con.prepareStatement("insert into emp values(?,?,?,?)");

stmt.setString(1,request.getParameter("id"));

stmt.setString(2,request.getParameter("name"));

stmt.setString(3, request.getParameter("salary"));

stmt.setString(4, request.getParameter("dept"));

int i=stmt.executeUpdate();

out.println(i+" records inserted");

con.close();

}catch(Exception e)

try{

if(b2.equals("delete"))

Class.forName("com.mysql.jdbc.Driver")

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/library","root","12345");

PreparedStatement stmt=con.prepareStatement("delete from emp where ID=?");

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

stmt.setString(1,request.getParameter("id"));

int i=stmt.executeUpdate();

out.println(i+" records deleted");

con.close();

}catch(Exception e)

try

if(b3.equals("update"))

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/library","root","12345");

PreparedStatement stmt=con.prepareStatement("update emp set NAME=? where ID=?");

stmt.setString(1,request.getParameter("name"));

stmt.setString(2,request.getParameter("id"));

int i=stmt.executeUpdate();

out.println(i+" records updated");

con.close();

}catch(Exception e)

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

try

if(b4.equals("Retrieve"))

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/library","root","12345");

PreparedStatement stmt=con.prepareStatement("select * from emp ");

ResultSet rs=stmt.executeQuery();

while(rs.next())

out.println(rs.getString(1) +" " + rs.getString(2)+" " + rs.getString(3)+" " + rs.getString(4)


+ "<br>");

out.print("\n");

con.close();

}catch(Exception e)

System.out.print(e.getMessage());

%>

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

Output:-

1) Inserting Employee Data:-

2) Updating Employee data:-

Enter Employee Id and Name

3) Deleting Employee Data:-

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

4) Retrieving Employee Data:-

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

Practical 8
Aim: Write a Student class with three properties. The useBean action declares a JavaBean
for use in a JSP. Write Java application to access JavaBeans Properties.
Source Code:
index.html

<html>

<body>

<h1>Student database</h1>

<form action="Student.jsp">

Roll No. <input type="text" name="rollno"> <br>

SName <input type="text" name="sname"><br>

Mobile<input type="text" name="mobile"><br>

<input type="submit" value="Submit">

<input type="reset" value="Clear">

</form>

</body>

</html>

Student.jsp

<html>

<body>

<h1>Student Details</h1>

<jsp:useBean id="stud" scope="session" class="StudBean.StudData">

</jsp:useBean>

<jsp:setProperty name="stud" property="*"></jsp:setProperty>

Roll No:<jsp:getProperty name="stud" property="rollno"/><br>

Sname:<jsp:getProperty name="stud" property="sname"/><br>

Mobile : <jsp:getProperty name="stud" property="mobile"/><br>

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

</body>

</html>

StudData.java

package StudBean;

public class StudData

int rollno;

String sname,mobile;

public int getRollno() {

return rollno;

public void setRollno(int rollno) {

this.rollno = rollno;

public String getSname() {

return sname;

public void setSname(String sname)

this.sname = sname;

public String getMobile()

return mobile;

public void setMobile(String mobile)

Rollno:52 Name: Prathamesh Sonkamble


S.Y.BSc(C.S.) Java Advanced

this.mobile = mobile;

Output:-

Rollno:52 Name: Prathamesh Sonkamble

You might also like