Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > J2EE
|
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 26th, 2004, 07:24 AM
Registered User
 
Join Date: Jun 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Servlet Connectivity with database????

I have written one program to connect to database(orcle 9i) using servlets. Thereis no compilation error in the program but still the programme does not retrieve any valus from the database. Can anyone tell me whats the problem with the following code. please ignore the username and password in the programme.

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

public class DBConnServlet extends HttpServlet{

Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String passwd = " ";

public void doGet(HttpServletRequest req , HttpServletResponse res) throws ServletException , IOException{

res.setContentType("text/html");
PrintWriter out = res.getWriter();
connect();
out.println("<HTML><HEAD><TITLE>DBConnection</TITLE></HEAD>");
out.println("<BODY><BR>The password is : ");
out.println(passwd);
out.println("<BR></BODY></HTML>");

}
/* This method connects to the database*/
public void connect()
{
try
{
System.out.println("Just for debugging purposes");
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection("jdbc:oracle:MyConn","username","passwd");
ps = conn.prepareStatement("select * from sample where name=?");
ps.setString(1,"a");
rs = ps.executeQuery();
rs.next();
passwd = passwd + rs.getString(2);
System.out.println("The passwd is "+passwd);
ps.close();
conn.close();
}
catch( SQLException sql)
{
System.out.println("SQL Exception Caught");
}
}
}
in the above code MyConn is the DSN connection i created using DataSources from ControlPanel.

Krish
 
Old July 20th, 2004, 06:38 PM
Registered User
 
Join Date: Jul 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to jrlomboto
Default

If this program connect to the database and the database has data in it, why don't you try to debug by checking if the ResultSet had data retrieved from the database. I will suggest that you do the follwing:
while(rs.next())
{
   System.out.println("I got here, some data must have been retrieved. Debugging");
   passWord = rs.getString(2);
}
this can help you detect where the problem may be.

jeano
 
Old July 29th, 2004, 07:03 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 345
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to gokul_blr Send a message via Yahoo to gokul_blr
Default

you can try with

    Class.forName("oracle.jdbc.driver.OracleDriver");

instead of line :

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

and Don't forget to add ClassNotFoundException handler.

Gokul








Similar Threads
Thread Thread Starter Forum Replies Last Post
database connectivity MathLearner ASP.NET 1.0 and 1.1 Professional 1 January 3rd, 2008 09:09 AM
database connectivity singh.mandeep82 Struts 0 December 29th, 2007 05:22 PM
Database Connectivity pageturner71 Access 1 September 8th, 2004 05:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.