0% found this document useful (0 votes)
124 views35 pages

Advance Lab

1. The Java program demonstrates multithreading by creating three ThreadDemo objects with different thread names ("Thread 1", "Thread 2", "Thread 3"). Each thread executes a yield loop 3 times and prints the current thread name. 2. The two Java programs demonstrate communication between programs using sockets and datagrams. The ChatClient program accepts user input and sends it to the ChatServer program, which prints the received messages and waits for input to send back to the client. 3. The Java Servlet program implements the get and post methods to demonstrate form submission and parameter passing. It checks login credentials on submit and displays the result. 4. The Java JSP program implements user login verification by checking

Uploaded by

Amit Kumar
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)
124 views35 pages

Advance Lab

1. The Java program demonstrates multithreading by creating three ThreadDemo objects with different thread names ("Thread 1", "Thread 2", "Thread 3"). Each thread executes a yield loop 3 times and prints the current thread name. 2. The two Java programs demonstrate communication between programs using sockets and datagrams. The ChatClient program accepts user input and sends it to the ChatServer program, which prints the received messages and waits for input to send back to the client. 3. The Java Servlet program implements the get and post methods to demonstrate form submission and parameter passing. It checks login credentials on submit and displays the result. 4. The Java JSP program implements user login verification by checking

Uploaded by

Amit Kumar
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
You are on page 1/ 35

1 . Write a JAVA program demonstrating multithreading.

CODE:
import java.io.*;

import java.net.*;

public class ThreadDemo implements Runnable

Thread t;

ThreadDemo(String str)

t = new Thread(this, str);

t.start();

public void run()

for (int i = 1; i <= 3; i++)

System.out.println(Thread.currentThread().getName() + "Executing.");

Thread.yield();

System.out.println(Thread.currentThread().getName()+"Finished Executing.");

public static void main(String args[ ]) {

new ThreadDemo("Thread 1");

new ThreadDemo("Thread 2");

new ThreadDemo("Thread 3");

OUTPUT:
2 . Write a set of two JAVA programs for communicating between them using
socket & datagram programming.
CODE:
import java.io.*;

import java.net.*;

public class ChatClient

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

Socket sock = new Socket("127.0.0.1", 3000);

// reading from keyboard (keyRead object)

BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));

// sending to client (pwrite object)

OutputStream ostream = sock.getOutputStream();

PrintWriter pwrite = new PrintWriter(ostream, true);

// receiving from server ( receiveRead object)

InputStream istream = sock.getInputStream();

BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));


System.out.println("Start the chitchat, type and press Enter key");

String receiveMessage, sendMessage;

while(true)

sendMessage = keyRead.readLine(); // keyboard reading

pwrite.println(sendMessage); // sending to server

pwrite.flush(); // flush the data

if((receiveMessage = receiveRead.readLine()) != null) //receive from server

System.out.println(receiveMessage); // displaying at DOS prompt

import java.io.*;

import java.net.*;

public class ChatServer

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

ServerSocket sersock = new ServerSocket(3000);

System.out.println("Server ready for chatting");

Socket sock = sersock.accept( );

// reading from keyboard (keyRead object)

BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));

// sending to client (pwrite object)

OutputStream ostream = sock.getOutputStream();

PrintWriter pwrite = new PrintWriter(ostream, true);


// receiving from server ( receiveRead object)

InputStream istream = sock.getInputStream();

BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));

String receiveMessage, sendMessage;

while(true)

if((receiveMessage = receiveRead.readLine()) != null)

System.out.println(receiveMessage);

sendMessage = keyRead.readLine();

pwrite.println(sendMessage);

pwrite.flush();

OUTPUT:
3.Write a JAVA Servlet Program to implement and demonstrate get()
and post() methods(Using HTTP Servlet Class).
CODE:
FOR GET
<html>
<body>
<form action="login" method="get">
<table>
<tr>
<td>User</td>
<td><input name="user" /></td>
</tr>
<tr>
<td>password</td>
<td><input name="password" /></td>
</tr>
</table>
<input type="submit" />
</form>
</body>
</html>

package com.edu4java.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {


@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String user = req.getParameter("user");
String pass = req.getParameter("password");
if ("edu4java".equals(user) && "eli4java".equals(pass)) {
response(resp, "login ok");
} else {
response(resp, "invalid login");
}
}

private void response(HttpServletResponse resp, String msg)


throws IOException {
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<t1>" + msg + "</t1>");
out.println("</body>");
out.println("</html>");
}
}
<web-app>
<servlet>
<servlet-name>timeservlet</servlet-name>
<servlet-class>com.edu4java.servlets.FirstServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>login-servlet</servlet-name>
<servlet-class>com.edu4java.servlets.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>timeservlet</servlet-name>
<url-pattern>/what-time-is-it</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>login-servlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>

FOR POST
<html>
<body>
<form action="login" method="post">
<table>
<tr>
<td>User</td>
<td><input name="user" /></td>
</tr>
<tr>
<td>password</td>
<td><input name="password" /></td>
</tr>
</table>
<input type="submit" />
</form>
</body>
</html>
package com.edu4java.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {


@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String user = req.getParameter("user");
String pass = req.getParameter("password");
if ("edu4java".equals(user) && "eli4java".equals(pass)) {
response(resp, "login ok");
} else {
response(resp, "invalid login");
}
}

private void response(HttpServletResponse resp, String msg)


throws IOException {
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<t1>" + msg + "</t1>");
out.println("</body>");
out.println("</html>");
}
}

4 .Write a JAVA JSP Program to implement verification of a particular user login


and display a Welcome page.
CODE:

Login page
<!DOCTYPE html>

<html>
<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

Sample login Example (try with username as "admin" and password as "admin" without quart ) <br> <br>

<form action="LoginController" method="post">

Enter username :<input type="text" name="username"> <br>

Enter password :<input type="password" name="password"><br>

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

</form>

</body>

</html>

web.xml mapping
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>login</display-name>
<servlet>
<description></description>
<display-name>LoginController</display-name>
<servlet-name>LoginController</servlet-name>
<servlet-class>com.candidjava.LoginController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginController</servlet-name>
<url-pattern>/LoginController</url-pattern>
</servlet-mapping>

</web-app>

Login controller
package com.candidjava;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* Servlet implementation class LoginController

*/

public class LoginController extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,


IOException {

String un=request.getParameter("username");

String pw=request.getParameter("password");

if(un.equals("admin") && pw.equals("admin"))

response.sendRedirect("success.html");

return;

else

response.sendRedirect("error.html");

return;

}
success page
<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

Login success

WELCOME

</body>

</html>

error page

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

Invalid username or password

</body>

</html>

OUTPUT:
5 . Write a JDBC Program to insert data into Student DATABASE and
retrieve info based on particular queries.
CODE:
Create a Table:
CREATE TABLE studid(

id varchar2(30) NOT NULL PRIMARY KEY,


pwd varchar2(30) NOT NULL,

fullname varchar2(50),

email varchar2(50)

);

Connecting to the Database


import java.sql.*;

public class connect


{
public static void main(String args[])
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");

// Establishing Connection
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl", "login1", "pwd1");

if (con != null)
System.out.println("Connected");
else
System.out.println("Not Connected");

con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Output :
Connected

Implementing Insert Statement


import java.sql.*;

public class insert1


{
public static void main(String args[])
{
String id = "id1";
String pwd = "pwd1";
String fullname = "geeks for geeks";
String email = "[email protected]";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("
jdbc:oracle:thin:@localhost:1521:orcl", "login1", "pwd1");
Statement stmt = con.createStatement();

// Inserting data in database


String q1 = "insert into studid values('" +id+ "', '" +pwd+
"', '" +fullname+ "', '" +email+ "')";
int x = stmt.executeUpdate(q1);
if (x > 0)
System.out.println("Successfully Inserted");
else
System.out.println("Insert Failed");

con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Output :
Successfully Registered

Implementing Update Statement


import java.sql.*;

public class update1


{
public static void main(String args[])
{
String id = "id1";
String pwd = "pwd1";
String newPwd = "newpwd";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("
jdbc:oracle:thin:@localhost:1521:orcl", "login1", "pwd1");
Statement stmt = con.createStatement();

// Updating database
String q1 = "UPDATE studid set pwd = '" + newPwd +
"' WHERE id = '" +id+ "' AND pwd = '" + pwd + "'";
int x = stmt.executeUpdate(q1);

if (x > 0)
System.out.println("Password Successfully Updated");
else
System.out.println("ERROR OCCURED :(");

con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Output :
Password Successfully Updated

Implementing Delete Statement


import java.sql.*;

public class delete


{
public static void main(String args[])
{
String id = "id2";
String pwd = "pwd2";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("
jdbc:oracle:thin:@localhost:1521:orcl", "login1", "pwd1");
Statement stmt = con.createStatement();

// Deleting from database


String q1 = "DELETE from studid WHERE id = '" + id +
"' AND pwd = '" + pwd + "'";

int x = stmt.executeUpdate(q1);

if (x > 0)
System.out.println("One User Successfully Deleted");
else
System.out.println("ERROR OCCURED :(");

con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Output :
One User Successfully Deleted

Implementing Select Statement


import java.sql.*;

public class select


{
public static void main(String args[])
{
String id = "id1";
String pwd = "pwd1";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("
jdbc:oracle:thin:@localhost:1521:orcl", "login1", "pwd1");
Statement stmt = con.createStatement();

// SELECT query
String q1 = "select * from studid WHERE id = '" + id +
"' AND pwd = '" + pwd + "'";
ResultSet rs = stmt.executeQuery(q1);
if (rs.next())
{
System.out.println("User-Id : " + rs.getString(1));
System.out.println("Full Name :" + rs.getString(3));
System.out.println("E-mail :" + rs.getString(4));
}
else
{
System.out.println("No such user id is already registered");
}
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Output :
User-Id : id1

Full Name : geeks for geeks

E-mail :[email protected]

6 . Write a JSP program to read data from a DATABASE.


CODE:
First off all We have a create table in MySQL

CREATE TABLE `record` (


`id` varchar(15) NOT NULL,
`user_id` varchar(30) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

Then reading data…


<%@page import="java.sql.DriverManager"%>

<%@page import="java.sql.ResultSet"%>

<%@page import="java.sql.Statement"%>

<%@page import="java.sql.Connection"%>

<%

String id = request.getParameter("studid");

String driverName = "com.mysql.jdbc.Driver";

String connectionUrl = "jdbc:mysql://localhost:3306/";

String dbName = "jsptutorials";

String studid = "root";

String password = "root";

try {

Class.forName(driverName);

} catch (ClassNotFoundException e) {

e.printStackTrace();

Connection connection = null;

Statement statement = null;

ResultSet resultSet = null;


%>

<h2 align="center"><font><strong>Retrieve data from database in jsp</strong></font></h2>

<table align="center" cellpadding="5" cellspacing="5" border="1">

<tr>

</tr>

<tr bgcolor="#A52A2A">

<td><b>id</b></td>

<td><b>user_id</b></td>

<td><b>Password</b></td>

<td><b>Name</b></td>

<td><b>Email</b></td>

</tr>

<%

try{

connection = DriverManager.getConnection(connectionUrl+dbName, studid, password);

statement=connection.createStatement();

String sql ="SELECT * FROM record";

resultSet = statement.executeQuery(sql);

while(resultSet.next()){

%>

<tr bgcolor="#DEB887">

<td><%=resultSet.getString("id") %></td>

<td><%=resultSet.getString("user_id") %></td>

<td><%=resultSet.getString("password") %></td>

<td><%=resultSet.getString("name") %></td>

<td><%=resultSet.getString("email") %></td>

</tr>
<%

} catch (Exception e) {

e.printStackTrace();

%>

</table>

OUTPUT:

7 . Write a set of JAVA programs to implement Remote method invocation.


CODE:

Interface Program (rint.java)

import java.rmi.*;
public interface rint extends Remote // rint is the interface name
{
double fact(double x) throws RemoteException; // fact() is the method declaration only
}

Implementation Program (rimp.java)

import java.rmi.*;

import java.rmi.server.*;

public class rimp extends UnicastRemoteObject implements rint // rimp is implementation name
{

public rimp() throws RemoteException // rimp() is constructor

{}

public double fact(double x) throws RemoteException // fact() method definition

if (x <= 1) return (1);

else return (x * fact(x - 1));

Server Program (rser.java)

import java.rmi.*;

import java.net.*;

public class rser // rser is server name

public static void main(String arg[]) {

try {

rimp ri = new rimp();

Naming.rebind("rser", ri);

} catch (Exception e) {

System.out.println(e);

Client Program (rcli.java)

import java.rmi.*;

public class rcli // rcli is client name

public static void main(String arg[]) {

try {

rint rr = (rint) Naming.lookup("rmi://172.16.13.2/rser"); // system IP address

double s = rr.fact(5);

System.out.println("Factorial value is… : " + s);


} catch (Exception e) {

System.out.println(e);

OUTPUT:
8 . Develop a JAVA SWING program to design a calculator.
CODE:
package ptunes;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.Scanner;

import javax.script.ScriptEngineManager;

import javax.script.ScriptEngine;

import javax.script.ScriptException;

//import java.util.ActionEvent;

public class gui implements ActionListener {

public gui() {

public void actionPerformed (ActionEvent ae ){


// JOptionPane.showMessageDialog(ìHello is pressedî);

public static void main(String[] args) {

JFrame j = new JFrame("Buttons");

Container c = j.getContentPane();

//c.setLayout(new BorderLayout());

JPanel p1 = new JPanel();

p1.setLayout(new BorderLayout());

p1.setLayout(new GridLayout(4,4,4,4));

final JTextField t = new JTextField(100);

Font myFontSize = t.getFont().deriveFont(Font.BOLD,50f);

t.setFont(myFontSize);

c.add(t,BorderLayout.NORTH);

final JButton n1 = new JButton("1");

n1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

String num1 = n1.getText();

String global = t.getText();

global = global.concat(num1);

t.setText(global);

});

final JButton n2 = new JButton("2");

n2.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

String num1 = n2.getText();

String global = t.getText();

global = global.concat(num1);

t.setText(global);

}
});

final JButton n3 = new JButton("3");

n3.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

String num1 = n3.getText();

String global = t.getText();

global = global.concat(num1);

t.setText(global);

});

final JButton n4 = new JButton("4");

n4.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

String num1 = n4.getText();

String global = t.getText();

global = global.concat(num1);

t.setText(global);

});

final JButton n5 = new JButton("5");

n5.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

String num1 = n5.getText();

String global = t.getText();

global = global.concat(num1);

t.setText(global);

});

final JButton n6 = new JButton("6");


n6.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

String num1 = n6.getText();

String global = t.getText();

global = global.concat(num1);

t.setText(global);

});

final JButton n7 = new JButton("7");

n7.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

String num1 = n7.getText();

String global = t.getText();

global = global.concat(num1);

t.setText(global);

});

final JButton n8 = new JButton("8");

n8.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

String num1 = n8.getText();

String global = t.getText();

global = global.concat(num1);

t.setText(global);

});

final JButton n9 = new JButton("9");

n9.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)


{

String num1 = n9.getText();

String global = t.getText();

global = global.concat(num1);

t.setText(global);

});

final JButton n10 = new JButton("0");

n10.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

String num1 = n10.getText();

String global = t.getText();

global = global.concat(num1);

t.setText(global);

});

final JButton n11 = new JButton("+");

n11.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

String num1 = n11.getText();

String global = t.getText();

global = global.concat(num1);

t.setText(global);

});

final JButton n12 = new JButton("-");

n12.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

String num1 = n12.getText();


String global = t.getText();

global = global.concat(num1);

t.setText(global);

});

final JButton n13 = new JButton("*");

n13.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

String num1 = n13.getText();

String global = t.getText();

global = global.concat(num1);

t.setText(global);

});

final JButton n14 = new JButton("/");

n14.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

String num1 = n14.getText();

String global = t.getText();

global = global.concat(num1);

t.setText(global);

});

final JButton n15 = new JButton("=");

n15.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

//String num1 = n15.getText();

String global = t.getText();

//global = global.concat(num1);
ScriptEngineManager mgr = new ScriptEngineManager();

ScriptEngine engine = mgr.getEngineByName("JavaScript");

try {

String s = engine.eval(global).toString();

t.setText(s);

} catch (ScriptException e1) {

e1.printStackTrace();

});

final JButton n16 = new JButton("C");

n16.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

//String num1 = n16.getText();

String global = t.getText();

global = null;

t.setText(global);

});

p1.add(n1);

p1.add(n2);

p1.add(n3);

p1.add(n4);

p1.add(n5);

p1.add(n6);

p1.add(n7);

p1.add(n8);

p1.add(n9);

p1.add(n10);

p1.add(n11);

p1.add(n12);
p1.add(n13);

p1.add(n14);

p1.add(n15);

p1.add(n16);

c.add(p1,BorderLayout.CENTER);

j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

j.setSize(400,400);

j.setVisible(true);

OUTPUT:

9 . Using JSP develop a project to implement ONLINE EXAMINATION


SYSTEM.
CODE:
index.jsp

<html>

<head>

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

<title>Welcome to Online Examination</title>


</head>

<body bgcolor="cyan">

<form name="index" action="exam.jsp" method="post">

<center><h1><span><font color="red">Welcome to Online Examination</font></span></h1>

<br>

<h2><u><span><font color="blue">Instructions to the Candidates</font></span></u></h2>

<br><h3><ol><li>Fill the correct Registration number.</li>

<br><li>Enter your name.</li>

<br><li>Read the questions carefully.</li>

<br><li>No negative marking.</li></ol></h3>

<br>

<b>Enter your Register number</b>

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

<b>Enter your Name</b>

<input type="text" name="txt_name"><br><br>

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

</center>

</form>

</body>

</html>

exam.jsp

<html>

<head>

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

<title>Examination Panel</title>

</head>

<body bgcolor="cyan">

<%@ page language="java" %>


<%@ page import ="java.sql.*" %>

<%

String reg= request.getParameter("txt_reg");

String name = request.getParameter("txt_name");

out.println("<h2>Welcome" name "...Your Register number is " reg "!!</h2><br><br><br>");

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String sTable = "exam";

String sSql = "SELECT * FROM " sTable "";

String sDBQ = "C:/Users/A/Documents/NetBeansProjects/Online Examination/exam.mdb";

String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" sDBQ ";DriverID=22;READONLY=true";

Connection cn = null;

Statement st = null;

ResultSet rs = null;

try {

cn = DriverManager.getConnection( database ,"","");

st = cn.createStatement();

rs = st.executeQuery( sSql );

ResultSetMetaData rsmd = rs.getMetaData();

String s1,s2,s3,s4;

int i=1;

while(rs.next())

out.println("<form name='exam' action='report.jsp' method='post'><b>" i " . " rs.getString(1) "</b><br><br>");

s1 = rs.getString(2);

s2 = rs.getString(3);

s3 = rs.getString(4);

s4 = rs.getString(5);

out.println("<input type=radio name=opt" i " value=" s1 " CHECKED>" s1 " <br><br>");

out.println("<input type=radio name=opt" i " value=" s2 ">" s2 "<br><br>");

out.println("<input type=radio name=opt" i " value=" s3 ">" s3 "<br><br>");


out.println("<input type=radio name=opt" i " value=" s4 ">" s4 "<br><br>");

i ;

out.println("<input name ='submit' value='Submit' type='submit'/>");

/*int n = rsmd.getColumnCount();

out.println( "<table border=1 cellspacing=3><tr>" );

for( int i=1; i<=n; i ) // Achtung: erste Spalte mit 1 statt 0

out.println( "<th>" rsmd.getColumnName( i ) "</th>" );

while( rs.next() )

out.println( "</tr><tr>" );

for( int i=1; i<=n; i ) // Achtung: erste Spalte mit 1 statt 0

out.println( "<td nowrap>" rs.getString( i ) "</td>" );

out.println( "</tr></table>" );*/

finally {

try { if( null != rs ) rs.close(); } catch( Exception ex ) {}

try { if( null != st ) st.close(); } catch( Exception ex ) {}

try { if( null != cn ) cn.close(); } catch( Exception ex ) {}

%>

</body>

</html>

report.jsp

<html>

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

<title>Exam Report</title>

</head>

<body bgcolor="cyan">

<center><h1>Your Report Card</h1></center>

<%@ page language="java" %>

<%@ page import ="java.sql.*" %>

<%

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String sTable = "exam";

String sSql = "SELECT * FROM " sTable "";

String sDBQ = "C:/Users/A/Documents/NetBeansProjects/Online Examination/exam.mdb";

String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" sDBQ ";DriverID=22;READONLY=true";

Connection cn = null;

Statement st = null;

ResultSet rs = null;

try {

cn = DriverManager.getConnection( database ,"","");

st = cn.createStatement();

rs = st.executeQuery( sSql );

ResultSetMetaData rsmd = rs.getMetaData();

String s1,s2,s3,s4;

int i=1;

int correct=0,incorrect=0,total=0;

out.println("<h2><br><br><center><table border=1 cellpadding=2 cellspacing=2><tr><th>Question</th><th>Your


Answer</th><th>Correct Answer</th><th>Status</th></tr>");

while(rs.next())

total ;

s1 = rs.getString(1);

s2 = request.getParameter("opt" i);
s3 = rs.getString(6);

if(s2.equals(s3))

s4="Correct";

correct ;

else

s4="Incorrect";

incorrect ;

out.println("<tr><td>" s1 "</td><td>" s2 "</td><td>" s3 "</td><td>" s4 "</td></tr>");

i ;

out.println("</table><br><br><table><b><tr><td>Correct Answers</td><td>" correct "</td></tr>");

out.println("<tr><td>Incorrect Answers</td><td>" incorrect "</td></tr>");

out.println("<tr><td>Total Questions</td><td>" total "</td></tr></table></b></center></h2>");

finally {

try { if( null != rs ) rs.close(); } catch( Exception ex ) {}

try { if( null != st ) st.close(); } catch( Exception ex ) {}

try { if( null != cn ) cn.close(); } catch( Exception ex ) {}

%>

</body>

</html>

OUTPUT:

You might also like