Servlet (doPost or doGet) to access Oracle DB
We have the following Servlet and trying to see the easiest method to connect to an Oracle DB and get some values based on the SQL statement. I am building the servlet in Jdev 10g (10.1.3) and deploying to OAS 10.1.2.3. I know, we need to move to 11G. We are very eager to move as soon as we finish this project...
If anyone has a good example of a Servlet, I greatly appreciate it.
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.print("<html><head>");
out.print("</head><body>");
out.print("<code><pre>");
out.print("<font color=green>ID\tFirst ");
out.println("Name\tLast Name\n</font>");
// debugging info
long time1 = System.currentTimeMillis();
// connecting to database
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@machine.domain:1521:dbinstant","user","password");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM table");
// 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");
}
Thanks so much
KA