The example in this article describes how Java implements JSP to connect to Oracle database using Servelt. In order to connect to the database in a Servlet, a Servlet class program must be written. Place the Servlet class program under the servlets or classes directory of the WEB server. In order to call the Servlet, you need to create an HTML document that sends the Servlet request. In this example, the (Driver)Class.forName(driverName).newInstance() method is used to load the driver and establish a connection with the database.
The specific program code is:
1. The Database class inherits the HttpServlet class and has two methods: doGet() and displayResult(). The code is as follows:
public class Database extends HttpServlet{public void doGet(); public void displayResult(ResultSet results,PrintWriter out);}
2. Establish a connection to the database and execute the query in the doGet() method:
public void doGet(){HttpServletRequest request,HttpServletResponse response}throws ServletException, IOException{PrintWriter out;String title = "Simple Servlet connecting to Oracle DB";response.setContentType("text/html;charset=GB2312");out = response.getWriter();out.println("<HTML><HEAD><TITLE>");out.println(title);out.println("</TITLE></HEAD><BODY>");out .println("<H1>" + title + "</H1>");out.println("<P>This is output from SimpleServlet.");String driverName = "oracle.jdbc.driver.OracleDriver";Driver d;Connection con;Statement stmt;ResultSet results;try{d = (Driver)Class.forName(driverName).newInstance();con = DiverManager.getConnection("jdbc:oracle:thin:ndb/[email protected]:1521:PC6");stmt = con.createStatement();String sqlstr = "select * from data";results = stmt.executeQuery(sqlstr);displayResult(results,out);stmt.close();con.close();}catch (Exception e){out.println("error: " + e.toString());} out.println("</BODY></HTML>");out.close();}
3. DisplayResult() method displays query results:
public void displayResult(ResultSet results,PrintWriter out){StringBuffer buf = new StringBuffer();String temp;try{ResultSetMetaData rsmd = results.getMetaData();int numCols = rsmd.getColumnCount();int i, rowcount = 0;for (i=1; i <= numCols; i++){if (i > 1) buf.append(",");buf.append(rsmd.getColumnLabel(i));}buf.append("");while (results.next() && rowcount < 100){for (i= 1; i <= numCols; i++){if (i > 1) buf.append(",");buf.append((results.getString(i)));}buf.append("<br>");rowcount++;}out.println("<br>");out .println(buf.toString());results.close();}catch (Exception e){out.println("error: " + e.toString());return;}}
4. Because the program uses JDBC classes, servlet classes and uses console output, the following packages need to be introduced:
import java.sql.*;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;
5. Compile Database.java, generate Database.class file, and put Database.class in the servlets directory of the WEB server. In this example, Java Web Server is used as the WEB server. Configure the WEB server, add database.class, and specify the name as database.
6. Write the database.html file that calls the Servlet. The code is as follows:
<html><head><title>Jsp uses Servlet to connect to the database</title></head><body><center><form action="/servlet/database" method="get"><input name="action " type="submit" value="Connect to database"></form></center></body></html>