0% found this document useful (0 votes)
2 views

292 java

The document outlines a series of practical exercises related to JDBC programming and web development, including adding and deleting records from an employee table, navigating through a student table, and validating user credentials. It provides code snippets for various functionalities such as adding, deleting, and checking user validity, along with servlet examples for handling user input. Additionally, it includes HTML forms for user interaction and demonstrates the use of JSP for displaying multiplication tables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

292 java

The document outlines a series of practical exercises related to JDBC programming and web development, including adding and deleting records from an employee table, navigating through a student table, and validating user credentials. It provides code snippets for various functionalities such as adding, deleting, and checking user validity, along with servlet examples for handling user input. Additionally, it includes HTML forms for user interaction and demonstrates the use of JSP for displaying multiplication tables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

G.

N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

PRACTICAL 1

AIM: JDBC program to add records to employee table

Table(eno,ename,edesignation) Eclipse IDE “ADD” BUTTON code

RESET BUTTON:

1
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

OUTPUT:

MySql:

2
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

PRACTICAL 2

AIM: JDBC program to delete records from employee table


ECLIPSE DELETE BUTTON CODE

Reset Button Code:-


JButton resetbtn = new JButton("Reset");
resetbtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {


t1.setText("");
}
});

MY SQL:-

3
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

OUTPUT

4
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

PRACTICAL 3
Aim: JDBC program to perform navigation(first,next,last
& previous) on students table.

package pract3;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import
javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.awt.event.ActionEvent;
public class navigation extends JFrame {

private static final long serialVersionUID = 1L;


private JPanel contentPane;
private JTextField t1;
private JTextField t2;
private JTextField t3;
Connection con;
Statement st;
ResultSet rs;
String query;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable()
{ public void run() {
try {

5
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

navigation frame = new navigation();


frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public void showdata(ResultSet rs) {
try
{
t1.setText(rs.getString("eno"));
t2.setText(rs.getString("ename"));
t3.setText(rs.getString("designation"));
}
catch(Exception ex)
{
System.out.println(ex);
}
}
public navigation() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLO
SE); setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setForeground(new Color(0, 0, 0));
contentPane.setBackground(new Color(255, 255,
255)); contentPane.setBorder(new EmptyBorder(5, 5,
5, 5));

setContentPane(contentPane);
contentPane.setLayout(null);

JLabel lblNewLabel = new JLabel("eno");


lblNewLabel.setForeground(new Color(0, 0, 0));
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD,
14)); lblNewLabel.setBounds(41, 34, 45, 13);
contentPane.add(lblNewLabel);

JLabel lblNewLabel_1 = new JLabel("ename");


lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD,
14)); lblNewLabel_1.setForeground(new Color(0, 0, 0));
lblNewLabel_1.setBounds(41, 85, 72, 13);
contentPane.add(lblNewLabel_1);

6
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

JLabel lblNewLabel_2 = new JLabel("edesignation ");


lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD,
14)); lblNewLabel_2.setForeground(new Color(0, 0, 0));
lblNewLabel_2.setBounds(41, 141, 109, 13);
contentPane.add(lblNewLabel_2);

t1 = new JTextField();
t1.setBounds(220, 31, 96, 19);
contentPane.add(t1);
t1.setColumns(10);

t2 = new JTextField();
t2.setBounds(220, 82, 96, 19);
contentPane.add(t2);
t2.setColumns(10);

t3 = new JTextField();
t3.setBounds(220, 138, 96, 19);
contentPane.add(t3);
t3.setColumns(10);

JButton btnNewButton = new JButton("FIRST");


btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { try {
rs.first();
showdata(rs);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
});
btnNewButton.setBounds(10, 202, 85, 21);
contentPane.add(btnNewButton);

JButton btnNewButton_1 = new JButton("LAST");


btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { try {
rs.first();
showdata(rs);
}
catch(Exception ex)
{
System.out.println(ex);
}
}

7
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

});
btnNewButton_1.setBounds(118, 202, 85, 21);
contentPane.add(btnNewButton_1);

JButton btnNewButton_2 = new JButton("NEXT");


btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { try
{
rs.next();
if(rs.isAfterLast())
{
JOptionPane.showMessageDialog(btnNewButton_2,"You are at the last");
rs.first();
showdata(rs);
}else {
showdata(rs);}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
});
btnNewButton_2.setBounds(231, 202, 85, 21);
contentPane.add(btnNewButton_2);

JButton btnNewButton_3 = new JButton("PREVIOUS");


btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try
{
rs.previous();
if(rs.isBeforeFirst())
{
JOptionPane.showMessageDialog(btnNewButton_2,"You are at the first");
rs.last();
showdata(rs);
}else {
showdata(rs);}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
});
btnNewButton_3.setBounds(341, 202, 85, 21);
contentPane.add(btnNewButton_3);

8
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/employee","root","tiger" );
System.out.println("Connection created");
query="select * from emp";
st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UP
DATABLE); rs=st.executeQuery(query);
rs.first();
showdata(rs);
}
catch(Exception ex) {
System.out.println(ex);
}
}
}

OUTPUT:

9
G.N KHALSA COLLEGE (AUTONOMOUS) B-292
Siddiqui Sara

10
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

PRACTICAL 4

AIM: JDBC program to check whether a user is valid or not.


ECLIPSE ADD BUTTON:

RESET BUTTON:

OUTPUT:

11
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

MySql:

12
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

Practical 5

5.a. Create a servlet program to check whether a user is


valid or not.

1.evenodd.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Even Odd</title>
</head>
<body>
<form action=Servlet1>
<pre>
Enter any number:<input type=text name=t1>
<input type=submit> <input type=reset>
</pre>
</form>
</body>
</html>

2.Servlet1.java
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
response.setContentType("text/html");
int num=Integer.parseInt(request.getParameter("t1"));
if(num%2==0) {
out.println("<h1>"+num+" Number is Even");
}
else {
out.println("<h1>"+num+" Number is Odd");
}

13
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

14
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

5.b. Create a servlet program to check username and


password with predefined values. If it matches, display
login successful otherwise redirect user again to login
page.

2.Login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action=Servlet2>
<pre>
Enter Username:
<input type="text" name="t1">
Enter Password:
<input type="password" name="t2">
<br>
<input type="submit"><input type="reset">
</pre>
</form>
</body>
</html>

15
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

Servlet2.java
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
response.setContentType("text/html");
String s = request.getParameter("t1");
String s2 = request.getParameter("t2");

if(s2.equals("Password") && s.equals("Users2005")) {


out.println("<h1>" + "Welcome"+ t1);
}else {
out.println("<h1>"+"Denied permission check username of
password");
out.println("<br><a href=Login.html>Back Page!!</a>");
}
}

​ ​

16
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

Practical 6

File— Dynamic web project —multivale—6.0— finish


Window–show view–Project explore— select file —src–main–webApp–new html
file–input.html

A] Create a JSP program to accept a number


from the user and display its multiplication
table .

input.html

1.<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Registration </title>
</head>
<body bgcolor=cyan>
<center><h1>Registration form</h1></center>
<form action=MultivaluedServlet>
<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>

Select Degree: <select


name=hob><option>SSC</option><option>HSC</option>

<option>UG</option><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=Travellling

17
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

name=chk>Travelling
<input type=submit value=Register> <input type=reset>
</pre>
</form>
</body>
</html>

2.multiservlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
response.setContentType("text/html");
String n=request.getParameter("t1");
String addr=request.getParameter("t2");
String email=request.getParameter("t3");
String mob=request.getParameter("t4");
String age=request.getParameter("t5");
String degree=request.getParameter("hob");
String[] hobbies=request.getParameterValues("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<hobbies.length;i++)
msg+=hobbies[i]+",";
out.println(msg);

18
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

2.input.html
<!DOCTYPE html>
<html>

19
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action=InsertServlet></form>
<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>

2.protected void doGet(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException {
try
{

PrintWriter out=response.getWriter();
response.setContentType("text/html");

String no=request.getParameter("t1");
String name=request.getParameter("t2");
String desig=request.getParameter("t3");

Class.forName("com.mysql.cj.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sycs","root","ro
ot");
String query="insert into emp values(?,?,?)";
PreparedStatement ps=con.prepareStatement(query);
ps.setString(1,no);
ps.setString(2,name);
ps.setString(3, desig);
ps.executeUpdate();
out.println("<h1>Record Added Successfully!");
out.println("<br><a href=input.html>Wants to add more

20
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

records?</a>");
}
catch(Exception ex)
{
System.out.println(ex);
}
}

21
G.N KHALSA COLLEGE (AUTONOMOUS) B-292
Siddiqui Sara

22
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

Practical no 7
Aim:- Create a web service to check a number is

even or odd

1.input.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action=output.jsp>
<pre>
Enter any no: <input type=text name=t1>
<input type=submit> <input type=reset>

</pre>
</form>
</body>
</html>

Output.jsp
<%@ page language="java" contentType="text/html;
charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
int n=Integer.parseInt(request.getParameter("t1"));
for(int i=1; i<=10; i++)
out.println(n+"*"+i+"="+(n*i)+"</br>");
%>
</body>
</html>
Output

23
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

2.
input.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action=Output.jsp>
<pre>
Enter First No: <input type=text name=t1>
Enter Second No: <input type=text name=t2>

<input type=submit> <input type=reset>

</pre>
</form>
</body>
</html>

Output.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
24
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
int n1=Integer.parseInt(request.getParameter("t1"));
int n2=Integer.parseInt(request.getParameter("t2"));
out.println("<h1>Addition is "+(n1+n2));
%>
</body>
</html>

3.input.jsp
<%@ page language="java" contentType="text/html;
charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action=output.jsp>
<pre>
Enter Any No: <input type=text name=t1>

<input type=submit> <input type=reset>


</pre>
</body>
</html>

25
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

Output.jsp
<%@ page language="java" contentType="text/html;
charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
int n=Integer.parseInt(request.getParameter("t1"));
int fact=1;
while(n>1)
{
fact=fact*n;
n--;
}
out.println("<h1>Factorial is "+fact);
%>
</body>
</html>

Output:

4.
Input.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

26
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

<title>Insert title here</title>


</head>
<body>
<form action="output.jsp">
<pre>
Enter Number: <input type = text name = t1><br> Enter
Name: <input type = text name = t2><br> Enter
Designation: <input type = text name = t3><br> <input type
= submit> <input type = reset>
</pre>
</form>
</body>
</html>

Output.jsp
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
try
{
String no=request.getParameter("t1");
String name=request.getParameter("t2");
String desig=request.getParameter("t3");

Class.forName("com.mysql.cj.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/sycs","root","root");

String query="insert into emp values(?,?,?)";


PreparedStatement ps=con.prepareStatement(query);
ps.setString(1, no);
ps.setString(2, name);
ps.setString(3, desig);
ps.executeUpdate();
out.println("<h1>Record Added successfully!");
out.println("<br><a href=input.jsp>Wants to add more
records?</a>");

27
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

}
catch(Exception ex)
{
System.out.println(ex);
}
%>

</body>
</html>

Output:

28
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

PRACTICAL-8

AIM:Create and Consume Web services in Java.


NETBEANS:
Evenoddws.java
package server;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
@WebService(serviceName = "evenoddws")
public class evenoddws {
/**
* Web service operation
*/
@WebMethod(operationName = "evenodd")
public String evenodd(@WebParam(name = "n") int n)
{
if(n%2==0)
return n+"is even";
else
return n+"is odd";
}
}
Index.jsp
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
29
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

<title>JSP Page</title>
</head>
<body>
<%-- start web service invocation --%><hr/>
<%
try {

client.Evenoddws_Service service = new


client.Evenoddws_Service();
client.Evenoddws port = service.getEvenoddwsPort();
// TODO initialize WS operation arguments here
int n = Integer.parseInt(request.getParameter("t1"));
// TODO process result here
java.lang.String result = port.evenodd(n);
out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}
%>
</body>
</html>
<%-- end web service invocation --%><hr/>
Input.jsp
<%@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>

30
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

<body>
<form action="index.jsp">
<pre>
Enter any no: <input type="text" name="t1">
<input type="submit"><input type="reset"> </pre>
</form>
</body>
</html>
Output:

Index.jsp
<%@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>
<%-- start web service invocation --%><hr/>
<%
try {
client.Additionws_Service service = new
31
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

client.Additionws_Service(); client.Additionws port =


service.getAdditionwsPort();
// TODO initialize WS operation arguments here
int n1 = Integer.parseInt(request.getParameter("t1"));
int n2 = Integer.parseInt(request.getParameter("t2"));
// TODO process result here
java.lang.String result = port.addition(n1, n2);
out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}
%>
</body>
</html>
<%-- end web service invocation --%><hr/>
Addition.java
package server;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
@WebService(serviceName = "additionws")
public class additionws {
/**
* Web service operation
*/
@WebMethod(operationName = "addition")
public String addition(@WebParam(name = "n1") int
n1, @WebParam(name = "n2")
int n2) {
return "addition is"+(n1+n2);
}
}
32
G.N KHALSA COLLEGE (AUTONOMOUS) B-292 Siddiqui Sara

Input.jsp
<%@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>
<form action="index.jsp">
<pre>
Enter first no: <input type="text" name="t1">
Enter second no:<input type="text" name="t2">
<input type="submit"><input type="reset">
</pre>
</form>
</body>
</html>

33

You might also like