You are on page 1of 25

1

Q1.Jdbc program to connect the oracle database.


import java.sql.*;
class OracleXEDemo
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Connected");
Connection connection =
DriverManager.getConnection("jdbc:odbc:oracleXE","system","password");
System.out.println("The connection object is:"+connection);
connection.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

cmd output:
2

Q2.Create a new Database Table using JDBC


import java.sql.*;
class CreateTable
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:msaDSN");
Statement st=con.createStatement();
st.executeUpdate("create table student(rollno int,name varchar(15))");
st.close();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

Database Changes:
3

Q3.Jdbc Program to insert records into Database.

import java.sql.*;
class InsertTable
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:msaDSN");
Statement st=con.createStatement();
st.executeUpdate("insert into student values(101,'mukesh')");
st.executeUpdate("insert into student values(102,'sukesh')");
st.executeUpdate("insert into student values(103,'rakesh')");
st.executeUpdate("insert into student values(104,'ramesh')");
st.executeUpdate("insert into student values(105,'suresh')");
st.executeUpdate("insert into student values(106,'nikesh')");
st.close();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

Database Changes output:


4

Q4.JDBC program to update records in database

import java.sql.*;
class UpdateTable
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:msaDSN");
Statement st=con.createStatement();
st.executeUpdate("update student set rollno=107 where name='nikesh'");
st.close();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

Database Changes output:


5

Q5.JDBC program to delete records from database

import java.sql.*;
class DeleteTableRow
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:msaDSN");
Statement st=con.createStatement();
st.executeUpdate("delete from student where name='nikesh'");
System.out.println("Row Deleted");
st.close();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

Database Changes output:


6

Q6.JDBC program to read the data from database using ResultSet.

import java.sql.*;
class ResultSetDemo
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:msaDSN");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from student");
while(rs.next())
{
int i=rs.getInt(1);
String s=rs.getString(2);
System.out.println("Rollno="+i+"\t"+"Name="+s);
}
rs.close();
st.close();
con.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

cmd output:
7

Q7. Jdbc Program to demonstrate PreparedStatement

import java.sql.*;
class PreparedStatementDemo
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:msaDSN");
PreparedStatement pst=con.prepareStatement("insert
into student values(?,?)");
pst.setInt(1,106);
pst.setString(2,"somesh");
pst.executeUpdate();
System.out.println("one record inserted");
pst.setInt(1,107);
pst.setString(2,"lingesh");
pst.executeUpdate();
System.out.println("second record inserted");
PreparedStatement
pst2=con.prepareStatement("update student set name=? where
rollno=?");
pst2.setString(1,"mayuresh");
pst2.setInt(2,104);
pst2.executeUpdate();
System.out.println("one record updated");

pst2.close();
pst.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
cmd output:
8

Database Changes output:


9

Q8. Jdbc Program to demonstrate CallableStatement

SQL procedure:

Java Code:

import java.sql.*;
class CallableStatementDemo
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:oracleXE","system",
"password");
CallableStatement cst=con.prepareCall("{call
getName(?,?)}");
cst.setInt(1,7876);
cst.registerOutParameter(2,Types.VARCHAR);
cst.execute();
System.out.println(cst.getString(2));
cst.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
10

cmd output:
11

Q9. Servlet Program to print Hello World.

import javax.servlet.*;
import java.io.*;
public class HelloServlet extends GenericServlet
{
public void service(ServletRequest req,ServletResponse
res)throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
pw.println("First Servlet Programming");
}
}

Browser Output:
12

Q10.Program to demonstrate Servlet LifeCycle methods.

Html Login Page:

<html lang="en">
<head>
<meta charset="UTF-8">
<title>Servlet Lifecycle Login</title>
</head>
<body>
<h1>Login Page</h1>
<center>
<form action="http://localhost:8090/g/LifeCycle" method="GET">
Username:<input type="text" name="u1">
<br>
Password:<input type="password" name="p1">
<br>
<input type="submit" value="SUBMIT">
<input type="reset" value="RESET">
</form>
</center>
</body>
</html>

Servlet Program:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class LifeCycle extends HttpServlet
{
Connection con;ResultSet rs;Statement st;
public void init(ServletConfig sc)throws ServletException
{
try
{
super.init(sc);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:msaDSN");
}
catch(Exception e)
{
e.printStackTrace();
}
13

}
public void doGet(HttpServletRequest
req,HttpServletResponse res)throws
ServletException,IOException
{
try
{
String user=req.getParameter("u1");
String pwd=req.getParameter("p1");
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
st=con.createStatement();
st.executeUpdate("insert into user
values('"+user+"','"+pwd+"')");
rs=st.executeQuery("select * from user");
pw.println("<html><body><table border=3
bordercolor='green'>"+"<caption>Login
Table</caption>"+"<tr><th>UserName</th><th>Password</th></tr>"
);
while(rs.next())
{
String u=rs.getString(1);
String p=rs.getString(2);

pw.println("<tr><td>"+u+"</td><td>"+p+"</td></tr>");
}
pw.println("</table></body></html>");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void destroy()
{
try
{
rs.close();
st.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
14

Html login page output:

Servlet Browser Output:


15

Q11.Servlet Program to Demonstrate Session Tracking.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class HttpSessionDemo extends HttpServlet
{
public void doGet(HttpServletRequest
req,HttpServletResponse res)throws
ServletException,IOException
{
HttpSession ses=req.getSession(true);
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
Date d=(Date)ses.getValue("Date");
if(d==null)
{
pw.println("Frist Request");
d=new Date();
ses.putValue("Date",d);
}
else
{
pw.println("Last requested time:"+d);
d=new Date();
ses.putValue("Date",d);
}
}
}

Browser output:

1st request output:


16

Second request output:


17

Q12.Servlet program to Demonstrate Cookies.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class CookieDemo extends HttpServlet
{
public void doGet(HttpServletRequest
req,HttpServletResponse res)throws
ServletException,IOException
{

Cookie c=new Cookie("Country","India");


res.setContentType("text/html");
PrintWriter pw=res.getWriter();
res.addCookie(c);
pw.println("Cookie is created");
}
}

Browser output:
18

Q13.Program to demonstrate JSP scripting elements.

<html>
<body>
<h1>Demonstration of Scripting Tag</h1>
<%
for(int i=1;i<=10;i++)
{
out.println(i);
}
%>
<h1>Demonstration of Declarative tag</h1>
<%! int a,b;
int fun(int a)
{
return a*a;
}
%>
<%
a=10;
b=fun(a);
out.println(b);
%>
<h1>Demonstration of Expression Tag</h1>
<%! int count;%>
<%count++;%>
This page is requested by
<%=count%>number of users till now
</body>
</html>

Browser output:
19

Q14.Program to demonstrate JSP implicit objects.

HTML Login Page:

<html lang="en">
<head>
<meta charset="UTF-8">
<title>Servlet Lifecycle Login</title>
</head>
<body>
<h1>Login Page</h1>
<center>
<form action="http://localhost:8090/n/Implicit.jsp" method="GET">
Username:<input type="text" name="u1">
<br>
Password:<input type="password" name="p1">
<br>
<input type="submit" value="SUBMIT">
<input type="reset" value="RESET">
</form>
</center>
</body>
</html>

Servlet Code:

<html>
<body>
<center>
<h1>Login Details</h1>
<%
String u=request.getParameter("u1");
String p=request.getParameter("p1");
out.println("UserName:"+u);
out.println("Password:"+p);
%>
</center>
</body>
</html>
20

Html login page output:

Browser output:
21

Q15.JSP program to process the form .

Html Registration Form code:

<html>
<body>
<center>
<h1>Student Registration Form</h1>
<form action="http://localhost:8090/o/Registration.jsp" method="get">
<fieldset>
<legend>Enter Details</legend>
Student Name:<input type="text" name="s1"><br>
Roll NO:<input type="text" name="r1"><br>
Class:<input type="text" name="c1"><br>
EmailId:<input type="text" name="e1"><br>
Mobile:<input type="text" name="m1"><br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</fieldset>
</form>
</center>
</body>
</html>

Servlet Code:

<html>
<body>
<h1>Student Details</h1>
<%
String s=request.getParameter("s1");
String r=request.getParameter("r1");
String c=request.getParameter("c1");
String e=request.getParameter("e1");
String m=request.getParameter("m1");
%>
Student Name:<%=s%><br>
Roll No:<%=r%><br>
Class:<%=c%><br>
Email:<%=e%><br>
Mobile:<%=m%><br>
</body>
</html>
22

Html registration form output:

Browser output:
23

Q16.Develop simple form application to process the registration from using JSP and JDBC.

JSPLifeCycle Login Page:

<html>
<head>
<title>Login Form</title>
</head>
<body>
<center>
<H1>Jsp Login Page</h1>
<form action="http://localhost:8090/q/JSPLifecycle.jsp" method=POST><br>
UserName:<input type=text name="u1"><br>
Password :<input type=password name="p1"><br>
<input type=submit value=submit>
<input type=reset value=reset>
</form>
</center>
</body>
</html>

JSPLifeCycle code:

<html>
<body>
<%@page import="java.sql.*, java.io.*" %>
<%! Connection con;Statement st;ResultSet rs;%>
<%
try{
String u=request.getParameter("u1");
String p=request.getParameter("p1");

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:msaDSN");
st=con.createStatement();
st.executeUpdate("insert into user values(' "+u+" ',' "+p+" ')");
rs=st.executeQuery("select * from user");
%>
<table border=3 bordercolor=green>
<caption>LoginTable</caption>
<tr><th>UserName</th><th>Password</th></tr>
<%
while(rs.next())
{
String s=rs.getString(1);
24

String s1=rs.getString(2); %>


<tr><td><%=s%></td><td><%=s1%></td></tr>
<%}%>
</table>
<% rs.close();st.close();con.close();}
catch(Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>

Html login page output:

Browser output:
25

You might also like