aspose file tools*
The moose likes JDBC and the fly likes tomcat with jdbc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "tomcat with jdbc" Watch "tomcat with jdbc" New topic
Author

tomcat with jdbc

kavitha vasu
Ranch Hand

Joined: Feb 22, 2007
Posts: 70
Please let me know Why my jsp engine couldnt get values from msaccess database. result set returns null also i checked my query it was correct
What i have to do to solve this?

am using Tomcat 5.0 and jdk 5.0
please help me .its urgent.
kavitha vasu
Ranch Hand

Joined: Feb 22, 2007
Posts: 70
here is my code . please check it out why my result set is null i checked my query. it was correct

Please help me to fix this in tomcat 5.0

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class sampleservlet extends HttpServlet
{
Connection c=null;
Statement s=null;
ResultSet rs=null;
int count=0;
public void init() throws ServletException
{

try
{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection c=DriverManager.getConnection("jdbc dbc:vasu","","");
Statement s=c.createStatement();
}
catch(Exception e)
{
System.out.println(e);
}



}
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();




String dbuser=null;
String dbpass=null;


try
{
out.println("result set is here :"+rs);
rs=s.executeQuery("select * from user");


out.println("result set is after:"+rs);
while(rs.next())
{
out.println("hello ");
dbuser=rs.getString("uname");
dbpass=rs.getString("dbpass");
System.out.println(dbuser);
System.out.println(dbpass);
count++;
}

}
catch(Exception e)
{
out.println(e.getMessage());
}

out.println("count :"+count);
out.println(dbpass);
}
}
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42952
    
  73
I'm amazed that the ResultSet is null; it should throw a NullPointerException. The problem is here:

That should read:

For what it's worth, I wouldn't reuse Statement objects across web requests, but instead create them for each new request. Also be aware that the JDBC/ODBC bridge driver you're using is not thread-safe, so if multiple requests come in at the same time, the will be trouble.
kavitha vasu
Ranch Hand

Joined: Feb 22, 2007
Posts: 70
i changed my code. but still getting same problem
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31105
    
165


To confirm, the result set is null at both of these, or just the first one? We would expect it to be a null at the first println. If it is null at the second println, there is some problem with your driver.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Sunil Vasudevan
Ranch Hand

Joined: Mar 05, 2007
Posts: 107
In addition to what Jeanne mentioned, here are few more checks...

You have the following code:
-----------------------------
catch(Exception e)
{
out.println(e.getMessage());
}
------------------------------

Replace that with:
------------------------------
catch (Exception exception)
{
ByteArrayOutputStream output = new ByteArrayOutputStream();
exception.printStackTrace( new PrintStream( output ) );
out.println( output.toString() );
}
------------------------------

This should give you a better picture of what is happening in your code.

If a NullPointerException is throw, exception.getMessage() method returns null. Hence may not provide a clean picture of what has happened.

In your code, check the following line:
--------------------------
try
{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection c=DriverManager.getConnection("jdbc dbc:vasu","","");
Statement s=c.createStatement();
}
catch(Exception e)
{
System.out.println(e);
}
----------------------------------

Here if there is a problem getting a connection or a statement object, the program catches the exception and lets it proceed to the next method. This is bad programming to suppress such critical exceptions. In doPost(), your statement object may be null, which is a potential candidate for a NullPointerException to be thrown.


Sunil.V<br />SCJP2, SCWCD1.4, SCBCD1.3
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: tomcat with jdbc