Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

tomcat with jdbc

 
kavitha vasu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i changed my code. but still getting same problem
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34410
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Sunil Vasudevan
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic