JDBC and servlets

<!-- Simple program to demonstrate JDBC access -->
<!-- Form to submit Department name to server -->
<!-- html forms -->

<html>

<head>
<title>HTML Forms and Servlets</title>

<style>
label
{
font-family:vivaldi;
font-size:x-large;
letter-spacing:2px;
line-height:40px;
}
</style>
</head>

<body>
<form method="GET" action="JDBC_DC">
<center>

<h2>HTML, Servlets and Database</h2></br>

<label>Enter Department Name  </label>
<input type="text" Name="DN" tabindex=1/>

</br> </br>

<button class="btn" type="button " >Get Code!!! </button>

</center>
</form>

</body>

</html>




// Servlet to access form data and generate response to client browser

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.util.*;
import java.lang.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;

public class JDBC_DC extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{

//setting the content type
res.setContentType("text/html");

//create the stream to write the data
PrintWriter pw=res.getWriter();

String D = req.getParameter("DN").trim();


//writing html in the stream
pw.println("<html><body><h2>");
pw.println("<center>");
pw.println("Dept = "+D+"<br/>");
try
{

Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XEPDB1", "system", "Harini97#");

String qry = "SELECT * FROM DC WHERE name ='"+ D+"'";
PreparedStatement ps = conn.prepareStatement(qry);
ResultSet rs = ps.executeQuery();
while(rs.next())
pw.println("Code = "+rs.getString(2)+"<br/>");

}
catch (Exception e) {
            e.printStackTrace();
        }

pw.println("</center>");
pw.println("</h2></body></html>");

//closing the stream
pw.close();

}
}



No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu