Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Servlet (doPost or doGet) to access Oracle DB

user59119Jan 12 2010 — edited Jan 13 2010
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 10 2010
Added on Jan 12 2010
11 comments
2,058 views