0% found this document useful (0 votes)
10 views50 pages

Java Database Operations and GUI Examples

The document contains a series of Java programs and servlets demonstrating various database operations such as retrieving, inserting, updating, and deleting records from MS-Access and Oracle databases. It also includes GUI designs for navigating and inserting records, as well as servlets for handling HTTP requests and displaying messages. Additionally, there are examples of forms for user input and corresponding servlet logic to process that input.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views50 pages

Java Database Operations and GUI Examples

The document contains a series of Java programs and servlets demonstrating various database operations such as retrieving, inserting, updating, and deleting records from MS-Access and Oracle databases. It also includes GUI designs for navigating and inserting records, as well as servlets for handling HTTP requests and displaying messages. Additionally, there are examples of forms for user input and corresponding servlet logic to process that input.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Name :- Rajani Suryabhan Kawade

Roll no:- 08

Sub:- Java practical

que no:-14] Write a program to retrive the records of the table to MS-Access database using DSN.

import [Link].*;

import [Link].*;

class JDBCDemo

public static void main(String args[])throws IOException

try

String url="jdbc:odbc:studdsn";

[Link]("[Link]");

Connection con=[Link](url);

Statement st=[Link]();

Resultset rs= [Link]("select * from stud");

while([Link]())

[Link]([Link]("roll no")+" "+[Link]("name")+" "[Link]("add"));

}// while close

[Link]();

}//try close

catch(SQL Exception e)

[Link]("error in sql"+e);

catch(Exception e)
{

[Link]("error"+e);

que no:- 15] Write a program to retrive the records of a table to the oracle Database using DSN.

import [Link].*;

import [Link].*;

import [Link].*;

class OracleJDBC

public static void main(String args[])throws IOException

try

String url="jdbc:odbc:studdsn";

[Link]("[Link]");

Connection con=[Link](url,"MS","ims");

Statement st=[Link]();

Resultset rs= [Link]("select * from stud");

while([Link]())

[Link]([Link]("roll no")+" "+[Link]("name")+" "[Link]("add"));

}// while close

[Link]();

}//try close

catch(SQL Exception e)

{
[Link]("error in sql"+e);

catch(Exception e)

[Link]("error"+e);

qus no:- 16] Write a program to retrive the records of a table to the oracle Database using Thin
Driver.

import [Link].*;

import [Link].*;

import [Link].*;

class Thindemo

public static void main(String args[])throws IOException

try

String url="jdbc:odbc:thin:@localhost:1881:xe";

[Link]("[Link]");

Connection con=[Link](url,"MS","ims");

Statement st=[Link]();

Resultset rs= [Link]("select * from stud");

while([Link]())

[Link]([Link]("roll no")+" "+[Link]("name")+" "[Link]("add"));

}// while close


[Link]();

}//try close

catch(SQL Exception e)

[Link]("error in sql"+e);

catch(Exception e)

[Link]("error"+e);

que no:- 17] Write a program to insert , update, delete and search a record using statement.

import [Link].*;

import [Link].*;

class Student

public static void main(String args[])

Connection con=null;

Statement st=null;

ResultSet rs=null;

try

[Link]("[Link]");

con=[Link]("jdbc:odbc:sdsn");

st=[Link]();

String sql="insert into stud values(8,'misha','btech')";


int i=[Link](sql);

String sql1="update stud set course='mtech' where rollno=3";

[Link](sql1);

String sql2="delete from stud where course='bcs'";

[Link](sql2);

BufferedReader br=new BufferedReader(new InputStreamReader([Link]));

[Link]("enter course to search");

String course=[Link]();

String sql3="select * from stud where course="+course;

rs=[Link](sql3);

if([Link]())

[Link]([Link](1)+" "+[Link](2)+" "+[Link](3));

else

[Link]("record not found");

[Link]();

[Link]();

[Link]();

catch(Exception e)

[Link]("error");

}
que no:- 21] Design a GUI to a navigate the records of the table using scrollable Resultset.

[Link]

import [Link].*;

import [Link].*;

import [Link].*;

class FrameNavigate extends Frame implements ActionListener

Label l1,l2,l3;

TextField t1,t2,t3;

Button b1,b2,b3,b4;

Connection con=null;

ResultSet rs=null;

Statement st=null;

public FrameNavigate()

l1=new Label("rollno");

l2=new Label("name");

l3=new Label("course");

t1=new TextField(20);

t2=new TextField(20);

t3=new TextField(20);

b1=new Button("First");

b2=new Button("Next");

b3=new Button("Prev");

b4=new Button("Last");
setBackground([Link]);

setForeground([Link]);

setSize(500,500);

setVisible(true);

setLayout(new GridLayout(5,2));

add(l1);

add(t1);

add(l2);

add(t2);

add(l3);

add(t3);

add(b1);

add(b2);

add(b3);

add(b4);

[Link](this);

[Link](this);

[Link](this);

[Link](this);

addWindowListener(new WindowAdapter()

public void windowClosing(WindowEvent we)

dispose();

});

public void actionPerformed(ActionEvent ae)

try
{

[Link]("[Link]");

con=[Link]("jdbc:odbc:sdsn");

st=[Link](ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

rs=[Link]("select * from stud");

if([Link]()==b1)

[Link]();

else

if([Link]()==b2)

[Link]();

if([Link]())

[Link]();

else

if([Link]()==b3)

[Link]();

if([Link]())

[Link]();

else

if([Link]()==b4)

[Link]();

[Link]([Link](1));
[Link]([Link](2));

[Link]([Link](3));

catch(Exception e2)

[Link](e2);

public static void main(String args[]) throws Exception

new FrameNavigate();

que no:-22] Design the GUI to insert the record in table.

[Link]

import [Link].*;

import [Link].*;

import [Link].*;

class InsertFrame extends Frame implements ActionListener

Label l1,l2,l3;

TextField t1,t2,t3;

Button b1,b2;

Connection con=null;

ResultSet rs=null;

Statement st=null;

public InsertFrame()

l1=new Label("Rollno");
l2=new Label("Name");

l3=new Label("Course");

t1=new TextField(20);

t2=new TextField(20);

t3=new TextField(20);

b1=new Button("Add");

b2=new Button("clear");

setBackground([Link]);

setForeground([Link]);

setSize(500,500);

setVisible(true);

setLayout(new GridLayout(5,2));

add(l1);

add(t1);

add(l2);

add(t2);

add(l3);

add(t3);

add(b1);

add(b2);

[Link](this);

[Link](this);

addWindowListener(new WindowAdapter()

public void windowClosing(WindowEvent we)


{

dispose();

});

public void actionPerformed(ActionEvent ae)

if([Link]()==b1)

try

[Link]("[Link]");

con=[Link]("jdbc:odbc:sdsn");

PreparedStatement pst=[Link]("insert into stud values(?,?,?)");

int a=[Link]([Link]().trim());

String b=[Link]().trim();

String c=[Link]().trim();

[Link](1,a);

[Link](2,b);

[Link](3,c);

[Link]();

[Link]("Record inserted");

}//try

catch(Exception e1)

[Link](e1);

else

if([Link]()==b2)
{

[Link](" ");

[Link](" ");

[Link](" ");

public static void main(String args[]) throws Exception

new InsertFrame();

que no:-23] Write a program to insert,update & delete records using updatable

[Link]

import [Link].*;

class UpdatableDemo

public static void main(String args[])

try

[Link]("[Link]");

Connection con=[Link]("jdbc:odbc:sdsn");

Statement
st=[Link](ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

ResultSet rs=[Link]("select * from stud");

//inserting
[Link]();

[Link]("rollno",7);

[Link]("name","karuna");

[Link]("course","msc");

[Link]();

//[Link]();

[Link]("new record inserted");

//updating

[Link](1);

[Link]("course","bba");

[Link]();

[Link]("first record updated");

//deleting

[Link](3);

[Link]();

[Link]("3rd record deleted");

[Link]();

[Link]();

[Link]();

catch(Exception e)

[Link](e);

que no:- 24] Write a program to use demonstrate the use of ResultSetMetadata
& ganerate.

[Link]

import [Link].*;

public class TestRSMD

public static void main(String args[])throws Exception

[Link]("[Link]");

Connection con=[Link]("jdbc:odbc:sdsn");

Statement st=[Link]();

ResultSet rs=[Link]("select * from stud");

ResultSetMetaData rsmd=[Link]();

for(int i=0;i<[Link]();i++)

[Link]([Link](i+1) + "\t");

[Link]();

while([Link]())

[Link]([Link](1)+"\t"+[Link](2)+"\t"+[Link](3));

[Link]();

[Link]();

[Link]();

servlet:
que no:- 27] Write a servlet to display welcome msg using GenericServlet.

import [Link].*;

import [Link].*;

public class TestGServ extends GenericServlet

public void service(ServletRequest req,ServletResponse res)throws ServletException,IOException

[Link]("text/html");

PrintWriter out=[Link]();

[Link]("<html>");

[Link]("<body>");

[Link]("<b>Welcome to servlets</b>");

[Link]("</body>");

[Link]("</html>");

[Link]();

[Link]

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="[Link]

xmlns:xsi="[Link]

xsi:schemaLocation="[Link] [Link]
app_2_5.xsd"

version="2.5">
<display-name>Welcome to Tomcat</display-name>

<description>

Welcome to Tomcat

</description>

<servlet>

<servlet-name>TestGServ</servlet-name>

<servlet-class>TestGServ</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>TestGServ</servlet-name>

<url-pattern>/test/TestGServ</url-pattern>

</servlet-mapping>

</web-app>

que no:- 28] Write a servlet to display welcome msg using httpservlet.

[Link]

import [Link].*;

import [Link].*;

import [Link].*;

public class TestHServ extends HttpServlet

public void doGet(HttpServletRequest req,HttpServletResponse res)throws


ServletException,IOException

[Link]("text/html");

PrintWriter out=[Link]();

[Link]("<html>");
[Link]("<body>");

[Link]("<b>Welcome to servlets</b>");

[Link]("</body>");

[Link]("</html>");

[Link]();

[Link]

<servlet>

<servlet-name>TestHServ</servlet-name>

<servlet-class>TestHServ</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>TestHServ</servlet-name>

<url-pattern>/test/TestHServ</url-pattern>

</servlet-mapping>

que no:- 29] Write a servlet to handle login form data using GET method.

[Link]

<html>

<head>

<title>login form</title>

</head>

<body>

<form method =GET action="[Link]

Enter Username

<input type="Text" name="t1">

<br>
Enter Password

<input type="password" name="t2">

<br>

<input type="submit" value="Login">

<input type="reset" value="clear">

</form>

</body>

</html>

[Link]

import [Link].*;

import [Link].*;

import [Link].*;

public class Greet extends HttpServlet

public void doGet(HttpServletRequest req,HttpServletResponse res)throws


ServletException,IOException

[Link]("text/html");

PrintWriter pw=[Link]();

String uname=[Link]("t1");

String pass=[Link]("t2");

[Link]("Username: "+uname);

[Link]("\nPassword: "+pass);

[Link]();

[Link]

<servlet>

<servlet-name>Greet</servlet-name>
<servlet-class>Greet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Greet</servlet-name>

<url-pattern>/test/Greet</url-pattern>

</servlet-mapping>

que no:- 30] Write a servlet to handle login form data using POST method.

[Link]

<html>

<head>

<title>login form</title>

</head>

<body>

<form method =POST action="[Link]

Enter Username

<input type="Text" name="t1">

<br>

Enter Password

<input type="password" name="t2">

<br>

<input type="submit" value="Login">

<input type="reset" value="clear">

</form>

</body>

</html>

[Link]

import [Link].*;

import [Link].*;
import [Link].*;

public class Details extends HttpServlet

public void doPost(HttpServletRequest req,HttpServletResponse res)throws


ServletException,IOException

[Link]("text/html");

PrintWriter pw=[Link]();

String uname=[Link]("t1");

String pass=[Link]("t2");

[Link]("Username: "+uname);

[Link]("\nPassword: "+pass);

[Link]();

[Link]

<servlet>

<servlet-name>Details</servlet-name>

<servlet-class>Details</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Details</servlet-name>

<url-pattern>/test/Details</url-pattern>

</servlet-mapping>

que no:- 31] Design a form to accept the name & age of a person .write a servlet to check if the
person is eligible for voting or not.

[Link]

<html>
<head>

<title>voting eligibility</title>

</head>

<body>

<form method =GET action="[Link]

Enter Name

<input type="Text" name="t1">

<br>

Enter Age<input type="Text" name="t2">

<br>

<input type="submit" value="Login">

<input type="reset" value="clear">

</form>

</body>

</html>

[Link]

import [Link].*;

import [Link].*;

import [Link].*;

public class CheckAge extends HttpServlet

public void doGet(HttpServletRequest req,HttpServletResponse res)throws


ServletException,IOException

[Link]("text/html");

PrintWriter pw=[Link]();

String name=[Link]("t1");

int age=[Link]([Link]("t2"));

if(age>=18)

[Link](name+" is eligible for voting");


else

[Link](name+" is not eligible for voting");

[Link]();

[Link]

<servlet>

<servlet-name>CheckAge</servlet-name>

<servlet-class>CheckAge</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>CheckAge</servlet-name>

<url-pattern>/test/CheckAge</url-pattern>

</servlet-mapping>

que no:- 32] Design a form to accept favorite fruit details of a person. write a servlet to display
details of a proper fruits.

[Link]

<html>

<head>

<title>Fruits</title>

</head>

<body>

<form method =GET action="[Link]

Select fruits of your choice:

<br>

<input type=checkbox name="f1" value="Apple">Apple</select>


<br>

<input type=checkbox name="f2" value="Banana">Banana</select>

<br>

<input type=checkbox name="f3" value="Pineapple">Pineapple</select>

<br>

<input type=checkbox name="f4" value="Grapes">Grapes</select>

<br>

<input type=checkbox name="f5" value="Orange">Orange</select>

<br>

<input type="submit" value="Submit">

</form>

</body>

</html>

[Link]

import [Link].*;

import [Link].*;

import [Link].*;

public class ShowFruits extends HttpServlet

public void doGet(HttpServletRequest req,HttpServletResponse res)throws


ServletException,IOException

[Link]("text/html");

PrintWriter pw=[Link]();

String h=" ";

if([Link]("f1")!=null)

h=h+[Link]("f1")+" ";

if([Link]("f2")!=null)
h=h+[Link]("f2")+" ";

if([Link]("f3")!=null)

h=h+[Link]("f3")+" ";

if([Link]("f4")!=null)

h=h+[Link]("f4")+" ";

if([Link]("f5")!=null)

h=h+[Link]("f5")+" ";

[Link]("<br>Selected fruits: "+h);

[Link]();

[Link]

<servlet>

<servlet-name>ShowFruits</servlet-name>

<servlet-class>ShowFruits</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>ShowFruits</servlet-name>

<url-pattern>/test/ShowFruits</url-pattern>

</servlet-mapping>

que no:- 33] Design a registration form for a Fest-Der-Tech. write a servlet to display the details
along with amount proper format.

[Link]

<html>

<head>
<title>Fest-der-tech reqistration form</title>

</head>

<body>

<form method =GET action="[Link]

<table border=0>

<caption>FEST-DER-TECH Form</caption>

<tr>

<td>Name:

<td><input type="Text" name="t1">

</tr>

<tr>

<td>Class:

<td><input type="Text" name="t2">

</tr>

<tr>

<td>Gender:

<td><input type="Radio" name="r1" value="Male">Male

<input type="Radio" name="r1" value="Female">Female

</tr>

<tr>

<td>Mobile no:

<td><input type="Text" name="t3">

</tr>

<tr>

<td>Events:

<td><input type="Checkbox" name="c1" value="Aptitude test">Aptitude test

<br><input type="Checkbox" name="c2" value="Website development">Website development

<br><input type="Checkbox" name="c3" value="Poster presentation">Poster presentation

<br><input type="Checkbox" name="c4" value="Quiz competition">Quiz competition

</tr>

<tr>
<td><input type="submit" value="Submit">

<td><input type="reset" value="clear">

</table>

</form>

</body>

</html>

[Link]

import [Link].*;

import [Link].*;

import [Link].*;

public class FestDetails extends HttpServlet

public void doGet(HttpServletRequest req,HttpServletResponse res)throws


ServletException,IOException

[Link]("text/html");

PrintWriter pw=[Link]();

String name=[Link]("t1");

String course=[Link]("t2");

String gender=[Link]("r1");

String mob=[Link]("t3");

String h=" ";

if([Link]("c1")!=null)

h=h+[Link]("c1")+" ";

if([Link]("c2")!=null)

h=h+[Link]("c2")+" ";

if([Link]("c3")!=null)
h=h+[Link]("c3")+" ";

if([Link]("c4")!=null)

h=h+[Link]("c4")+" ";

int amt=0;

if([Link]("c1")!=null)

amt=amt+50;

if([Link]("c2")!=null)

amt=amt+50;

if([Link]("c3")!=null)

amt=amt+50;

if([Link]("c4")!=null)

amt=amt+50;

[Link]("Name: "+name);

[Link]("<br>Class: "+course);

[Link]("<br>Gender: "+gender);

[Link]("<br>Mobile no: "+mob);

[Link]("<br>Selected events: "+h);

[Link]("<br>Amount: "+amt);

[Link]();

[Link]

<servlet>
<servlet-name>FestDetails</servlet-name>

<servlet-class>FestDetails</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>FestDetails</servlet-name>

<url-pattern>/test/FestDetails</url-pattern>

</servlet-mapping>

que no:- 34] Write a servlet program to retrive & display the record in table.

[Link]

import [Link].*;

import [Link].*;

import [Link].*;

import [Link].*;

public class ShowStud extends HttpServlet

public void doGet(HttpServletRequest req,HttpServletResponse res)throws


ServletException,IOException

[Link]("text/html");

PrintWriter pw=[Link]();

try

[Link]("[Link]");

Connection con=[Link]("jdbc:odbc:sdsn");

Statement st=[Link]();

ResultSet rs=[Link]("select * from stud");

[Link]("<table border=1>");

[Link]("<caption>Student Details</caption>");
[Link]("<tr><th>Rollno<th>Name<th>Course</tr>");

while([Link]())

[Link]("<tr>");

[Link]("<td>"+[Link](1));

[Link]("<td>"+[Link](2));

[Link]("<td>"+[Link](3));

[Link]("</tr>");

[Link]("</table>");

[Link]();

[Link]();

[Link]();

catch(ClassNotFoundException e1)

[Link]("Driver not found");

catch(SQLException e2)

[Link](e2);

catch(Exception e3)

[Link](e3);

[Link]();

[Link]
<servlet>

<servlet-name>ShowStud</servlet-name>

<servlet-class>ShowStud</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>ShowStud</servlet-name>

<url-pattern>/test/ShowStud</url-pattern>

</servlet-mapping>

que no:- 35] Write servlet program to insert the record in a database table.

[Link]

<html>

<head>

<title>Inserting record</title>

</head>

<body>

<form method =GET action="[Link]

<table border=0>

<tr>

<td>Enter Rollno:

<td><input type="Text" name="t1">

</tr>

<tr>

<td>Enter Name:

<td><input type="Text" name="t2">

</tr>

<tr>

<td>Enter course:

<td><input type="Text" name="t3">


</tr>

<tr>

<td><input type="submit" value="Submit">

<td><input type="reset" value="clear">

</tr>

</table>

</form>

</body>

</html>

[Link]

import [Link].*;

import [Link].*;

import [Link].*;

import [Link].*;

public class InsertStud extends HttpServlet

public void doGet(HttpServletRequest req,HttpServletResponse res)throws


ServletException,IOException

[Link]("text/html");

PrintWriter pw=[Link]();

try

[Link]("[Link]");

Connection con=[Link]("jdbc:odbc:sdsn");

PreparedStatement pst=[Link]("insert into stud values(?,?,?)");

int a=[Link]([Link]("t1").trim());

String b=[Link]("t2").trim();

String c=[Link]("t3").trim();
[Link](1,a);

[Link](2,b);

[Link](3,c);

[Link]();

[Link]("new record added");

[Link]();

[Link]();

catch(ClassNotFoundException e1)

[Link]("Driver not found");

catch(SQLException e2)

[Link](e2);

catch(Exception e3)

[Link](e3);

[Link]();

[Link]

<servlet>

<servlet-name>InsertStud</servlet-name>

<servlet-class>InsertStud</servlet-class>

</servlet>
<servlet-mapping>

<servlet-name>InsertStud</servlet-name>

<url-pattern>/test/InsertStud</url-pattern>

</servlet-mapping>

que no:- 36] Write a servlet program to show the user of servletcontex interface.

[Link]

import [Link].*;

import [Link].*;

import [Link].*;

public class Demoservlet extends HttpServlet

public void doGet(HttpServletRequest req,HttpServletResponse res)throws


ServletException,IOException

[Link]("text/html");

PrintWriter pw=[Link]();

servlet context context=getservlet context();

String driverName=[Link] parameter("dname");

[Link]("driver name is ="+deiverName);

[Link]();

[Link]

<servlet>

<servlet-name>abc</servlet-name>

<servlet-class>Demoservlet</servlet-class>
</servlet>

<context-param>

<param-name>dname</param-name>

<param-value>[Link] Driver</param-value>

</context-param>

<servlet-mapping>

<servlet-name>abc</servlet-mapping>

<url-pattern>/context</url-pattern>

</servlet-mapping>

que no:- 37] Write a servlet program to show the user of servletconfig.

[Link]

import [Link].*;

import [Link].*;

import [Link].*;

public class Myservlet extends HttpServlet

public void doGet(HttpServletRequest req,HttpServletResponse res)throws


ServletException,IOException

[Link]("text/html");

PrintWriter out=[Link]();

servlet config sc= getservlet config();

[Link]([Link] parameter("email"));

[Link]

<servlet>
<servlet-name>check</servlet-name>

<servlet-class>Myservlet</servlet-class>

<init-param>

<param-name>email</param-name>

<param-value>abc@[Link]</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>check</servlet-mapping>

<url-pattern>/check</url-pattern>

</servlet-mapping>

JSP

location:(C:\Program Files (x86)\Apache

Software Foundation\Tomcat 6.0\webapps\abc)

Que no:-39] Write a JSP page to print the multiplication table of a number.

[Link]

<html>

<head>

<title>Multiplication table</title>

</head>

<body>

<%

int n=5,i;

for(i=1;i<=10;i++)

[Link]("5 * "+i+"=" + (n*i));


[Link]("<br>");

%>

</body>

</html>

Que no:-40] Write a JSP page to find factorial number.

[Link]

<html>

<head>

<title>Factorial</title>

</head>

<body>

<%!

int factorial(int n)

if (n==1)

return n;

else

return n * factorial(n-1);

%>

<%

[Link]("The factorial of 5 is:" + factorial(5));

%>

</body>
</html>

Que no:-41] Design the html form to accept the personal details-name,address,contact no. write a
jsp page to display details in proper format.

[Link]

<html>

<head>

<title>Details</title>

</head>

<body>

<form method=GET

action="[Link]

Enter name

<input type=text name="t1">

<br>

<br>

Enter address

<input type=text name="t2">

<br>

<br>

Enter contact no.

<input type=text name="t3">

<br>

<br>

<input type=submit value="submit">

</form>

</body>

</html>

disp_details.jsp
<%@page language="java"%>

<%

try

String name=[Link]("t1");

String address=[Link]("t2");

String number=[Link]("t3");

[Link]("Name: "+name);

[Link]("<br>");

[Link]("Address: "+address);

[Link]("<br>");

[Link]("Contact number: "+number);

catch(Exception e1)

[Link](e1);

%>

Que no:- 42] Design the html form to select the favorite fruits using checkboxes. write a JSP page to
display the selected fruits as a bullated list.

[Link]

<html>

<head>

<title>welcome</title>

</head>

<body>

<form method=GET

action="[Link]
Select fruits of your choice:

<br>

<input type=checkbox name="fruits" value="Apple">Apple</select>

<br>

<input type=checkbox name="fruits" value="Banana">Banana</select>

<br>

<input type=checkbox name="fruits" value="Pineapple">Pineapple</select>

<br>

<input type=checkbox name="fruits" value="Grapes">Grapes</select>

<br>

<input type=checkbox name="fruits" value="Orange">Orange</select>

<br>

<input type=submit value="submit">

</form>

</body>

</html>

disp_fruits.jsp

<%@page language="java"%>

<%! String[] fruits; %>

<%

[Link]("You selected:");

fruits=[Link]("fruits");

if(fruits!=null)

for(int i=0;i<[Link];i++)

[Link]("<ul><li>"+fruits[i]+"</li></ul>");

else
[Link]("<b>None<b>");

%>

Que no:-43] Write a JSP page to demonstrate the user of <%@include---%> directive.

[Link]

<html>

<head>

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

<title>Home page</title>

</head>

<body>

<%@ include file="[Link]" %>

<hr/>

<h2>This is main content</h2>

<hr/>

<%@ include file="[Link]" %>

</body>

</html>

[Link]

<h1>This is header</h1>

[Link]

<h5>This is footer</h5>

Que no:-44] Write a jsp page to retrive & display the records from a database table.

student_disp.jsp

<%@ page language="java"


import="[Link].*"%>

<%

try

[Link]("[Link]");

Connection con=[Link]("jdbc:odbc:sdsn");

Statement st=[Link]();

ResultSet rs=[Link]("select * from stud");

while([Link]())

[Link]([Link](1)+" "+[Link](2)+" "+[Link](3));

[Link]("<br>");

[Link]();

[Link]();

catch(Exception e1)

[Link](e1);

%>

Que no:-45] Write a jsp page to insert a record in a table.

[Link]

<html>

<head>

<title>inserting row</title>

</head>

<body>
<form method=GET

action="[Link]

Enter Rollno

<input type=text name="t1">

<br>

<br>

Enter Name

<input type=text name="t2">

<br>

<br>

Enter course

<input type=text name="t3">

<br>

<br>

<input type=submit value="submit">

</form>

</body>

</html>

[Link]

<%@ page language="java"

import="[Link].*"%>

<%

try

[Link]("[Link]");

Connection con=[Link]("jdbc:odbc:sdsn");

PreparedStatement pst=[Link]("insert into stud values(?,?,?)");

int a=[Link]([Link]("t1").trim());

String b=[Link]("t2").trim();

String c=[Link]("t3").trim();
[Link](1,a);

[Link](2,b);

[Link](3,c);

[Link]();

[Link]("New record is added");

[Link]();

[Link]();

catch(ClassNotFoundException e1)

[Link]("class Not found");

catch(SQLException e2)

[Link](e2);

catch(Exception e3)

[Link](e3);

%>

Que no:-46]write a jsp page to update a records in a table.

student_up.html

<html>

<head>

<title>updating row</title>

</head>

<body>

<form method=GET
action="[Link]

Enter Rollno

<input type=text name="t1">

<br>

<br>

Enter Name

<input type=text name="t2">

<br>

<br>

Enter course

<input type=text name="t3">

<br>

<br>

<input type=submit value="submit">

</form>

</body>

</html>

[Link]

<%@ page language="java"

import="[Link].*"%>

<%

try

[Link]("[Link]");

Connection con=[Link]("jdbc:odbc:sdsn");

PreparedStatement pst=[Link]("update stud set name=?, course=? where


rollno=?");

int a=[Link]([Link]("t1").trim());

String b=[Link]("t2").trim();

String c=[Link]("t3").trim();
[Link](1,b);

[Link](2,c);

[Link](3,a);

[Link]();

[Link]("record updated");

[Link]();

[Link]();

catch(ClassNotFoundException e1)

[Link]("class Not found");

catch(SQLException e2)

[Link](e2);

catch(Exception e3)

[Link](e3);

%>

Que no:-47] Write a jsp page to delete a record in a table.

stud_del.html

<html>

<head>

<title>deleting row</title>

</head>

<body>

<form method=GET
action="[Link]

Enter Rollno to delete

<input type=text name="t1">

<br>

<input type=submit value="submit">

</form>

</body>

</html>

[Link]

<%@ page language="java"

import="[Link].*"%>

<%

try

[Link]("[Link]");

Connection con=[Link]("jdbc:odbc:sdsn");

PreparedStatement pst=[Link]("delete from stud where rollno=?");

int a=[Link]([Link]("t1").trim());

[Link](1,a);

int i=[Link]();

[Link]("Record is deleted");

[Link]();

[Link]();

catch(Exception e1)

[Link](e1);

%>
Que no:-48] Write a jsp pade to search record in a table.

stud_search.html

<html>

<head>

<title>seraching record</title>

</head>

<body>

<form method= GET action="[Link]

Enter Rollno to search

<input type=text name="t1">

<br>

<input type=submit value="submit">

</form>

</body>

</html>

[Link]

<%@ page language="java"

import="[Link].*"%>

<%

try

[Link]("[Link]");

Connection con=[Link]("jdbc:odbc:sdsn");

PreparedStatement pst=[Link]("select * from stud where rollno=?");

int a=[Link]([Link]("t1").trim());

[Link](1,a);

ResultSet rs=[Link]();

if([Link]())
{

[Link]([Link](1)+" "+[Link](2)+" "+[Link](3));

else

[Link]("Record not found");

[Link]();

[Link]();

catch(Exception e1)

[Link](e1);

%>

Que no:-49] Write a jsp page to demonstrate the use of session management using session object.

[Link]

<html>

<head>

<title>welcome page: Enter your name</title>

</head>

<body>

<form action="[Link]">

<input type="text" name="inputname">

<input type="submit" value="click here!!"><br>

</form>

</body>

</html>
[Link]

<html>

<head>

<title>

Passing the input to session variable</title>

</head>

<body>

<%

String uname=[Link]("inputname");

[Link]("Welcome "+uname);

[Link]("sessname",uname);

%>

<a href="[Link]">check output page here</a>

</body>

</html>

[Link]

<html>

<head>

<title>

output page: Fetching the value from session

</title>

</head>

<body>

<%

String name=(String)[Link]("sessname");

[Link]("Hello User: You have entered the name:" +name);

%>

</body>

</html>

Common questions

Powered by AI

To perform CRUD operations using JDBC in Java, you should follow these steps: (1) Import the java.sql package and load the JDBC driver class, (2) Obtain a connection to the database using `DriverManager.getConnection`, (3) Create a Statement object if operations involve simple executions or a PreparedStatement for parameterized queries, (4) Use the Statement or PreparedStatement to execute the desired query for Create, Read, Update, or Delete operations, (5) After processing results, close the Statement and Connection to release resources .

ServletConfig is used for retrieving servlet-specific initialization parameters defined within the web.xml file, which provides information pertinent only to a single servlet. ServletContext, on the other hand, provides context initialization parameters that are application-wide, accessible by any part of the web application. Both interfaces help a servlet to get configuration details necessary for its work. They can be obtained within a servlet using `getServletConfig()` and `getServletContext()` methods, respectively, providing ways to manage application-level and servlet-level configuration settings .

To retrieve records from an MS-Access database using a DSN in Java, you can use the JDBC-ODBC bridge driver. First, load the driver class with `Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");`. Then, establish a connection using `DriverManager.getConnection("jdbc:odbc:studdsn");`. Afterward, create a statement and execute a query, for example, `Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from stud");`. Iterate through the ResultSet to retrieve the records .

The primary difference between using GET and POST in servlets is that GET appends form data to the URL, which can be seen in the browser's address bar, making it less secure and suitable for short, non-sensitive data. POST sends data in the request body, keeping it hidden from the user, accommodating larger data sizes, which makes it suitable for sensitive information. In servlets, the `doGet` method retrieves GET request params, while `doPost` is used for POST requests, both employing `req.getParameter("paramName")` to access input data .

To navigate through a ResultSet in a scrollable GUI in Java, you can use methods like `rs.first()`, `rs.last()`, `rs.previous()`, and `rs.next()`. These methods are available when you create a Statement with `ResultSet.TYPE_SCROLL_INSENSITIVE` and `ResultSet.CONCUR_UPDATABLE`. The `rs.first()` and `rs.last()` methods move the cursor to the first and last rows, respectively. The `rs.next()` method moves the cursor forward one row, and `rs.previous()` moves it backward one row. These methods allow the GUI to dynamically display records as users navigate through them using buttons .

To update a record in a database using JSP, first create an HTML form to collect input data. Use the `GET` or `POST` method to send the data to a JSP page. In the JSP page, load the database driver, establish a connection, and prepare a SQL `UPDATE` statement with a `PreparedStatement`. Use `setString` and `setInt` methods to bind parameters to your SQL statement, execute the update with `executeUpdate`, and manage exceptions with try-catch blocks. Confirm the update and close the Statement and Connection to free resources .

ResultSetMetaData can be obtained from a ResultSet object using `rs.getMetaData()`. It provides information about the types and properties of the columns in a ResultSet. You can use methods like `getColumnCount()`, `getColumnName(int column)`, and `getColumnTypeName(int column)` to access metadata and dynamically adapt to tables whose structure might not be known at compile time. This feature is particularly useful in applications that need to process various tables with unknown schema .

In a Java program interfacing with a database, SQL exceptions are handled using try-catch blocks. When executing SQL operations, wrap the code in a `try` block and catch specific `SQLException` exceptions in a `catch` block. This allows you to capture and respond to most errors that arise from database operations, such as connectivity issues or query errors. Additional optional `catch` blocks for catching generic `Exception` types can be employed for other unforeseen errors .

The <%@ include %> directive in JSP is used to include a file during the translation phase of the JSP lifecycle. It is static and includes the specified file into the current JSP page, meaning changes to the included file will reflect every time the main page is recompiled. It is useful for including headers, footers, or any common code across multiple pages, boosting maintainability and modularity of code .

To create a Java servlet that handles HTTP POST requests for a login form, you would begin by extending the `HttpServlet` class and overriding the `doPost` method. In this method, you retrieve form data parameters using `req.getParameter("paramName")`, process them as needed, and write a response using `HttpServletResponse` object methods. Also, ensure the form in the HTML uses `method=POST` in its `<form>` tag pointing to the servlet URL. The web.xml file should contain mappings for this servlet under `<servlet>` and `<servlet-mapping>` elements .

You might also like