Advanced Java Lab Guide
Advanced Java Lab Guide
Guided by
A. S. Gohil
1
Advanced JAVA-3360701
INDEX
2
Advanced JAVA-3360701
No. P P D S R
r a a i e
a g t g m
c e e n a
t r
i n k
c o
a
l
s
1 D
e
v
e
l
o
p
a
n
a
p
p
l
e
t
t
h
a
t
d
r
a
w
s
c
i
r
c
l
e
.
T
3
Advanced JAVA-3360701
h
e
d
i
m
e
n
s
i
o
n
o
f
t
h
e
a
p
p
l
e
t
s
h
o
u
l
d
b
e
5
0
0
3
0
0
p
i
4
Advanced JAVA-3360701
x
e
l
s
.
T
h
e
c
i
r
c
l
e
s
h
o
u
l
d
b
e
c
e
n
t
e
r
e
d
i
n
t
h
e
a
p
p
l
e
t
a
5
Advanced JAVA-3360701
n
d
h
a
v
e
r
a
d
i
u
s
o
f
1
0
0
p
i
x
e
l
s
.
D
i
s
p
l
a
y
y
o
u
r
n
a
m
e
c
6
Advanced JAVA-3360701
e
n
t
e
r
e
d
i
n
c
i
r
c
l
e
.
u
s
i
n
g
d
r
a
w
O
v
a
l
(
)
m
e
t
h
o
d
)
7
Advanced JAVA-3360701
2 D
r
a
w
t
e
n
r
e
d
c
i
r
c
l
e
s
i
n
v
e
r
t
i
c
a
l
c
o
l
u
m
n
i
n
t
h
e
c
e
8
Advanced JAVA-3360701
6 D
e
v
e
l
o
p
a
n
a
p
p
l
e
t
t
h
a
t
u
s
e
s
t
h
e
m
o
u
s
e
l
i
s
t
e
n
e
r
,
w
h
i
9
Advanced JAVA-3360701
7 D
e
v
e
l
o
p
p
r
o
g
r
a
m
t
h
a
t
h
a
s
o
n
l
y
o
n
e
b
u
t
t
o
n
i
n
t
h
e
f
10
Advanced JAVA-3360701
r
a
m
e
,
c
l
i
c
k
i
n
g
o
n
t
h
e
b
u
t
t
o
n
c
y
c
l
e
s
t
h
r
o
u
g
h
t
h
e
c
o
11
Advanced JAVA-3360701
l
o
r
s
:
r
e
d
-
>
g
r
e
e
n
-
>
b
l
u
e
a
n
d
s
o
o
n
.
O
n
e
c
o
l
o
r
c
h
a
n
g
12
Advanced JAVA-3360701
p
e
r
c
l
i
c
k
.
(
u
s
e
g
e
t
B
a
c
k
G
r
o
u
n
d
(
)
m
e
t
h
o
d
t
o
g
e
t
t
h
13
Advanced JAVA-3360701
c
u
r
r
e
n
t
c
o
l
o
r
)
14
Advanced JAVA-3360701
15
Advanced JAVA-3360701
Practical – 1
Aim: Develop an applet that draws a circle. The dimension of the applet should be
500 x 300 pixels. The circle should be centered in the applet and have a radius of
100 pixels. Display your name centered in a circle. ( using drawOval() method).
Ans:
Output:
16
Advanced JAVA-3360701
Practical – 2
Aim: Draw ten red circles in a vertical column in the center of the applet.
Ans:
Import java.awt.*;
import java. Applet.*;
/*<applet code= "SimpleApplet.class" width = "500" height= "500" >
</applet>*/
17
Advanced JAVA-3360701
Practical – 3
Aim: Built an applet that displays a horizontal rectangle in its center. Let the
rectangle fill with color from left to right.
Ans:
18
Advanced JAVA-3360701
19
Advanced JAVA-3360701
Practical – 4
Aim: Develop an applet that displays the position of the mouse at the upper left
corner of the applet when it is dragged or moved. Draw a 10x10 pixel rectangle filed
with black at the current mouse position.
Ans:
x=arg0.getX();
y=arg0.getY(); repaint();
}
}
Output:
20
Advanced JAVA-3360701
P
r
a
c
t
i
c
a
l
Aim: Develop an applet that contains one button. Initialize the label on the button to
“start”, when the user presses the button, which changes the label between these two
values each time the button is pressed.
21
Advanced JAVA-3360701
Ans:
else
{
b.setLabel("Start");
}
}
}
Output:
22
Advanced JAVA-3360701
Practical – 6
Aim: Develop an applet that uses the mouse listener, which overrides only two
methods, which are mousePressed and mouseReleased.
Ans:
MouseListener{
23
Advanced JAVA-3360701
Output:
24
Advanced JAVA-3360701
Practical – 7
Aim: Develop a program that has only one button in the frame, clicking on the
button cycles through the colors: red->green->blue and so on. One color changes per
click.(use getBackGround() method to get the current color) Ans:
25
Advanced JAVA-3360701
Output:
26
Advanced JAVA-3360701
27
Advanced JAVA-3360701
Practical – 8
Aim: Develop an program that contains three check boxes and 30 x 30 pixel
canvas.The three checkboxes should be labeled “Red”, “Green”,”Blue”. The selection
of the check boxes determine the color of the canvas. For example, if the user selects
both “Red” and “Blue”, the canvas should be purple.
Ans:
Frame f;
Checkbox c1;
Checkbox c2;
Checkbox c3; Canvas c;
int red = 0,blue=0,green=0;
JAVA_Practical_8()
{
f = new Frame("Practical - 8") ; c1
= new Checkbox("Red"); c2 = new
Checkbox("Green"); c3 = new
Checkbox("Blue");
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
f.add(c1);
f.add(c2);
f.add(c3);
f.add(c);
f.setSize(300,300);
f.setVisible(true);
}
28
Advanced JAVA-3360701
Output:
29
Advanced JAVA-3360701
30
Advanced JAVA-3360701
Practical – 9
Aim: Create an application that displays a frame with a menu bar. When a user
selects any menu or menu item, display that selection on a text area in the center of the
frame Ans:
JAVA_Practical_9()
Label("Select Menu.");
new MenuItem("Open");
new MenuItem("Save");
mi3.addActionListener(this);
Mi4.addActionListener(this); mn.add(mi1);
mn.add(mi2); mn.add(mi3);
mn.add(mi4); mb.add(mn);
f.setMenuBar(mb);
f.setLayout(new FlowLayout());
31
Advanced JAVA-3360701
f.add(l);
f.setSize(300,300);
f.setVisible(true); }
JAVA_Practical_9();
MenuItem)
{
l.setText(ae.getActionCommand());
Output:
32
Advanced JAVA-3360701
Sign:
Date:
33
Advanced JAVA-3360701
Practical – 10
Aim: Develop a program that draws two sets of
ever-decreasing rectangles one in outline form and one filled alternately in black and
white.
Ans:
import java.awt.*; public class JAVA_Practical_10
extends Canvas{
Frame f;
JAVA_Practical_10()
{ f = new Frame("Practical - 10");
f.setSize(500,300);
f.setVisible(true);
f.setLayout(new FlowLayout());
setSize(400,200); setVisible(true);
f.add(this); }
34
Advanced JAVA-3360701
JAVA_Practical_10();
{
g.setColor(Color.RED);
g.fillRect(0,0,400,200); int
while(i<19)
{ width=width-10;
height=height-10;
x=(400-width)/2; y=(200-height)/2;
if(i%2==1)
g.setColor(Color.WHITE);
else
g.setColor(Color.BLACK);
g.fillRect(x,y,width,height); i+
+; try{
Thread.sleep(1000);
} catch(InterruptedException e){
}
}
}
}
Output:
35
Advanced JAVA-3360701
36
Advanced JAVA-3360701
Practical – 11
import java.sql.*;
public class DbDemo
{
public static void main(String[] args)
{
Connection conn; Statement
stmt;
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Trying to connect with Database Server..");
conn=DriverManager.getConnection("jdbc:mysql://localhost/",
"root", "");
System.out.println("ConnectionEstablished Successfully...");
System.out.println("Trying to create Database..");
stmt = conn.createStatement();
String sql = "CREATE DATABASE STUDENTDB";
stmt.executeUpdate(sql);
System.out.println("Database Created Successfully...");
System.out.println("Trying to create Table..");
conn.close(); stmt.close();
}
catch(Exception e)
{
System.out.println("Exception Caught." + e); }
}
}
37
Advanced JAVA-3360701
Output:
Practical – 12
Aim: Develop a Graphical User Interface that performs the following SQL
operations: a) Insert b) Delete c) Update.
Ans:
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JTextField nm,enno,spi,delEno;
JLabel lblnm,lbleno,lblspi,lbldelno;
JButton sbmt,rtrv,dltRcrd,dlt;
JPanel pn1,pn2,pn3,pn4,pn5;
DatabaseDemo()
{ setSize(500,500); setLayout(new
FlowLayout()); nm = new
38
Advanced JAVA-3360701
JLabel("SPI : ");
pn1.setLayout(new FlowLayout());
pn1.add(lblnm); pn1.add(nm);
pn2.setLayout(new FlowLayout());
pn2.add(lbleno); pn2.add(enno);
pn3.setLayout(new FlowLayout());
pn3.add(lblspi); pn3.add(spi);
pn4.setLayout(new FlowLayout());
pn4.add(sbmt); pn4.add(rtrv);
new JPanel();
39
Advanced JAVA-3360701
setVisible(true); dlt.addActionListener(this);
dltRcrd.addActionListener(this);
sbmt.addActionListener(this);
rtrv.addActionListener(this);
addWindowListener(this);
actionPerformed(ActionEvent ae)
{
String acmd = ae.getActionCommand();
"Insert")
{
String arg[] = new String[3]; arg[0] =
nm.getText().toString(); arg[1] =
enno.getText().toString(); arg[2] =
stud_info values(?,?,?)",arg);
} if(acmd == "Retrive")
{ dbc.retriveRecords();
} if(acmd == "Delete")
40
Advanced JAVA-3360701
pn5.setVisible(false);
pn5.setVisible(true);
}
public static void main(String s[])
{ new DatabaseDemo();
} } class
DbConnect
Connection cnn;
PreparedStatement pstmt;
String DB_URL =
"jdbc:mysql://localhost:3306/Students
";
DbConnect()
try
41
Advanced JAVA-3360701
Class.forName("com.mysql.jdbc.Driver"); cnn
=DriverManager.getConnection(DB_URL,USER,PASS);
} catch(SQLException se)
{ se.printStackTrace();
} catch(ClassNotFoundException cnfe)
cnfe.printStackTrace();
try
{ pstmt = cnn.prepareStatement(qry);
pstmt.setString(1,arg[0]);
pstmt.setInt(2,Integer.parseInt(arg[1]));
pstmt.setString(3,arg[2]);
pstmt.executeUpdate();
} catch(SQLException se)
{ se.printStackTrace();
} } public void
retriveRecords()
try
* from stud_info");
42
Advanced JAVA-3360701
System.out.println("----DATABASE RECORDS----");
System.out.println("Name | En_No | SPI");
System.out.println("---------------");
while(rs.next())
{
System.out.println(rs.getString(1)+" | "+rs.getInt(2)+" | "+rs.getString(3));
}}
catch(SQLException se)
{ se.printStackTrace();
delno)
{
try
{ pstmt = cnn.prepareStatement("delete from stud_info where en_no=?");
pstmt.setInt(1,Integer.parseInt(delno)); pstmt.executeUpdate();
} catch(SQLException se)
{ se.printStackTrace();
}
}
}
Output:
43
Advanced JAVA-3360701
44
Advanced JAVA-3360701
45
Advanced JAVA-3360701
Practical – 13
Aim: Develop a program to present a set of choice for user to select a product and
display the price of product.
Ans:
46
Advanced JAVA-3360701
Output:
Practical – 14
Aim: Develop a simple servlet program which maintains a counter for the number of
times it has been accessed since its loading, initialize the counter using deployment
descriptor. Ans:
HitCounter.java:
import java.io.IOException; import
java.io.PrintWriter; import
javax.servlet.*;
import javax.servlet.http.HttpServlet; import
javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse;
47
Advanced JAVA-3360701
Web.xml:
<servlet>
<servlet-name>pagecounter</servlet-name>
<servlet-class>HitCounter</servlet-class>
<init-param>
<param-name>counter</param-name>
<param-value>0</param-value>
</init-param>
</servlet>
Output:
48
Advanced JAVA-3360701
Practical – 15
Aim :- Create a web form which processes servlet and demonstrates use of sessions.
Ans:
login.html:
<html>
<head> <title> Login Page </title>
</head>
<body>
<form action="loginservlet" method="post" >
<table>
<tr>
<td>User Name: </td>
<td> <input type = "text" name = "name"> </td>
</tr>
<tr>
<td>Password: </td>
<td> <input type = "password" name = "pass"> </td>
</tr>
<tr>
<td> </td>
<td> <input type = "submit" name = "submit" value = "SUBMIT"> </td> </tr>
</form>
</body>
</html>
loginservlet.java:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class loginservlet extends HttpServlet
{
protected void doPost (HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException
{
res.setContentType("text/html");
49
Advanced JAVA-3360701
PrintWriter out=res.getWriter();
String name=req.getParameter("name"); String
password=req.getParameter("pass");
if(name.equals("Aasti") && password.equals("Singh"))
{
HttpSession session=req.getSession(); session.setAttribute("Name", name);
out.println("<br/><h1>Welcome :"+ name + "</h1> "); out.println("<br/> <a
href=profile> Click here </a>");
}
else
{ out.println("<h1>You Have entered Wrong Password </h1>");
out.println("<br/> <a href=login.html> Click here </a> to Login"); }
}
}
Profile.java:
import java.io.IOException; import
java.io.PrintWriter; import
javax.servlet.ServletException; import
javax.servlet.http.HttpServlet; import
javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse; import
javax.servlet.http.HttpSession; public class profile
extends HttpServlet
{
protected void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{ res.setContentType("text/html");
PrintWriter out=res.getWriter();
HttpSession session=req.getSession(false); if(session!=null)
{
String name=(String) session.getAttribute("Name"); out.println("Welcome," +
name);
out.println("<h1>This is Session Program. </h1> "); out.println("<a
}
else
{ out.println("<br> <h1> Plz Login First</h1>"); out.println("<br/> <a
href=login.html> Click here </a> to Login");
}
}
50
Advanced JAVA-3360701
Logoutservlet.java :
import java.io.IOException; import
java.io.PrintWriter; import
javax.servlet.ServletException; import
javax.servlet.http.HttpServlet; import
javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse; import
javax.servlet.http.HttpSession;
Web.xml:
<servlet>
<servlet-name>loginservlet</servlet-name>
<servlet-class>loginservlet</servlet-class> </servlet>
<servlet>
<servlet-name>logoutservlet</servlet-name>
<servlet-class>logoutServlet</servlet-class> </servlet>
<servlet>
<servlet-name>profile</servlet-name>
<servlet-class>profile</servlet-class> </servlet>
<servlet-mapping>
<servlet-name>profile</servlet-name>
<url-pattern>/profile</url-pattern>
</servlet-mapping>
51
Advanced JAVA-3360701
<servlet-mapping>
<servlet-name>logoutservlet</servlet-name>
<url-pattern>/logoutservlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>loginservlet</servlet-name>
<url-pattern>/loginservlet</url-pattern> </servlet-mapping>
Output:
52
Advanced JAVA-3360701
53
Advanced JAVA-3360701
Aim: Develop a
Practical – 16
Develop a simple JSP program for user registration and then control will be
transfer it into second page.
Ans:
firstpage.jsp:
<html>
<head>
<title>Registration Page</title>
</head>
<body>
<input type="submit">
</form>
</body>
</html>
Secondpage.jsp:
<html>
<head>
<title>Welcome Page</title>
</head>
54
Advanced JAVA-3360701
<body>
%>
</body>
</html>
Output:
55
Advanced JAVA-3360701
Aim: Develop a
Practical – 17
simple JSP program for user login form with static and dynamic
database.
Ans:
login.jsp:
<html>
<head>
</head>
<body>
type="password" name="pass"><br/>
</html>
welcome.jsp:
<% String username=request.getParameter("uname");
Stringpassword=request.getParameter("pass");
} else
%>
56
Advanced JAVA-3360701
Output:
57
Advanced JAVA-3360701
Aim: Develop a
Practical – 18
JSP program to display the grade of a student by accepting the
marks of five subjects.
Ans:
input.jsp:
<html>
<head>
<title>Subject Marks</title>
</head>
<body>
<p><input type="submit"></p>
</form>
</body>
</html>
result.jsp:
<%
58
Advanced JAVA-3360701
PHP=Integer.parseInt(request.getParameter("php")); int
WNS=Integer.parseInt(request.getParameter("wns")); int
ANDROID=Integer.parseInt(request.getParameter("android")); int
PROJECT=Integer.parseInt(request.getParameter("pro"));
59
Advanced JAVA-3360701
%>
Output:
60
Advanced JAVA-3360701
Sign:
Date:
61