Hi, I'm trying my very best to include Java Utilites bean inside my application but it is just not working. Hope the expert here can give me some advice.
Thanks.
My code for the package is name adroit.
And here's the jsp file which I hope to reduce the code by inserting that java bean utilities.
Hope to hear the solution soon. Thanks.
Thanks.
My code for the package is name adroit.
Code:
package adroit;
import java.sql.*;
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;
import java.sql.*;
import java.util.Enumeration;
import javax.servlet.http.HttpSession;
public class OrderDB {
Connection conn;
public OrderDB() throws Exception{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:XYZODBC");
}
public ResultSet query(String query) throws Exception {
// method insert data into the database
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
System.out.println(query+ ":" + rs);
return rs;
}
public void close() throws Exception {
conn.close();
} // close Connection
}
Code:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@page language="java" import ="java.sql.*" %>
<%
String access = (String) session.getAttribute("access");
if (access == null) {
response.sendRedirect("login.html");
}%>
<%-- Display page --%>
<% Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Statement stmt = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:XYZODBC";
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
} catch (Exception e) {
System.out.println(e.getMessage());
}
rs = stmt.executeQuery("Select * from ABC");
%>
<html>
<head>
<link rel="stylesheet" style type ="text/css" href ="style.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<tr><td><b>ID</b></td>
<td><b>Name</b></td>
<td><b>NRIC</b></td>
</tr>
<% int no = 1;
while (rs.next()) {
int id = rs.getInt("ID");
%>