Advance Java Lab Manual

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 93

Computer Department Tolani F.

G Polytechnic ---Advance Java Programing(3360701) 1

TOLANI F. G. POLYTECHNIC, ADIPUR


NR. RAILWAY STATION, ADIPUR (KACHCHH)
DEPARTMENT OF COMPUTER ENGINEERING

LAB MANUAL
OF
ADVANCE JAVA PROGRAMMING
(3360701)

SEMESTER – 6

Student Name : PRIYANSHU HARESH GOODWANI

Enrollment No. : 206530307032

Term : 6th SEMESTER

Term Date: : 23/01/2023 To 20/05/2023

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 2

TOLANI F. G. POLYTECHNIC, ADIPUR


NR. RAILWAY STATION, ADIPUR (KACHCHH)
DEPARTMENT OF COMPUTER ENGINEERING

Certificate
This is to certify that

Mr./Miss Priyanshu Haresh Goodwani_________________

of Diploma Computer Engineering Semester 6th Enrollment No .

206530307032 has satisfactorily completed his/her term work in

Advance Java Programming (3360701) for the term ending

in_________

Date of Submission: - ____/____/______

Subject In-charge Head of Department

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 3

Institute vision:
To develop Skilled, Creative and Socially Sensitive Engineers to Satisfy the Needs of a
Changing Society.

Institute Mission:
Imparting Value Added Technical Education for Skill Development through
Adequate Resources, while Developing Aptitude for Lifelong Learning and
Promoting Creativity, Innovation and Entrepreneur Spirit.

Department vision:
To become a recognized leader in Gujarat by producing Technically Skilled, Creative,
Independent and Socially Inclined Diploma Computer Engineers contributing towards the
ever changing needs of society.

Department Mission:
(1) To provide quality undergraduate education in both the theory and applied computer
science and train the students to implement this knowledge to solve the problems of real
world.
(2) To develop positive attitude towards lifelong learning according to the demands of versatile
society.
(3) To support other academic and socially interactive programs conducted by the institute.

Program Educational Objectives (PEOs)


Our graduates will….
1) Excel in higher education and professional career.
2) Provide well-defined solutions in related domains with professional ethics.
3) Be an entrepreneur in computer and allied domains

Program Specific Outcomes (PSOs)

Our graduates will….

1) Design and develop Desktop, Web, and Android Application to provide Solution for
problems
2) Manage, support, and troubleshoot hardware, software, networking components,
operating system (OS), and data storage in IT Infrastructure.

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 4

Index
SR. Date C Signature
PRACTICAL
NO. O
1 1 Develop a program to Test life cycle of an Applet.
2 Develop a program to print “WELCOME TO APPLET” in center
of Applet window with BLUE color.
3 Develop an applet that draw a two circle of radius 50 pixel in
horizontal line (one black color, one red color)
4 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)
5 Draw ten filled red circles in a vertical column in the center of
the applet.
6 Develop an applet that sets the background color to yellow and
draw circle of radius 50 pixels centered in the middle of the
applet. Fill the circle with red color.
7 Develop an Applet to display Your name and Your City name
using passing the parameters to the Applet.
8 2 Develop a program that contains TextField, Push Button and
Label in the frame, clicking on the button content of the
TextField will displayed on Label. If TextField is empty then
display "Please Enter Text" on Label.
9 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
10 2 Develop an program that contains three check boxes and 50 x 50
pixel canvas.The three checkboxes should be labeled “Red”,
“Green”,”Blue”. The selection of the check boxes determines the
color of the canvas. For example, if the user selects both “Red”
and “Blue”, the canvas should be purple.

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 5

11 Develop a program that draws two sets of ever-decreasing


rectangles one in outline form and one filled alternately in black
and white.
12 Develop a program to design a Registration Page using different
sub classes of Jcomponent class and display information in pop-
up window.
13 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
14 Develop an applet which draw a line between two points 1)
mouse pressed and 2) mouse released
15 Develop an applet which draw a string "Mouse Clicked" at the
position of cursor when mouse is click
16 Develop an applet that uses the mouse listener, which overrides
only two methods which are mousePressed and mouseReleased
17 Develop an applet that contains one button. Initialize the label on
the button to "start" and "stop", when the user presses the button,
which changes the label between these two values each time the
button is pressed.
18 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)
19 3 Develop a JDBC application that uses any JDBC driver to
perform insert, update, delete and display all records in/from
database. (Assume suitable database)
20 Develop a Graphical User Interface that performs the following
SQL operations: a) Insert b) Delete c) Update. (Assume suitable
database)
21 Develop a program to present a set of choice for user to select a

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 6

product and display the price of product.


22 4 Develop a Servlet code to print “Hello”.
23 Develop a servlet to read values of textboxes and display its
values on the page using doGet() and doPost() methods, from an
HTML code (create login form having one submit button, two
textboxes labeled as Login name and Password respectively)
when user clicked on submit button.
24 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.
25 Create a web form which processes servlet and demonstrates use
of cookies and sessions.
26 Develop servlet to read records from database and displaying
them on page.
27 5 Develop a JSP to determine how many times the visitors has
loaded the page.
28 Develop a JSP application to following operations on database.
(Assume suitable database)
1) Insert and Update record into database table
2) Delete and Select records from database table

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 7

TOLANI F. G. POLYTECHNIC, ADIPUR


COMPUTER ENGINEERING

RUBRICS FOR LABORATORY PRACTICALS ASSESSMENT

SUBJECT NAME: ADVANCE JAVA PROGRAMING


SUBJECT CODE: 3360701
TERM:

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 8

TOLANI F. G. POLYTECHNIC, ADIPUR


COMPUTER ENGINEERING

LABORATORY PRACTICALS ASSESSMENT

SUBJECT NAME: ADVANCE JAVA PROGRAMING


SUBJECT CODE: 3360701
TERM: Even
NAME:
ENROLLMENT NO. :
CLASS:
BATCH:

PRAC. CO FACULTY
RB1 RB2 RB3 RB4 TOTAL DATE SIGN
NO. NO.

1 CO1

2 CO2

3 CO3

4 CO4

5 CO5

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 9

1. Develop a program to Test life cycle of an Applet.

import java.applet.*;
import java.awt.*;

/*
<applet code="collage1.class" width="200" height="200">
</applet>

*/
public class collage1 extends java.applet.Applet {

String msg="Priyanshu's Applet";

public void init() {

msg+="init()";
}
public void start() {

msg+="start()";
}
public void stop(){

msg+="stop()";
}
public void paint(Graphics g) {

g.drawString(msg,100,100);
showStatus("Priyanshu applet");
}
}

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 10

output:-

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 11

2. Develop a program to print “WELCOME TO APPLET” in center of Applet window with


BLUE color.

import java.applet.*;
import java.awt.*;

/*<applet code="collage3.class" height="200" width="200"></applet>*/

public class collage3 extends java.applet.Applet {

Font f1;
public void init() {

f1 = new Font("Arial",Font.BOLD,18);

public void paint(Graphics g) {

g.setColor(Color.blue);
g.setFont(f1);
g.drawString("Welcome to Priyanshu Applet",50,100);
showStatus("206530307032-Priyanshu Applet");

}
}

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 12

output:-

3. Develop an applet that draw a two circle of radius 50 pixel in horizontal line (one black
color, one red color)

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 13

import java.applet.*;
import java.awt.*;

/*<applet code="program2.class" height="400" width="400"></applet>*/

public class program2 extends java.applet.Applet {

Font f1;
public void init() {

f1 = new Font("Arial",Font.BOLD,18);

public void paint(Graphics g) {

g.fillOval(50,100,100,100);
g.setColor(Color.black);

g.setColor(Color.red);
g.fillOval(210,100,100,100);

showStatus("206530307032-Priyanshu's Applet");

}
}

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 14

output:-

4. 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)

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 15

import java.applet.*;
import java.awt.*;
/*
<applet code = "P5.class" width="500" height="300">
</applet>
*/

public class P5 extends java.applet.Applet {

public void init() {

setSize(50,50);

public void paint(Graphics g) {

g.drawOval(50,150,200,200);
g.drawString("Priyanshu",140,240);
showStatus("206530307032--Priyanshu's applet");

}
}

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 16

output:-

5. Draw ten filled red circles in a vertical column in the center of the applet.

import java.applet.*;

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 17

import java.awt.*;
/*
<applet code = "P6.class" width="500" height="700">
</applet>
*/

public class P6 extends java.applet.Applet {

public void init() {

setSize(50,50);

public void paint(Graphics g) {

int x=50;
for(int i=0;i<10;i++){
g.setColor(Color.red);
g.fillOval(225,x,50,50);

x=x+50;
}
showStatus("206530307032--Priyanshu's applet");
}

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 18

output:-

6. Develop an applet that sets the background color to yellow and draw circle of radius 50
pixels centered in the middle of the applet. Fill the circle with red color.

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 19

import java.applet.*;
import java.awt.*;
/*<applet code="Practical6.class" height="400" width="400"></applet>*/
/*
*Set background color of an applet using
* void setBackground(Color c) method.
*/
public class Practical6 extends java.applet.Applet {

Font f1;
public void init() {

setSize(50,50);

f1 = new Font("Arial",Font.BOLD,18);

public void paint(Graphics g) {

g.setColor(Color.red);
g.fillOval(50,100,100,100)
setBackground(Color.yellow);
showStatus("206530307032-Priyanshu's Applet");

}
}

output:-

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 20

7. Develop an Applet to display Your name and Your City name using passing the parameters
to the Applet.

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 21

import java.applet.*;
import java.awt.*;
/*<applet code="P7.class" height=300 width=600>
<param name="nm" value="Priyanshu Goodwani"> </param>
<param name="City" value="Gandhidham"> </param>

</applet>*/
public class P7 extends java.applet.Applet {

public void paint (Graphics g)


{
String nm=getParameter("nm");
String City = getParameter("City");
Font f=new Font("Calibri", Font.BOLD | Font.ITALIC,28);
g.setFont(f);
g.drawString("Your NAME IS: "+nm,70,50);
g.drawString("Your City is: "+City,70,100);

showStatus("priyanshu parameter method");


}

output:-

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 22

7(b)-Built an applet that displays a horizontal rectangle in its center. Let the rectangle fill with color from
left to right.

import java.awt.*;
/*<applet code="prac7_2.class" height="250" width="400"></applet>*/
public class prac7_2 extends java.applet.Applet {

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 23

public void init() {

public void paint(Graphics g) {


int x1=100, y1=100, y2=50;
g.setColor (Color.red);
g.drawRect (100, 100, 200, 50);
for (x1=100;x1<300;x1=x1+5){
try
{
Thread. sleep(200);
g.fillRect (x1, y1, 5, y2);
}
catch (Exception e){
e.printStackTrace();
}
}
}
}

output:-

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 24

8. Develop a program that contains TextField, Push Button and Label in the frame, clicking on the
button content of the Textield will displayed on Label. If TextField is empty then display "Please
Enter Text" on Label.

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 25

import java.awt.*;
import java.awt.event.*;

/* <applet code="Practical8.class" width="300" height="300"></applet>*/

public class Practical8 extends java.applet.Applet {

Frame f;
Label lb1;
Label lb2;
Button sub;
TextField txt;

public Practical8()
{
f = new Frame();
lb1 = new Label("Enter Name");
lb2 = new Label(" ");
txt = new TextField(10);
sub = new Button("Submit");

public void init() {


f.setLayout(new FlowLayout());
f.add(lb1);
f.add(txt);
f.add(sub);
f.add(lb2);

sub.addActionListener(this);

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 26

f.setSize(400,400);
f.show();

public void actionPerformed(ActionEvent e)


{
String s = txt.getText();

if(s.equals(""))
{
lb2.setText("Please Enter Text");
}
else
{
lb2.setText(s);
}
}

public static void main(String [] args)


{
Practical8 p = new Practical8();
}

Output1:-

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 27

Output2:-

9. 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.

import java.awt.*;
import java.awt.event.*;

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 28

public class practical9 extends Frame implements ActionListener {


TextArea a;
public practical9() {
a=new TextArea();
addWindowListener(new Terminator());

setSize(300,300);
setVisible(true);

MenuBar m=new MenuBar();


setMenuBar(m);

Menu f=new Menu("File");


m.add(f);

Menu e=new Menu("Edit");


m.add(e);

MenuItem m1;
m1=new MenuItem("Open");
m1.addActionListener(this);
f.add(m1);

m1=new MenuItem("Close");
m1.addActionListener(this);
f.add(m1);

m1=new MenuItem("Undo");
m1.addActionListener(this);
e.add(m1);

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 29

setLayout(new FlowLayout(FlowLayout.CENTER));
add(a);

public void actionPerformed(ActionEvent e){

if(e.getSource() instanceof MenuItem)


{
a.setText("Your Choice:"+ e.getActionCommand());
}
}

public static void main(String[] args) {


practical9 a=new practical9();
}
}

class Terminator extends WindowAdapter{


public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}

Output:-

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 30

10. Develop an program that contains three check boxes and 50 x 50 pixel canvas.The three checkboxes
should be labeled “Red”, “Green”,”Blue”. The selection of the check boxes determines the color of
the canvas. For example, if the user selects both “Red” and “Blue”, the canvas should be purple.

import java.awt.*;
import java.awt.event.*;

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 31

/*<applet code="prac10.class" height="300" width="300"></applet>*/

public class prac10 extends java.applet.Applet implements ItemListener {

Checkbox c1=new Checkbox("Red");


Checkbox c2=new Checkbox("Green");
Checkbox c3=new Checkbox("Blue");
Color color;
Canvas c=new Canvas();

public void init() {


add(new Label("Select Color:"));
add(c1);
add(c2);
add(c3);
add(c);
c.setSize(50,50);
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);
}

public void itemStateChanged(ItemEvent e)


{
int r = c1.getState() ? 255 : 0;
int g = c2.getState() ? 255 : 0;
int b = c3.getState() ? 255 : 0;
color = new Color(r, g, b);
c.setBackground(color);

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 32

}
}

Output1:-

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 33

Output2:-

Output3:-

11. Develop a program that draws two sets of ever-decreasing rectangles one in outline form and one
filled alternately in black and white.

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 34

import java.awt.*;
import java.awt.event.*;

//<applet code="prac11.class" width="400" height="400"/></applet>


public class prac11 extends java.applet.Applet {

int x,y,w,h;

public void init() {


x=60;
y=100;
w=200;
h=300;
}

public void paint(Graphics g) {


for(int i=0;i<20;i++)
{
x+=5;
y+=5;
h-=10;
w-=10;
if(i%2==0){
g.setColor(Color.black);
g.fillRect(x,y,h,w);
}else{
g.setColor(Color.white);
g.fillRect(x,y,h,w);
}

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 35

}
showStatus("Priyanshu's applet ");
}
}

Output:-

12. Develop a program to design a Registration Page using different sub classes of Jcomponent class and
display information in pop-up window.

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 36

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class prac12 extends JFrame implements ActionListener {

JTextField name,email;
JPasswordField pass;
JButton save;
public prac12() {

setLayout(new GridLayout(4, 1));


add(new JLabel("Enter Name:"));
name=new JTextField(20);
add(name);

add(new JLabel("Enter Password:"));


pass=new JPasswordField(20);
add(pass);

add(new JLabel("Enter Email:"));


email=new JTextField(20);
add(email);

save=new JButton("Submit");
add(save);
save.addActionListener(this);

setSize(400,400);
setVisible(true);
}

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 37

public void actionPerformed(ActionEvent e){


JOptionPane.showMessageDialog(this,"Name:"+name.getText()+"\nPassword:"+pass.getText()+"\
nEmail:"+email.getText());
}

public static void main(String[] args) {


prac12 a=new prac12();
}
}

Output1:-

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 38

Output2:-

13. 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
import java.awt.*;

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 39

import java.awt.event.*;
//<applet code="Prac13.class" height="300" width="300"></applet>
public class Prac13 extends java.applet.Applet implements MouseMotionListener {

Label l1;
public void init() {
setLayout(null);
l1=new Label();
l1.setBounds(0,0,100,20);
add(l1);
addMouseMotionListener(this);
}

public void mouseMoved(MouseEvent e){


l1.setText("X:"+e.getX()+",Y:"+e.getY());
Graphics g=getGraphics();
g.setColor(Color.black);
g.fillRect(e.getX(),e.getY(),10,10);
}

public void mouseDragged(MouseEvent e){


l1.setText("X:"+e.getX()+",Y:"+e.getY());
}
}

Output1:-

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 40

14. Develop an applet which draw a line between two points 1) mouse pressed and 2) mouse released

import java.awt.*;
import java.awt.event.*;

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 41

public class practical14 extends Frame implements MouseListener{

int x,y;
public practical14() {
addMouseListener(this);

setSize(600,600);
setVisible(true);

public void mousePressed(MouseEvent e){


x=e.getX();
y=e.getY();
}

public void mouseReleased(MouseEvent e){


Graphics g=getGraphics();
g.drawLine(x,y,e.getX(),e.getY());
}

public void mouseClicked(MouseEvent e){


}

public void mouseEntered(MouseEvent e){


}

public void mouseExited(MouseEvent e){


}

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 42

public static void main(String[] args) {


practical14 a=new practical14();
}
}

Output:-

15. Develop an applet which draw a string "Mouse Clicked" at the position of cursor when mouse is click

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 43

/*<applet code="prac15.class" width="300" height="300">


</applet>*/

public class prac15 extends Applet implements MouseListener {


String msg="Mouse Clicked";

public void init()


{
addMouseListener(this);
setSize(300,300);
setVisible(true);
}

public void mouseClicked(MouseEvent e){


Graphics g=getGraphics();
g.setColor(Color.black);
g.drawString(msg,e.getX(),e.getY());
}

public void mousePressed(MouseEvent e){


}

public void mouseReleased(MouseEvent e){


}

public void mouseEntered(MouseEvent e){


}

public void mouseExited(MouseEvent e){


}

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 44

Output:-

16. Develop an applet that uses the mouse listener, which overrides only two methods which are
mousePressed and mouseReleased

import java.awt.*;
import java.awt.event.*;

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 45

public class prac16 extends Frame implements MouseListener {

public prac16() {
addMouseListener(this);
setSize(400,300);
setVisible(true);

public void mousePressed(MouseEvent e){


Graphics g=getGraphics();
g.setColor(Color.black);
g.drawString("Mouse Pressed",e.getX(),e.getY());
}

public void mouseReleased(MouseEvent e){


Graphics g=getGraphics();
g.setColor(Color.black);
g.drawString("Mouse Realeased",e.getX(),(e.getY()+10));
}

public void mouseClicked(MouseEvent e){


}

public void mouseEntered(MouseEvent e){


}

public void mouseExited(MouseEvent e){


}

public static void main(String[] args) {

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 46

prac16 a=new prac16();


}
}

Output:-

17. Develop an applet that contains one button. Initialize the label on the button to "start" and "stop",
when the user presses the button, which changes the label between these two values each time the
button is pressed.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 47

/*<applet code="prac17.class" width="300" height="300">


</applet>*/

public class prac17 extends Applet implements ActionListener {


private Button button;
private String label = "Start";

public void init() {


button = new Button(label);
add(button);
button.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {


if (label.equals("Start")) {
label = "Stop";
} else {
label = "Start";
}
button.setLabel(label);
}
}

Output1:-

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 48

Output2:-

18. 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

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 49

the current color)

import java.awt.*;

import java.awt.event.*;

public class prac18 extends Frame implements ActionListener {

Button b;

public prac18() {

setLayout(new FlowLayout());

b=new Button("Change Color");

add(b);

b.addActionListener(this);

setBackground(Color.red);

b.setSize(60,60);

setSize(400,300);

setVisible(true);

public void actionPerformed(ActionEvent e){

if(getBackground()==Color.red){

setBackground(Color.green);

}else if(getBackground()==Color.green){

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 50

setBackground(Color.blue);

else if(getBackground()==Color.blue){

setBackground(Color.red);

public static void main(String[] args) {

new prac18();

Output1:-

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 51

Output2:-

Output3:-

19. Develop a JDBC application that uses any JDBC driver to perform insert, update, delete and display
all records in/from database. (Assume suitable database)

import java.awt.*;

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 52

import java.sql.*;

public class prc19 extends Frame {


static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3307/data";
static final String USER = "root";
static final String PASS = "1234";

public static void main(String[] args) {


Connection conn = null;
Statement stmt = null;

try {
Class.forName(JDBC_DRIVER);

conn = DriverManager.getConnection(DB_URL, USER, PASS);

stmt = conn.createStatement();
System.out.println();
System.out.println("Creating table student...");
stmt.executeUpdate("create table student5 (rollno int, name varchar(20), sem varchar(20))");
stmt.executeUpdate("insert into student5 (rollno,name,sem)values(1,'priyanshu','six)");
stmt.executeUpdate("insert into student5 (rollno,name,sem)values(2,'sumit',' six')");
stmt.executeUpdate("insert into student5 (rollno,name,sem)values(3,'om',' three')");

System.out.println("Before updating student table");

ResultSet rs = stmt.executeQuery("select * from student5");


System.out.println("-----------------------------");
while (rs.next()) {
String row = (rs.getInt(1) + " " + rs.getString("name") + " " + rs.getString(3));

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 53

System.out.println(row);
}
System.out.println();
System.out.println("----------------");
System.out.println("Updating table...");
stmt.executeUpdate("update student5 set name='priyasnhu', sem='six' where rollno=1");

System.out.println("After updating student table");

System.out.println("-------------");
ResultSet r = stmt.executeQuery("select * from student5");
while (r.next()) {
String row = (r.getInt(1) + " " + r.getString(2) + " " + r.getString(3));

System.out.println(row);
}

} catch (SQLException se) {


se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
       }
    }

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 54

Output1:-

20. Develop a Graphical User Interface that performs the following SQL operations: a) Insert b) Delete c)
Update. (Assume suitable database)

import java.sql.*;

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 55

import javax.swing.JOptionPane;

public class Student_Registration extends javax.swing.JFrame {

Connection connection;

public Student_Registration() {
initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();


jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
jButton5 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("ID");

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 56

jLabel2.setText("Student Name:");

jLabel3.setText("Enrollment No:");

jLabel4.setText("Address");

jButton1.setText("Search");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText("Insert");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton4.setText("Delete");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N


jLabel5.setText("Student Registration");

jButton5.setText("Update");

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 57

jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});

jButton3.setText("Insert Using Para ");


jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(64, 64, 64)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 100,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel1)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addGap(18, 18, 18)

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 58

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField2)
.addComponent(jTextField3)))
.addGroup(layout.createSequentialGroup()
.addGap(67, 67, 67)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 80,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 82,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(29, 29, 29)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 77,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addComponent(jButton1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 277,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(111, 111, 111))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 182,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 59

.addGap(10, 10, 10)


.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 83,
Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jButton4)
.addComponent(jButton5))

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 60

.addGap(16, 16, 16)))


.addComponent(jButton3)
.addContainerGap())
);

getAccessibleContext().setAccessibleName("ID");

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


try {
load();
String Qry = "Select * from temp where s_enroll='" + jTextField2.getText() + "'";

ResultSet rs = null;
Statement stmt = connection.createStatement();
rs = stmt.executeQuery(Qry);
boolean f = false;
while (rs.next()) {
f = true;
jTextField1.setText(rs.getString(2));
jTextField3.setText("" + rs.getString(4));
jLabel1.setText(rs.getString(1));
}

if (!f) {
System.out.println("Record Not Found");
}
} catch (SQLException e) {
System.out.println(e);

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 61

}
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

try {
load();
String Qry = "insert into temp(s_name,s_enroll,addr) values('" + jTextField1.getText() + "','" +
jTextField2.getText() + "','" + jTextField3.getText() + "') ";
Statement stmt = connection.createStatement();
stmt.executeUpdate(Qry);

jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
System.out.println("Record Inserted");
} catch (SQLException e) {
System.out.println(e);
}
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
try {
if (!(jLabel1.getText().equals("ID"))) {
load();
//System.err.println("Connected Database Sucessfully");
String Qry = "delete from temp where id='" + jLabel1.getText() + "' ";

Statement stmt = connection.createStatement();


stmt.executeUpdate(Qry);

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 62

jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jLabel1.setText("ID");
System.out.println("Record Deleted");
} else {
System.out.println("Please Select Proper Id");
}

} catch (SQLException e) {
System.out.println(e);
}
}

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {


try {
if (!(jLabel1.getText().equals("ID"))) {
load();
//System.err.println("Connected Database Sucessfully");
String Qry = "update temp set s_name='" + jTextField1.getText() + "',s_enroll='" +
jTextField2.getText() + "',addr='" + jTextField3.getText() + "' where id='" + jLabel1.getText() + "' ";

Statement stmt = connection.createStatement();


stmt.executeUpdate(Qry);

jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
jLabel1.setText("ID");
System.out.println("Record Updated");

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 63

} else {
System.out.println("Please Select Proper Id");
}

} catch (SQLException e) {
System.out.println(e);
}
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
try {
// System.out.println("op...");
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl = "jdbc:mysql://localhost:3306/data";

connection = DriverManager.getConnection(connectionUrl,"root","");

//System.err.println("Connected Database Sucessfully");


String Qry="insert into temp(s_name,s_enroll,addr) values(?,?,?) ";

PreparedStatement pstmt = connection.prepareStatement(Qry);

pstmt.setString(1,"sneh");
pstmt.setString(2,"14");
pstmt.setString(3,"Anjar");
pstmt.executeUpdate();

String snames[]={"a","b","c","d"};
String senrolls[]={"1","2","3","4"};
String sadrs[]={"a2","b2","c2","d2"};

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 64

for(int i=0;i<snames.length;i++)
{
pstmt.setString(1,snames[i]);
pstmt.setString(2,senrolls[i]);
pstmt.setString(3,sadrs[i]);
}

jTextField1.setText("");
jTextField2.setText("");
jTextField3.setText("");
JOptionPane.showMessageDialog(this, "All Value Inserted");
} catch (ClassNotFoundException | SQLException e) {
System.out.println(e);
} }

public void load() {


try {
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl = "jdbc:mysql://localhost:3306/data";

connection = DriverManager.getConnection(connectionUrl, "root", "");

} catch (ClassNotFoundException | SQLException e) {


System.out.println(e);
}
}

public static void main(String args[]) {


/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 65

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Student_Registration.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Student_Registration.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Student_Registration.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Student_Registration.class.getName()).log(java.util.logging.Le
vel.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Student_Registration().setVisible(true);
}

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 66

});
}

// Variables declaration - do not modify


private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
// End of variables declaration
}

Output1:-

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 67

Output2:-

21. Develop a program to present a set of choice for user to select a product and display the price of
product.

package collage;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 68

public class prac21 extends javax.swing.JFrame {

Connection connection;
public prac21() {
initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();


jLabel2 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N


jLabel1.setText("Practical 21");

jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N


jLabel2.setText("Select Item:");

jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Shirt",


"TShirt", "Jeans", "Shorts" }));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 69

}
});

jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N


jLabel3.setText("The price of the Product is");

jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

jButton1.setText("View Price");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(89, 89, 89)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN
G)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 70

)
.addComponent(jLabel4))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED
)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE,
130, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createSequentialGroup()
.addGap(129, 129, 129)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 96,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(156, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addComponent(jLabel1)
.addGap(42, 42, 42)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(37, 37, 37)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 34,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 52,
Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 71

.addComponent(jLabel4))
.addGap(41, 41, 41))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


try {
load();
String Qry = "Select item_price from item where item_name='" +
jComboBox1.getSelectedItem() + "'";
ResultSet rs = null;
Statement stmt = connection.createStatement();
rs = stmt.executeQuery(Qry);
while (rs.next()) {
jLabel4.setText(rs.getString(1));
}

} catch (SQLException e) {
System.out.println(e);
}
}

private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

public void load() {


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

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 72

String connectionUrl = "jdbc:mysql://localhost:3306/data";

connection = DriverManager.getConnection(connectionUrl, "root", "");

} catch (ClassNotFoundException | SQLException e) {


System.out.println(e);
}
}

public static void main(String args[]) {

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(prac21.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(prac21.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(prac21.class.getName()).log(java.util.logging.Level.SEVE

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 73

RE, null, ex);


} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(prac21.class.getName()).log(java.util.logging.Level.SEVE
RE, null, ex);
}
//</editor-fold>

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new prac21().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton jButton1;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
// End of variables declaration
}

Output:-

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 74

22. Develop a Servlet code to print “Hello”.

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;

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 75

public class prac22 extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet prac22</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World</h1>");
out.println("</body>");
out.println("</html>");
}
}
}

Output:-

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 76

23. Develop a servlet to read values of textboxes and display its values on the page using doGet() and
doPost() methods, from an HTML code (create login form having one submit button, two textboxes
labeled as Login name and Password respectively) when user clicked on submit button.

Prac23.html:

<html>

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 77

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" action="prac23">
Name:<input type="text" name="name"/><br/>
Password:<input type="password" name="upass"/><br/>
<input type="submit" value="Login" name="Sub"/>
</form>
</body>
</html>

OUTPUT 1 :

OUTPUT 2 :

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 78

24. 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.

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;

public class prac24 extends HttpServlet {

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 79

int c=0;

public void init() throws ServletException{


c = Integer.parseInt(getServletConfig().getInitParameter("Count"));
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet prac24</title>");
out.println("</head>");
out.println("<body>");

c++;
out.println("<h1>The Vistor Count is "+c);

out.println("</body>");
out.println("</html>");
}
}
}

WEB.XML:

<servlet>

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 80

<servlet-name>prac24</servlet-name>
<servlet-class>prac24</servlet-class>
<init-param>
<param-name>Count</param-name>
<param-value>0</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>prac24</servlet-name>
<url-pattern>/prac24</url-pattern>
</servlet-mapping>

OUTPUT :

25. Create a web form which processes servlet and demonstrates use of cookies and sessions.
Prac_25.html:
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

<form method="GET" action="prac25">

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 81

Name:<input type="text" name="fname"/><br/>


<input type="submit" value="Send" name="Sub"/>
</form>
</body>
</html>

Prac_25.java:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class prac25 extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet prac25</title>");
out.println("</head>");

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 82

out.println("<body>");

String n=request.getParameter("fname");

HttpSession session=request.getSession();
session.setAttribute("s_name",n);

Cookie cookie=new Cookie("c_name",n);


response.addCookie(cookie);
out.println("<a href=prac25_1>Check Cookie and Session Values</a>");
out.println("</body>");
out.println("</html>");

}
}
}

Prac25_1.java:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 83

public class prac25_1 extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet prac25_1</title>");
out.println("</head>");
out.println("<body>");

HttpSession session=request.getSession(false);
String n=(String)session.getAttribute("s_name");
out.println("<b>Session Value:<b/>"+ n +"<br/>");

Cookie[] c=request.getCookies();
for(int i=0;i<c.length;i++){
String name=c[i].getName();
String value=c[i].getValue();
out.println("<b>Cookie name:</b>"+name+"<b>,Cookievalue:</b>"+value+"<br/>");
}

out.println("</body>");
out.println("</html>");
}
}
}

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 84

OUTPUT 1 :

OUTPUT 2 :

OUTPUT 3:

26. Develop servlet to read records from database and displaying them on page.

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 85

import javax.servlet.http.HttpServletResponse;

public class prac26 extends HttpServlet {

Connection connection;
Statement stmt;
ResultSet rs;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet prac26</title>");
out.println("</head>");
out.println("<body>");
try{
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl = "jdbc:mysql://localhost:3306/data";

connection = DriverManager.getConnection(connectionUrl,"root","");
rs=null;
stmt = connection.createStatement();
rs=stmt.executeQuery("select * from demo");

while(rs.next()){
out.println("<h5>Id:"+rs.getInt(1)+",Name:"+rs.getString(2)+"</h5><br/>");
}

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 86

}catch (ClassNotFoundException | SQLException e) {


out.println(e);
}
out.println("</body>");
out.println("</html>");
}
}
}

Output:-

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 87

27. Develop a JSP to determine how many times the visitors has loaded the page.
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 88

<title>Visitor Counter</title>
</head>
<body>
<h1>Visitor Counter</h1>
<%-- Read the visitor count from the file --%>
<% int count = 0;
String fileName = "visitorCount.txt";
try {
File file = new File(fileName);
if (file.exists()) {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
if (line != null) {
count = Integer.parseInt(line);
}
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
count++;
try {
FileWriter writer = new FileWriter(fileName);
writer.write(String.valueOf(count));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
%>
<p>You are visitor number <%= count %> to this page.</p>
</body>

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 89

</html>

Output:-

28. Develop a JSP application to following operations on database.(Assume suitable database)


1) Insert and Update record into database table
2) Delete and Select records from database table

<%@page contentType="text/html" pageEncoding="UTF-8" %>

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

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 90

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<%

Connection connection;

Statement stmt;

ResultSet rs;

String query;

//it will establish connection

try {

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

String connectionUrl = "jdbc:mysql://localhost:3306/data";

connection = DriverManager.getConnection(connectionUrl,"root","");

rs=null;

stmt = connection.createStatement();

//Insert Code

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 91

// stmt.executeUpdate("insert into demo(id,name) values(1,'ABC')");

//stmt.executeUpdate("insert into demo(id,name) values(8,'Khungla')");

%>

<h3>Record Inserted...</h3>

<%

//Update Code

// stmt.executeUpdate("update demo set name='Daksh' where id=1");

%>

<h3>Record Updated...</h3>

<%

//Delete Code

// stmt.executeUpdate("delete from demo where id=2");

%>

<h3>Record Deleted...</h3>

<%

//showing record

rs=stmt.executeQuery("select * from demo");

while(rs.next()){

%>

<h5>Id: <%= rs.getInt(1) %>,Name:<%= rs.getString(2)%></h5>

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 92

<%

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

%>

<h5>All Record Showed...</h5>

</body>

</html>

Output:-

Priyanshu (206530307032) Prof .HoneyGurnani


Computer Department Tolani F.G Polytechnic ---Advance Java Programing(3360701) 93

Priyanshu (206530307032) Prof .HoneyGurnani

You might also like