Java implements JSP methods to connect to Oracle databases in servlets

  • 2020-04-01 03:24:40
  • OfStack

This article illustrates how a Java implementation JSP USES servlets to connect to an Oracle database. In order to connect to a database in a Servlet, you must write a Servlet class program. Placing the Servlet class under the servlets or classes directory of the WEB server, you need to create an HTML document that sends the Servlet request in order to invoke the Servlet. This example USES the (Driver) class.forname (driverName).newinstance () method 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. In the doGet() method, establish a connection to the database and execute the query:


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/ndb@192.168.1.6: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. The DisplayResult() method displays the query result:


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 the JDBC class, the servlet class, and the console output, the following package needs to be introduced:


import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

5. Compile database.java, generate the database.class file, and put the database.class into the servlets directory of the WEB Server. In this example, Java WEB Server is used as the WEB Server. Configure the WEB server and add database.class, specifying the name database.

6. Write the database.html file that invokes the Servlet. The code is as follows:


<html>
<head>
<title>Jsp use Servlet Connect to database </title>
</head>
<body>
<center>
<form action="/servlet/database" method="get">
<input name="action" type="submit" value=" Connect to database ">
</form>
</center>
</body>
</html>

Related articles: