//Displays the enquiries, taking the data from two separate tables //with a join on the person id. //this data was input to database by web form html, input by customer //and then transported by servlet using jdbc to mysql database //This merely retrieves from the two tables and displays the //enquiry to the website administrator import java.sql.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class DisplayServlet3 extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.print("
"); out.print(""); out.print(""); out.print(""); out.close(); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.print(""); out.print(""); out.print("");
out.print("ID\tFirst ");
out.println("Name\tLast Name\n");
// debugging info
long time1 = System.currentTimeMillis();
// connecting to database
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("org.gjt.mm.mysql.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/databasenameRemoved","usernameRemoved","passwordRemoved");
String textValue=req.getParameter("myvalue");
stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO TBLPAGES VALUES(,,textValue,,)");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM TBLPAGES");
// displaying records
while(rs.next()) {
out.print(rs.getObject(1).toString());
out.print("\t");
out.print(rs.getObject(2).toString());
out.print("\t\t");
out.print(rs.getObject(3).toString());
out.print("\n");
}
} catch (SQLException e) {
throw new
ServletException("Servlet Could not display records.", e);
} catch (ClassNotFoundException e) {
throw new
ServletException("JDBC Driver not found.", e);
} finally {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(con != null) {
con.close();
con = null;
}
} catch (SQLException e) {}
}
// debugging info
long time2 = System.currentTimeMillis();
out.print("");
out.print("Search took : "); out.print( (time2 - time1) ); out.print(" ms.
"); out.print(""); out.print(""); out.close(); } }