Jeniba Poodurai.
A - 234
JDBC Practical - 1
Aim:- JDBC program to insert records to employee table.
“ADD” Button CODE:-
JButton btnAdd = new JButton("Add");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
[Link]("[Link]");
Connection con=
[Link]("jdbc:mysql://localhost:3306/sycs","root","root");
[Link]("Connection Created");
String query="insert into emp values(?,?,?)";
PreparedStatement
ps=[Link](query);
[Link](1,[Link]());
[Link](2,[Link]());
[Link](3,[Link]());
int i=[Link]();
[Link](btnAdd,i+"record(s) inserted successfully");
[Link]();
[Link]();
} catch(Exception ex)
{
[Link](ex);
}
}
});
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
OUTPUT:-
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
JAVA PRACTICAL - 2
Aim:- JDBC program to delete records from employee table.
“Delete” Button CODE:-
JButton deletebtn = new JButton("Delete");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
[Link]("[Link]");
Connection con =
[Link]("jdbc:mysql://localhost:3306/jeni_34","root","root"
);
[Link]("Connection created");
String query="delete from emp where eno=?";
PreparedStatement ps =
[Link](query);
[Link](1, [Link]());
int i=[Link]();
[Link](deletebtn,i+
"record(s) deleted successfully");
[Link]();
[Link]();
}
catch(Exception ex)
{
[Link](ex);
}
}
});
“Reset” Button Code:-
JButton resetbtn = new JButton("Reset");
[Link](new ActionListener() {
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
public void actionPerformed(ActionEvent e) {
[Link]("");
}
});
OUTPUT:-
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
Practical - 3
(Skip) Aim:- JDBC program to perform navigation (first, next, last and
previous) on students table.
CODE:-
Add the packages:-
import [Link];
import [Link];
Create objects:-
public class Jeniba34 extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField t1;
private JTextField t2;
private JTextField t3;
Connection cn;
Statement st;
ResultSet rs;
String query;
public void showdata(ResultSet rs)
{
try
{
[Link]([Link]("eno"));
[Link]([Link]("ename"));
[Link]([Link]("designation"));
}
catch(Exception ex)
{
[Link](ex);
}
}
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
First button code:-
JButton btnFirst = new JButton("First");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
[Link]();
showdata(rs);
}
catch (Exception ex) {
[Link](ex);
}
}
});
Next button code:-
JButton btnNext = new JButton("Next");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
[Link]();
if([Link]())
{
[Link](btnNext,
"You are at the last record");
[Link]();
}
showdata(rs);
}
catch (Exception ex) {
[Link](ex);
}
}
});
Previous button code:-
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
JButton btnPrevious = new JButton("Previous");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
[Link]();
if([Link]())
{
[Link](btnPrevious, "You are at the first record");
[Link]();
}
showdata(rs);
}
catch (Exception ex) {
[Link](ex);
}
}
});
Last button code:-
JButton btnLast = new JButton("Last");
[Link](new Font("Tahoma", [Link], 12));
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
[Link]();
showdata(rs);
}
catch (Exception ex) {
[Link](ex);
}
}
});
[Link](169, 206, 89, 23);
[Link](btnLast);
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
To create Connection:-
try
{
[Link]("[Link]");
Computer Science Department 7
Guru Nanak Khalsa college (Autonomous) 223
con =
[Link]("jdbc:mysql://localhost:3306/sycs","root","ro
ot"); [Link]("Connection created!");
query="select * from employee";
st=[Link](ResultSet.TYPE_SCROLL_INSENSITIVE,[Link]
NCUR_UP DATABLE);
rs=[Link](query);
[Link]();
showdata(rs);
}
catch(Exception ex)
{
[Link](ex);
}
}
Output:-
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
Practical - 4
Aim:- Create a JDBC program to check whether a user is valid or not.
First Create a database Table:
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
Login button code:-
JButton btnlogin = new JButton("Login");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
[Link]("[Link]");
Connection con =
[Link]("jdbc:mysql://localhost:3306/sycs","root","root");
[Link]("COnnection created");
String query = "select * from login where
username=? and password=?";
PreparedStatement ps =
[Link](query);
[Link](1,[Link]());
[Link](2,[Link]());
ResultSet rs = [Link]();
if([Link]())
[Link](btnlogin,"Login Successful!!!");
else
[Link](btnlogin,"Login UnSuccessful!!!");
}
catch (Exception ex) {
[Link](ex);
}
}
});
Reset Button Code:-
JButton btnreset = new JButton("Reset");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link]("");
[Link]("");
} });
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
Practical - 05
A] Aim:- Create a servlet program to check whether a number is even or odd.
Create two file .html and .java
1. html Code :
2. java code:
Output:
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
B] Aim:- Create a servlet program to check username and password with
predefined values. If it matches, display login successfully otherwise redirect
user again to login page.
[Link] Code:
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
[Link] Code:
Output:-
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
(Skip) C] Aim:- Create a Servlet program to develop a Registration form.
[Link] Code:
<title>Registration</title>
</head>
<body bgcolor=cyan>
<center><h1> Registration Form </h1></center>
<form action=MultiServlet method=post>
<pre>
Enter name: <input type =text name=t1>
Enter address: <textarea rows=5 cols=20 name=t2></textarea>
Enter Email: <input type= text name=t3>
Enter Mobile: <input type= text name=t4>
Enter Age: <input type= text name=t5>
Select Degree: <select
name=hob><option>SSC</option><option>HSC</option><option>UG</optio
n><option>PG</option></select>
Select Hobbies: <input type=checkbox value=Cricket name=chk>Cricket
<input type=checkbox value=Music name=chk>Music
<input type=checkbox value=Travelling
name=chk>Travelling
<input type=submit value=Register> <input type=reset>
</pre></form>
[Link] Code:
PrintWriter out = [Link]();
[Link]("text/html");
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
String n=[Link]("t1");
String addr=[Link]("t2");
String email=[Link]("t3");
String mob=[Link]("t4");
String age=[Link]("t5");
String degree=[Link]("hob");
String[] hobbies=[Link]("chk");
String msg="<h2>Thank you for registration! <br> Name:"+n+
"<br>Address:"+addr;
msg+="<br>Email:" +email+"<br> Mobile No:" +mob+"<br>
Age:" +age+"<br> Degree:"+degree;
msg+="<br> Hobbies:";
for(int i=0;i<[Link];i++)
msg+=hobbies[i]+",";
[Link](msg);
Output:-
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
D] Aim: Create a servlet program to insert records to the employee table.
To Create an emp1 table:
Create table emp1(no int(20), name varchar(30), desig varchar(30));
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
[Link] Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Details</title>
</head>
<body>
<form action = insert>
<pre>
Enter No: <input type=text name=t1>
Enter Name: <input type=text name=t2>
Enter Designation: <input type=text name=t3>
<input type = Submit value=insert> <input type=Reset>
</pre>
</form>
</body>
</html>
[Link] Code:
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
try {
PrintWriter out=[Link]();
[Link]("text/html");
String no=[Link]("t1");
String name=[Link]("t2");
String desig=[Link]("t3");
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
[Link]("[Link]");
Connection con=
[Link]("jdbc:mysql://localhost:3306/sycs","root","root
");
String query="insert into emp1 values(?,?,?)";
PreparedStatement ps=[Link](query);
[Link](1,no);
[Link](2,name);
[Link](3,desig);
[Link]();
[Link]("<h1> Record Added Successfully!!");
[Link]("<br><a href=[Link]>Wants to add more
records?</a>");
} catch (Exception ex)
{
[Link](ex);
}
}
Output:-
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
Practical - 6
JSP
A] Create a JSP program to accept a number from the user and
display its multiplication table .
[Link] Code:
<body>
<form action = [Link]>
<pre>
Enter any no: <input type=text name=t1>
<input type=submit> <input type=reset>
</pre>
</form>
</body>
[Link] Code:-
<body>
<%
int n=[Link]([Link]("t1"));
for(int i=1;i<=10;i++)
[Link](n+"*"+i+"="+(n*i)+"<br>");
%>
</body>
Output:-
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
B] Create a JSP program to accept two numbers from the user and
display their Addition.
[Link] Code:-
<body>
<form action=[Link]>
<pre>
Enter the First Value: <input type=text name=t1>
Enter the Second Value: <input type=text name=t2>
<input type=submit value=Check>
<input type=reset value=Reset>
</pre>
</form>
</body>
</html>
[Link] Code:-
<body>
<%
int n=[Link]([Link]("t1"));
int n1=[Link]([Link]("t2"));
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
[Link]("<h1>Addition is:" +(n+n1));
%>
</body>
Output:-
C] Create a JSP program to accept a number from the user and
calculate its factorial.
[Link] Code:-
<body>
<form action = [Link]>
<pre>
<h1>
Enter any no: <input type=text name=t1>
<input type=submit> <input type=reset>
</h1>
</pre>
</form>
</body>
</html>
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
[Link] Code:-
<body>
<%
int n=[Link]([Link]("t1"));
int fact=1;
while(n>1)
{
fact=fact*n;
n--;
}
[Link]("<h1>Factorial is: " +fact);
%>
</body>
Output:-
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
D]Create a JSP program to insert records in the employee table.
[Link] Code:-
<body>
<form action = [Link]>
<pre>
<h1>
Enter no: <input type=text name=t1>
Enter name: <input type=text name=t2>
Enter Designation: <input type=text name=t3>
<input type=submit value=Insert> <input type=reset>
</h1>
</pre>
</form>
</body>
[Link] Code:-
<body>
<%
try
{
String no=[Link]("t1");
String name=[Link]("t2");
String desig=[Link]("t3");
[Link]("[Link]");
Connection con =
[Link]("jdbc:mysql://localhost:3306/sycs","root","ro
ot");
String query="insert into emp values(?,?,?)";
PreparedStatement ps=[Link](query);
[Link](1, no);
[Link](2, name);
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
[Link](3, desig);
[Link]();
[Link]("<h1>Record Added successfully!");
[Link]("<br><a href=[Link]>Wants to add more
records?</a>");
}
catch(Exception ex)
{
[Link](ex);
}
%>
</body>
Output:-
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
Practical - 7
Using Netbeans
Aim: Create a web service to check a number is even or odd.
EvenOddWs Code:-
@WebMethod(operationName = "evenOdd")
public String evenOdd(@WebParam(name = "n") int n) {
if(n %2 == 0) {
return n + " is even" ;
}
else {
return n + "is odd";
}
}
[Link] Code:-
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action = [Link]>
Enter any number: <input type = text name=t1>
<input type =submit> <input type= reset>
</form>
</body>
</html>
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
[Link] Code:-
try {
client.EvenOddWs_Service service = new client.EvenOddWs_Service();
[Link] port = [Link]();
// TODO initialize WS operation arguments here
int n = [Link] ([Link] ("t1"));
// TODO process result here
[Link] result = [Link](n);
[Link]("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}
Output:-
Aim:- Create a web service to get input from the user and display the
addition.
AdditionWs Code:-
@WebMethod(operationName = "add")
public String add(@WebParam(name = "n1") double n1, @WebParam(name =
"n2") double n2) {
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
return "adition is" +(n1+n2); }
[Link]:
try {
client.AdditionWs_Service service = new client.AdditionWs_Service();
[Link] port = [Link]();
// TODO initialize WS operation arguments here
double n1 = [Link]([Link]("n1"));
double n2 = [Link]([Link]("n2"));
// TODO process result here
[Link] result = [Link](n1, n2);
[Link]("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}
[Link] Code:-
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action = [Link]>
Enter 1st number:<input type = text name=n1>
Enter 2nd number: <input type = text name=n2>
<input type =submit> <input type= reset>
</form>
</body>
</html>
Output:-
Guru Nanak Khalsa College SYCS
Jeniba Poodurai. A - 234
Guru Nanak Khalsa College SYCS