Java implementation JSP method to connect Oracle database in Servelt _java

Source: Internet
Author: User
Tags rowcount stmt java web stringbuffer

This article illustrates the Java implementation JSP's approach to using Servelt to connect Oracle databases. In order to connect a database in a servlet, you must write a servlet class program. To place the Servlet class program under the Servlets or classes directory of the Web server, to invoke the servlet, you need to create an HTML document that sends a servlet request. This example uses the (Driver) class.forname (drivername). Newinstance () method to load the driver and establish a connection to the database.

The specific program code is:

1. The Database class inherits the HttpServlet class, and there are two methods: Doget () and Displayresult (), the following code:

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 {P
Rintwriter 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 results of the query:

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 < m)
{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, Servlet class, and console output, the following package needs to be introduced:

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

5. Compiling Database.java, generating database.class files, placing Database.class in the Servlets directory of the Web server, this example uses Java Web server as the Web servers. Configure the Web server, add Database.class, and specify a name of database.

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

 
 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.