Sie sind auf Seite 1von 7

Experiment No:8

Creating simple application to access database using JDBC formatting HTML with CSS

Aim:

To create an application to access database using JDBC formatting HTML with CSS.

Description:

This document describes how to create a simple web application that connects to a MySQL
database server. It also covers some basic ideas and technologies in web development, such as JavaServer
Pages (JSP), JavaServer Pages Standard Tag Library (JSTL), the Java Database Connectivity (JDBC)
API, and two-tier, client-server architecture. This tutorial is designed for beginners who have a basic
understanding of web development and are looking to apply their knowledge using a MySQL database.
MySQL is a popular open source database management system commonly used in web applications due
to its speed, flexibility and reliability. MySQL employs SQL, or Structured Query Language, for
accessing and processing data contained in databases.
This tutorial is a continuation from the Connecting to a MySQL Database tutorial and assumes
that you have already created a MySQL database named MyNewDatabase, which you have registered a
connection for in the NetBeans IDE. The table data used in that tutorial is contained in ifpwafcad.sql and
is also required for this tutorial. This SQL file creates two tables, Subject and Counselor, then populates
them with sample data. If needed, save this file to your computer, then open it in the NetBeans IDE and
run it on the MySQL database named MyNewDatabase.

Program:

HTML Program:

<html>
<head><title>database test </title></head>
<body bgcolor=lightgreen>
<center>
<h1>Online examination</h1>
</center>
<form>
<table>
<tr>
<td><b> seat number:</b></td>
</tr>
<tr>
<td><b>Name:</b></td>
</tr>
</table>
</hr>

<b>1.Apache is an open source web server</b><br/>


<input type="radio" name="group1" value="true"> true
<br>
<input type="radio" name="group1" value="false"> false <br>
<br/><br/>

<b>1.Apache is an open source web server</b><br/>


<input type="radio" name="group1" value="true"> true
<br>
<input type="radio" name="group1" value="false"> false <br>
<br/><br/>

<b>2.In modern pc there is no cache memory</b><br/>


<input type="radio" name="group2" value="true"> true
<br>
<input type="radio" name="group2" value="false"> false <br>
<br/><br/>

<b>3.time berner lee is the originator of java</b><br/>


<input type="radio" name="group3" value="true"> true
<br>
<input type="radio" name="group3" value="false"> false <br>
<br/><br/>

<b>4.JPG is not a video file extension</b><br/>


<input type="radio" name="group4" value="true"> true
<br>
<input type="radio" name="group4" value="false"> false <br>
<br/><br/>

<b>5.http is a statement protocol</b><br/>


<input type="radio" name="group5" value="true"> true
<br>
<input type="radio" name="group5" value="false"> false <br>
<br/><br/>

<br><br><br>
<center>
<input type=”submit” value=”submit”>
<input type=”reset” value=”clear”><br><br>
</center>
</form>
</body>
</html>

Servlet:

import java.io.*;
importjava.sql.*;
importjavax.servlet.*;
importjava.util.*;
Public class StudentsServlet3 extends HttpServlet
{
String Seat_no,Name;
String ans1,ans2,ans3,ans4,ans5;
Int a1,a2,a3,a4,a5;
Connection connect;
Statement stmt=null;
ResultSetrs=null;
Public void doPost(HttpServletRequestrequest,HttpServletResponse response) throws
ServletException, IOexception
{
Try
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
String url=”jdbc:odbc:StudentDB1”;
Connect=DriverManager.getConnection(url,” “,””);
}
Catch(ClassNotFoundException e){E.printStackTrace();}
Catch(SQLException e){E.printStackTrace();}
Catch(Exception e){E.printStackTrace();}
Seat_no=request.getParametr(“Seat_no”);
Name=request.getParametr(“Name”);
ans1=request.getParametr(“group1”);
if(ans1.equals(“true”))
a1=2;
else
a1=0;
ans2=request.getParameter(“group2”);
if(ans2.equals(“true”))
a2=0;
else
a2=0;
ans2=request.getParameter(“group2”);
if(ans3.equals(“true”))
a3=2;
else
a3=0;
ans3=request.getParameter(“group2”);

if(ans4.equals(“true”))
a4=2;
else
a4=0;
ans4=request.getParameter(“group2”);

if(ans5.equals(“true”))
a5=0;
else
a5=2;
int Total=a1+a2+a3+a4+a5;
try
{
Statement stmt=connect.creatStatement();
String query=”INSERT INTO StudentTable(“’ +”’Seat_no,Name,Marks”+ “)
VALUES (“ +Seat_no + “’ ,”’ +total + “’)”;
Int result=stmt.executeUpdate(query);
Stmt.close();
}Catch(SQLException e) {}

Response.setContentType(“text/html”);
PrintWriter out=response.getWriter();
Out.println(“<html>”);
Out.println(“<head>”);
Out.println(“</head>”);
Out.println(“<body bgcolor=pink>”);
Out.println(“<center>”);
Out.println(“<br><br>”);
Out.println(“<h2>Students Datebase</h2>”);
Out.println(“<table border =5>”);
Try
{
Statement stmt=connect.createStatement();
String query=”SELECT*FROM StudentTable”;
Rs=stmt.executeQuery(query);
Out.println(“<th>” + ” Seat_no” + “</th>”);
Out.println(“<th>” + ” Name”+ “</th>”);
Out.println(“<th>” + ” Marks” + “</th>”);
While(rs.next())
{
Out.println(“<tr>”);
Out.println(“<td>” + ” rs.getInt(1)” + “</td>”);
Out.println(“<td>” + ” rs.getString(2)” + “</td>”);
Out.println(“<td>” + ” rs.getString(3)” + “</td>”);
Out.println(“</tr>”);
}
Out.Println(“</table”>);
}
Catch(SQLException e){}
Finally
{
Try
{
If(rs!=null)
rs.close();
If(stmt!=null)
stmt.close();
if(connect!=null)
connect.close();
}
Catch(SQLException e) { }
}
Out.println(“<center>”);
Out.println(“<h1>Thanks</h1>\n”);
Out.println(“</center>”);
Out.println(“</body></html>”);
}
}

Output:
Conclusion:

In this experiment, simple application to access database using JDBC formatting HTML
with CSS was implemented and the output was verified successfully.

Das könnte Ihnen auch gefallen