Professional Documents
Culture Documents
OF
REGISTRATION NO - 1811341364
MCA SECTION-B
1
2
Index
S. No. Content Page no. Signature
1. Write a program for making 6-7
smiling face using applets.
2. Write a program for passing 8-10
parameter values from HTML
applet tag in applet.
3. Write a program to create a menu 11-12
in frame .
4. Create a GUI page using AWT (on 13-18
frame or applet) with any 6
components(button ,text field ,label
,list ,radio button ,scroll bar ,etc.)
5. Write a program to implement Flow 19
layout in frame.
6. Write a program to implement 20-21
Border layout.
7. Write a program to implement Grid 22-23
layout(3 rows and 2 columns)
8. Write a program to implement 24-26
Card layout.
9. Write a program to implement 27-29
Gridbag layout.
10. Write a program to implement 30-32
ActionListener on a Button.
11. Write a program to implement 31-33
Window Events.
12. Write a program to implement 34-36
ItemListener on Radio button.
13. Write a program to implement 37-38
Mouse Events.
3
14. Write a program to implement Key 39-40
Events.
15. Write a program to implement Text 41
Event.
16. Write a program to implement 42-47
MouseWheel Events.
17. Create a student table with 5 47-49
columns and 5 rows JTable in
JFrame.
18. Create a hierarchal tree structure 50-52
using JTree component on MCA
course(add atleast 20 nodes)
19. Create tabbed pane components 53-54
using JTabbedPane.
20. Write a program to create a hut in 55-57
JApplet.
21. Create a home page using Swing 58-60
components.
22. Create an HTML form for entering 61-62
marks of 4 subjects and Write a
code for finding average using
Javascript.
23. 63-64
Write a code for form field
validation using
Javascript(name,address,ph no.)
24. 65-67
Create alert box using JavaScript.
25. 68-70
Create a confirm box using
JavaScript.
4
26. 71-72
Create prompt box using
JavaScript.
27. 73-74
Write a code to call a function
within another function using
JavaScript.
28. 75-76
Create an XML for a bookstore.
5
36 Write a code Access a database 101-103
from Servlet(or JSP).
37 Write a program to create a 104-108
calculator using swing components.
38 . Write a Program to create Billing 109-114
System
6
Q1. Write a program for making Smiling face using applets.
import java.applet.*;
import java.awt.*;
/*
</applet>
*/
g.drawOval(80,70,150,150);
g.setColor(Color.BLACK);
g.fillOval(120,120,15,15);
g.fillOval(170,120,15,15);
g.drawArc(130,180,50,20,180,180);
7
8
Q2. Write a program for passing parameter values from HTML applet tag in applet.
import java.applet.*;
import java.awt.*;
/*
</applet>
*/
String name;
String age;
String sport;
String food;
String fruit;
String destination;
name = getParameter("Name");
9
age = getParameter("Age");
sport = getParameter("Sport");
food = getParameter("Food");
fruit = getParameter("Fruit");
destination = getParameter("Destination");
g.drawString("Name-" +name,20,40);
g.drawString("Age-" +age,20,60);
10
11
Q3. Write a program to create a menu in frame.
import java.awt.*;
public awtmenu()
setTitle("AWT menu");
setSize(400,400);
setVisible(true);
file.add(open);
file.add(save);
file.addSeparator();
sb.add(one);
sb.add(two);
file.add(saveas);
12
file.add(sb);
mb.add(file);
setMenuBar(mb);
new awtmenu();
13
Q4. Create a GUI page using AWT(on frame or applet) with any 6
components(button,text field,label,list,radio button,scroll bar etc).
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Label lb1,lb2,lb3;
TextField txt1,txt2,txt3;
Button btn1,btn2,btn3,btn4,btn5,btn6,btn7;
public Calculator()
14
btn5 = new Button("Mod");
add(lb1);
add(txt1);
add(lb2);
add(txt2);
add(lb3);
add(txt3);
add(btn1);
add(btn2);
add(btn3);
add(btn4);
add(btn5);
add(btn6);
add(btn7);
setSize(200,200);
setTitle("calculator");
setLayout(new FlowLayout());
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
15
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
double a=0,b=0,c=0;
try
a = Double.parseDouble(txt1.getText());
catch(NumberFormatException e)
txt1.setText("Invalid input");
try
b = Double.parseDouble(txt2.getText());
catch(NumberFormatException e)
txt2.setText("Invalid input");
16
if(ae.getSource()==btn1)
c=a+b;
txt3.setText(String.valueOf(c));
if(ae.getSource()==btn2)
c=a-b;
txt3.setText(String.valueOf(c));
if(ae.getSource()==btn3)
c=a*b;
txt3.setText(String.valueOf(c));
if(ae.getSource()==btn4)
c=a/b;
txt3.setText(String.valueOf(c));
17
if(ae.getSource()==btn5)
c=a%b;
txt3.setText(String.valueOf(c));
if(ae.getSource()==btn6)
txt1.setText("0");
txt2.setText("0");
txt3.setText("0");
if(ae.getSource()==btn7)
System.exit(0);
calC.setVisible(true);
calC.setLocation(300,300);
18
19
Q5. Write a program to implement flow layout in frame.
import java.awt.*;
f.setLayout(ff);
f.add(b);
f.add(tf);
f.setSize(300,300);
f.setVisible(true);
20
Q6. Write a program to implement border layout in applet.
import java.awt.*;
public BorderLayout1()
setTitle("My Frame");
setSize(300,400);
add(northButton,BorderLayout.NORTH);
add(southButton,BorderLayout.SOUTH);
add(eastButton,BorderLayout.EAST);
add(westButton,BorderLayout.WEST);
add(centerButton,BorderLayout.CENTER);
setVisible(true);
21
}
22
Q7. Write a program to implement Grid layout (3 rows 2 columns).
import java.awt.*;
public layoutgrid()
setTitle("GridLayout");
setSize(200,220);
setLayout(g);
add(button1);
add(button2);
add(button3);
add(button4);
add(button5);
add(button6);
add(button7);
23
setVisible(true);
24
Q8. Write a program to implement card layout
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
CardLayout card;
JButton b1,b2,b3;
Container c;
CardLayoutExample()
c=getContentPane();
card=new CardLayout(40,30);
c.setLayout(card);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c.add("a",b1);
c.add("b",b2);
c.add("c",b3);
25
public void actionPerformed(ActionEvent e)
card.next(c);
cl.setSize(400,400);
cl.setVisible(true);
cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
26
27
Q9. Write a program to implement gridbag layout.
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.*;
GridbagDemo()
setTitle("GridBagLayoutDemo");
p.setLayout(new GridBagLayout());
c.gridx = 0;
c.gridy = 0;
c.ipadx = 15;
c.ipady = 50;
c.gridx = 1;
c.ipadx = 90;
c.ipady = 40;
28
c.gridx = 0;
c.gridy = 1;
c.ipadx = 20;
c.ipady = 20;
c.ipadx = 10;
c.gridx = 1;
System.exit(0);
};
addWindowListener(wndCloser);
getContentPane().add(p);
setSize(600, 400);
setVisible(true);
new GridbagDemo();
29
}
30
Q10. Write a program to implement ActionListener on a button.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public SwingListenerDemo()
prepareGUI();
swingListenerDemo.showActionListenerDemo();
mainFrame.setSize(400,400);
31
headerLabel = new JLabel("",JLabel.CENTER );
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter()
System.exit(0);
});
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
32
JPanel panel = new JPanel();
panel.setBackground(Color.magenta);
okButton.addActionListener(new CustomActionListener());
panel.add(okButton);
controlPanel.add(panel);
mainFrame.setVisible(true);
33
34
Q11. Write a program to implement Window Events.
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
WindowExample()
addWindowListener(this);
setLayout (null);
setVisible (true);
new WindowExample();
System.out.println("activated");
35
{
System.out.println("closed");
System.out.println("closing");
dispose();
System.out.println("deactivated");
System.out.println("deiconified");
System.out.println("iconified");
36
public void windowOpened(WindowEvent arg0)
System.out.println("opened");
} }
37
Q12. Write a program to implement ItemListener on Radio button.
<html>
<body>
</body>
<script>
function checkButton() {
if(document.getElementById('summer').checked) {
document.getElementById("disp").innerHTML
= document.getElementById("summer").value
else if(document.getElementById('winter').checked) {
document.getElementById("disp").innerHTML
= document.getElementById("winter").value
38
}
else if(document.getElementById('rainy').checked) {
document.getElementById("disp").innerHTML
= document.getElementById("rainy").value
else if(document.getElementById('autumn').checked) {
document.getElementById("disp").innerHTML
= document.getElementById("autumn").value
else {
document.getElementById("error").innerHTML
</script>
</html>
39
Q14. Write a program to implement Mouse Events.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<button id="btn">Click me with any mouse button: left, right, middle, ...</button>
<p id="message"></p>
<script>
e.preventDefault();
});
switch (e.button) {
case 0:
break;
40
case 1:
break;
case 2:
break;
default:
});
</script>
</body>
</html>
41
Q15. Write a program to implement Key Events.
<!DOCTYPE html>
<html>
<body>
<p>Whenever a character key other than left arrow key or end key or home key is typed
inside the text field, an alert is displayed.</p>
<script>
function Function() {
alert("A character key is typed in the text field and prompting this alert to be displayed");
</script>
</body>
</html>
42
Q16. Write a program to Mouse Wheel Events.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
JTextArea textArea;
JScrollPane scrollPane;
public MouseWheelEventDemo() {
super(new BorderLayout());
43
textArea.setEditable(false);
scrollPane
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(scrollPane, BorderLayout.CENTER);
textArea.addMouseWheelListener(this);
String message;
if (notches < 0) {
+ newline;
} else {
+ newline;
44
if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
+ scrollPane.getVerticalScrollBar().getUnitIncrement(1)
+ scrollPane.getVerticalScrollBar().getBlockIncrement(1)
saySomething(message, e);
+ eventDescription);
textArea.setCaretPosition(textArea.getDocument().getLength());
45
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(newContentPane);
frame.pack();
frame.setVisible(true);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
createAndShowGUI();
});
46
47
Q17. Create a student table with 5 columns and 5 rows JTable in JFrame .
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
// frame
JFrame f;
// Table
JTable j;
// Constructor
JTableExamples()
// Frame initialization
f = new JFrame();
// Frame Title
f.setTitle("JTable Example");
String[][] data = {
48
{ "Rajesh Ahuja", "4321", "CS" },
};
// Column Names
// adding it to JScrollPane
f.add(sp);
// Frame Size
f.setSize(500, 200);
f.setVisible(true);
// Driver method
49
new JTableExamples();
50
Q18. Create a hierarchal strurcture using JTree component on MCA course(add atleast
20 nodes).
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
JFrame f;
Tree(){
f=new JFrame();
country.add(mca);
51
mca.add(ad); mca.add(os); mca.add(ab); mca.add(cs); mca.add(lx); mca.add(cc);
mca.add(db); mca.add(cg); mca.add(jv); mca.add(ds); mca.add(php); mca.add(cn);
mca.add(wd); mca.add(as);
f.add(jt);
f.setSize(500,500);
f.setVisible(true);
new Tree();
52
53
Q19. Create a Tabbed Pane component using JTabbedPane.
import javax.swing.*;
JFrame f;
TabbedPaneExample(){
f=new JFrame();
p1.add(ta);
tp.setBounds(50,50,200,200);
tp.add("main",p1);
tp.add("visit",p2);
tp.add("help",p3);
f.add(tp);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
new TabbedPaneExample();
}}
54
55
Q20. Create a program to create a hut in Applet.
import java.awt.*;
/*
</applet>
*/
g.drawLine(100,400,300,400);
g.drawLine(100,400,70,450);
g.drawLine(300,400,350,450);
g.drawLine(300,400,250,450);
g.drawLine(70,450,350,450);
g.drawLine(70,450,70,600);
g.drawLine(250,450,250,600);
g.drawLine(350,450,350,600);
g.drawLine(70,600,350,600);
g.drawRect(270,600,90,110);
setBackground(Color.black);
setForeground(Color.white);
g.drawOval(700,10,75,100);
56
g.fillOval(700,10,75,100);
g.drawOval(500,350,5,10);
g.fillOval(500,350,5,10);
g.drawOval(400,10,5,10);
g.fillOval(400,10,5,10);
g.drawOval(420,10,5,10);
g.fillOval(420,10,5,10);
g.drawOval(380,10,5,10);
g.fillOval(380,10,5,10);
g.drawOval(350,10,2,10);
g.fillOval(350,10,2,10);
g.drawOval(320,10,3,10);
g.fillOval(320,10,3,10);
g.drawOval(200,10,5,10);
g.fillOval(200,10,5,10);
g.drawOval(190,10,4,10);
g.fillOval(190,10,4,10);
g.drawOval(180,10,5,10);
g.fillOval(180,10,5,10);
g.drawOval(160,10,1,10);
g.fillOval(160,10,1,10);
g.drawOval(110,10,2,10);
g.fillOval(110,10,2,10);
g.drawOval(100,10,3,10);
57
g.fillOval(100,10,3,10);
g.drawOval(80,10,5,10);
g.fillOval(800,10,5,10);
g.drawOval(60,10,5,10);
g.fillOval(60,10,5,10);
58
Q21. Create a home page using Swing Components.
import javax.swing.*;
import java.awt.*;
class gui {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
mb.add(m1);
mb.add(m2);
m1.add(m11);
m1.add(m22);
59
panel.add(label);
panel.add(tf);
panel.add(send);
panel.add(reset);
frame.getContentPane().add(BorderLayout.SOUTH, panel);
frame.getContentPane().add(BorderLayout.NORTH, mb);
frame.getContentPane().add(BorderLayout.CENTER, ta);
frame.setVisible(true);
60
61
Q22. Create a HTML form for entering marks of 4 subjects and write a code for finding
average using Javascript.
<html>
<head>
<script type="text/javascript">
function total()
const s1=parseInt(form1.OS.value)
const s2=parseInt(form1.java.value)
const s3=parseInt(form1.adbms.value)
const s4=parseInt(form1.cloud.value)
const s5=parseInt(form1.web.value)
function calculate()
const total=s1+s2+s3+s4+s5
result=total/5
form1.answer.value=result+"%"
result=calculate()
</script></head>
<body>
<form name="form1">
62
Enter marks in Java :
Result
Your result:
</form>
</body>
</html>
63
Q23. Write a code for form field validations using Javascript(name,address,phn no).
<html>
<head>
function validate()
if(document.form1.yourname.value.length<2)
if(document.form1.address.value.length<3)
if(document.form1.mobileno.value.length!==10)
</script>
</head>
<body>
<form name="form1">
64
Address:<input type ="text" name="address">
onclick="validate()">
</form>
</body>
</html>
65
Q24. Create alert box using Javascript.
<!DOCTYPE html>
<html>
<head>
<title>
</title>
<style>
h1 {
color: Blue;
h2 {
font-family: Impact;
body {
text-align: center;
</style>
</head>
<body>
<h1>Welcome to DAVIM</h1>
<h2>Alert in JavaScript</h2>
66
<p>
</p>
<button ondblclick="myalert()">
</button>
<script>
function myalert() {
</script>
</body>
</html>
67
68
Q25. Create confirm box using Javascript.
<!DOCTYPE html>
<html>
<title>Confirm Box</title>
<!--CSS Styles-->
<style>
<p>
font-size: 28px;
color: green;
text-align: justify;
<h1>
color: blue;
text-align:center;
</style>
<!--JavaScript Logic-->
<script>
69
{
document.write("<h2 style='color:red'>Sorry! you can think about this why don't you take
courses in DAVIM!");//if we press Cancel then it will display this message
getYourDecision();
</script>
<body>
<div>
<p>Real time Example: Let suppose we have online application form for payment method.
After filling online payment application form, if you want to proceed with payment then it
will display pop up box. If you want to move further we have to click Ok button, if we click
Cancel button then payment stop there will itself and come back to home page.</p>
</div>
</body>
</html>
70
71
Q26. Create a prompt box using Javascript.
<!DOCTYPE html>
<html>
<head>
<title>
</title>
<script>
function fun() {
if (a != null) {
</script>
</head>
Hello World
</h1>
<h2>
72
</h2>
Click me
</button>
<p id = "para"></p>
</body>
</html>
73
Q27. Write a code to call afunction within another function using Javascript.
<html>
<head>
<script type="text/javascript">
function product()
const s1=parseInt(form1.num1.value)
const s2=parseInt(form1.num2.value)
const s3=parseInt(form1.num3.value)
function calculate()
const product1=s1*s2*s3
form1.answer.value=product1
result=calculate()
</script></head>
<body>
<form name="form1">
74
<input type="text" name="num3" ><br><br>
Result
Your result:
</form>
</body>
</html>
75
Q28. Create an XML for a bookstore.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
lastName, fullName).</p>
<p id="demo"></p>
<script>
const myObject = {
firstName:"AYUSHI",
document.getElementById("demo").innerHTML = myObject.fullName();
</script>
</body>
</html>
76
77
Q29. Connect Java application to a database (oracle or Mysql) and
perform the following queries
78
Retrieve Data from a table(executeQuery).
import java.sql.*
public class Example
{
public static void main(String args[])
{
Connection conn = null;
try{
System.out.println("This Program is done by Nikita");
Class.forName("com.mysql.jdbc.Driver");
System.out.println("connecting to database....");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sample","root","tige
r");
//here librarydb is the database name, root is the username and tiger is the password
System.out.println("Database Connected....");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from college");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2));
con.close();
System.out.println("Database Closed....");
79
}catch(Exception e)
{
System.out.println(e);
}
}
}
import java.sql.*;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("connecting to database....");
80
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sample","root","tige
r");
//here librarydb is the database name, root is the username and tiger is the password
System.out.println("Database Connected....");
Statement stmt=con.createStatement();
con.close();
System.out.println("Database Closed....");
}catch(Exception e)
System.out.println(e);
} }
81
Q30. Write a program to insert values to a table using batch query.
import java.sql.*;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("connecting to database....");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sample","root","tiger");
//here librarydb is the database name, root is the username and tiger is the password
System.out.println("Database Connected....");
Statement stmt=con.createStatement();
stmt.addBatch(sql);
stmt.addBatch(sql1);
stmt.addBatch(sql2);
82
stmt.addBatch(sql3);
stmt.addBatch(sql4);
stmt.executeBatch();
con.close();
System.out.println("Database Closed....");
}catch(Exception e)
System.out.println(e);
}}
83
84
Q31. Write a java application to show the use of parameterized query.
import java.sql.*;
public Input() {
initComponents();
@SuppressWarnings("unchecked")
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Name");
jLabel2.setText("Age");
jButton1.setText("Record");
jButton1.addActionListener(new java.awt.event.ActionListener() {
85
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
});
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG, false)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG)
.addComponent(jButton1)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADI
NG, false)
.addComponent(name)
.addContainerGap(56, Short.MAX_VALUE))
86
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(age, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jButton1)
.addContainerGap(19, Short.MAX_VALUE))
);
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
87
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(29, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(31, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
);
setLocationRelativeTo(null);
}// </editor-fold>//GEN-END:initComponents
try{
Class.forName("com.mysql.jdbc.Driver");
88
System.out.println("connecting to database....");
//here librarydb is the database name, root is the username and tiger is the password
//here librarydb is the database name, root is the username and tiger is the password
System.out.println("Database Connected....");
stmt.setString(1,name.getText());
stmt.setString(2,age.getText());
stmt.executeUpdate();
System.out.println("Database Closed....");
catch(ClassNotFoundException | SQLException e)
System.out.println(e);
}//GEN-LAST:event_jButton1ActionPerformed
new Input().setVisible(true);
89
private javax.swing.JLabel jLabel2;
90
Q32. Implement form processing using servlets.
Index.html
<html>
<head>
<title>Register Form</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" action="Register">
Name:<input type="text" name="Name"/><br/>
Email ID:<input type="text" name="Email"/><br/>
Password:<input type="text" name="Password"/><br/>
<input type="submit" value="Register"/>
</form>
</body>
</html>
Register.html
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Register extends HttpServlet
{
@Override
@SuppressWarnings("UseSpecificCatch")
91
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
String name=request.getParameter("Name");
String email=request.getParameter("Email");
String pass=request.getParameter("Password");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sample","root","tiger");
PreparedStatement pst=con.prepareStatement("insert into pro2 values(?,?,?)");
pst.setString(1,name);
pst.setString(2,email);
pst.setString(3,pass);
int i=pst.executeUpdate();
if(i>0)
{
out.println("You are Successfully Registered");
}
else
{
out.println("Got problem in Registering the data in database");
}
}
catch(Exception se)
{
System.out.println("Error"+se);
}
}}
92
93
Q33. Create and fetch cookies using Servlet.
Index.html
<html>
<head>
<title>
</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="firstservlet" method="post">
Name:<input type="text" name="Username"/><br/>
<input type="submit" value="GO"/>
</form>
</body>
</html>
firstservlet.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;
public class firstservlet extends HttpServlet {
@Override
@SuppressWarnings("ConvertToTryWithResources")
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String n=request.getParameter("Username");
94
out.println("welcome"+n);
Cookie ck=new Cookie("Username",n);
response.addCookie(ck);
out.println("<form action='secondservlet'>");
out.println("<input type='submit' value='go'>");
out.println("</form>");
out.close();
}
catch(Exception e)
{
System.out.print(e);
}
}}
secondservlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet(urlPatterns = {"/secondservlet"})
public class secondservlet extends HttpServlet {
@Override
@SuppressWarnings("ConvertToTryWithResources")
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie ck[] = request.getCookies();
out.println("Hello "+ ck[0].getValue());
out.close();
}
catch(Exception e)
{
95
System.out.println(e);
}
}}
OUTPUT:-
96
Q34. Write a JSP code showing the use of scriptlet, expression, declaration
and comments.
Index.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 action="sum.jsp" method="post">
Enter Your Name<input type="text" name="Name"/><br/>
<input type="submit" value="Welcome"/>
</form>
</body>
</html>
Sum.Jsp
<%--
Document : sum
Created on : Apr 3, 2017, 12:06:00 AM
Author : Nikita
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%!
int Sum(int n,int m)
97
{
return(n+m);
}
%>
<%
String n=request.getParameter("Name");
out.println("Welcome"+n);
out.println(Sum(20,30));
%>
<%= 30+50 %>
</body>
</html>
OUTPUT:-
98
Q35.Write a program to illustrate sharing of data between jsp pages(action
tags).
Index.html
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
</form>
</body>
</html>
Exam.jsp
<!DOCTYPE html>
<html>
<head>
<title>Exam Page</title>
</head>
99
<body>
<%
out.println("Stary Studing");
%>
</body>
</html>
MCA.jsp
100
OUTPUT:-
101
Q36. Write a code Access a database from Servlet(or JSP).
Index.html
<html>
<head>
<title>Register Form</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" action="Register">
Name:<input type="text" name="Name"/><br/>
Email ID:<input type="text" name="Email"/><br/>
Password:<input type="text" name="Password"/><br/>
<input type="submit" value="Register"/>
</form>
</body>
</html>
Register.html
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Register extends HttpServlet
{
@Override
@SuppressWarnings("UseSpecificCatch")
protected void doPost(HttpServletRequest request, HttpServletResponse response)
102
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
String name=request.getParameter("Name");
String email=request.getParameter("Email");
String pass=request.getParameter("Password");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sample","root","tiger");
PreparedStatement pst=con.prepareStatement("insert into pro2 values(?,?,?)");
pst.setString(1,name);
pst.setString(2,email);
pst.setString(3,pass);
int i=pst.executeUpdate();
if(i>0)
{
out.println("You are Successfully Registered");
}
else
{
out.println("Got problem in Registering the data in database");
}
}
catch(Exception se)
{
System.out.println("Error"+se);
}
}}
103
OUTPUT:-
104
Q37. Write a program to create a calculator using swing components
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
Calculator(){}
createWindow();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createUI(frame);
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
105
private static void createUI(JFrame frame) {
panel.setLayout(layout);
inputBox.setEditable(false);
button1.addActionListener(calculator);button2.addActionListener(calculator);
button3.addActionListener(calculator);button4.addActionListener(calculator);
button5.addActionListener(calculator);button6.addActionListener(calculator);
button7.addActionListener(calculator);button8.addActionListener(calculator);
button9.addActionListener(calculator);button0.addActionListener(calculator);
buttonPlus.addActionListener(calculator);buttonMinus.addActionListener(calculator);
buttonDivide.addActionListener(calculator);buttonMultiply.addActionListener(calculator);
buttonClear.addActionListener(calculator);buttonDot.addActionListener(calculator);
106
buttonEquals.addActionListener(calculator);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 3;
frame.getContentPane().add(panel, BorderLayout.CENTER);
107
String command = e.getActionCommand();
if (command.charAt(0) == 'C') {
inputBox.setText("");
inputBox.setText(evaluate(inputBox.getText()));
}else {
inputBox.setText(inputBox.getText() + command);
double result = 0;
if(operator.isEmpty()){
operand1 += arr[i];
}else{
operand2 += arr[i];
operator += arr[i];
108
}
if (operator.equals("+"))
else if (operator.equals("-"))
else if (operator.equals("/"))
else
OUTPUT:-
109
Q38. Write a Program to create Billing System.
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
//itvoyagers.in
HashMap<String, String>product_and_price;
DefaultListModel<String>product_name_bill, product_quantity_bill,product_price_bill;
JComboBoxproduct_list, quantity_list;
JButtonadd_product;
public MyStationery()
product_and_price.put("Book", "80");
product_and_price.put("Pen", "10");
product_and_price.put("Pencil", "7");
product_and_price.put("Marker", "40");
product_and_price.put("Eraser", "5");
//itvoyagers.in
110
product_price_bill = new DefaultListModel<>();
product_and_price.forEach((key,value)->
product_list.addItem(key);
});
quantity_list.addItem(Integer.toString(i));
//itvoyagers.in
111
add_product = new JButton("Add Product");
//itvoyagers.in
add(product_name);
add(price_per_unit);
add(quantity_label);
add(price_per_unit_label);
add(product_list);
112
add(quantity_list);
add(add_product);
//itvoyagers.in
add(bill_product);
add(bill_quantity);
add(bill_price);
add(product_bill);
add(product_quantity);
add(product_price);
add(total);
add(total_amount);
product_list.addActionListener(this);
add_product.addActionListener(this);
setSize(435,650);
setLayout(null);
setVisible(true);
setTitle("My Stationery");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//itvoyagers.in
if(e.getSource()==add_product)
113
product_name_bill.addElement((String) product_list.getSelectedItem());
product_quantity_bill.addElement((String) quantity_list.getSelectedItem());
int unit_price =
Integer.parseInt(product_and_price.get(product_list.getSelectedItem()));
int total_cost = 0;
product_price_bill.addElement(Integer.toString(total_for_product));
for(int i=0;i<product_price_bill.getSize();i++)
total_cost += Integer.parseInt(product_price_bill.getElementAt(i));
total_amount.setText(Integer.toString(total_cost));
else if(e.getSource()==product_list)
price_per_unit_label.setText(product_and_price.get(product_list.getSelectedItem()));
//itvoyagers.in
114
}
OUTPUT:-
115
116