• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with DbConnectionBroker

 
Subbu Aswathanarayan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am writing a servlet which uses DbConnectionBroker to create a pool of connections.The program compiles without any errors but when i try to open the servlet in a browser window i get a "NullPointerException" at the line "myBroker.freeConnection(conn);".
The program works without any errors if i run it as a standalone application instead of a servlet.
here is the program code.any help would be greatly appreciated.
(i am using mysql with JRun and IIS 4.0)
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import com.javaexchange.dbConnectionBroker.*;

public class mysqlservlet extends HttpServlet
{
DbConnectionBroker myBroker = null;
//PrintWriter out;
String error;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
//res.setContentType ("text/html");
//out = res.getWriter();
try
{
myBroker = new DbConnectionBroker("org.gjt.mm.mysql.Driver", "jdbc:mysql://localhost:3306/acl", "login", "password", 2, 7, "C:\\mysql\\log\\error.log", 1.0);
}
catch (IOException e5)
{
error = e5.getMessage();
}
}

Statement stmt = null;
Connection conn = null;

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
res.setContentType ("text/html");
PrintWriter out = res.getWriter();
try
{
conn = myBroker.getConnection();
stmt = conn.createStatement();
String query = "SELECT * FROM acl";
out.println("<html><head><title>My First JDBC-Servlet");
out.println("</title></head><body>");
out.println("<p>This text came from a servlet.</p>");
out.println("<p>" + error + "<p>");
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
String s = rs.getString("loginid");
String n = rs.getString("password");
out.println("<p>" + s + " " + n + "<p>");
}
out.println("</body></html>");
}
catch(SQLException e)
{
System.err.println (e.getMessage());
System.exit(2);
}
catch(Exception e)
{
System.err.println (e.getMessage());
}
finally
{
try
{
if(stmt != null)
{
stmt.close();
}
}
catch(SQLException e)
{
}
myBroker.freeConnection(conn);
}
out.close();
}
}
 
Frank Ying
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you may need to check if (myBroker != null)
myBroker.freeConnection
I didn't look through all your codes, but NullPointExcep always means the calling obj is null, this is why you need to check null obj first

Originally posted by Subbu Aswathanarayan:
Hi all,
I am writing a servlet which uses DbConnectionBroker to create a pool of connections.The program compiles without any errors but when i try to open the servlet in a browser window i get a "NullPointerException" at the line "myBroker.freeConnection(conn);".
The program works without any errors if i run it as a standalone application instead of a servlet.
here is the program code.any help would be greatly appreciated.
(i am using mysql with JRun and IIS 4.0)
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import com.javaexchange.dbConnectionBroker.*;

public class mysqlservlet extends HttpServlet
{
DbConnectionBroker myBroker = null;
//PrintWriter out;
String error;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
//res.setContentType ("text/html");
//out = res.getWriter();
try
{
myBroker = new DbConnectionBroker("org.gjt.mm.mysql.Driver", "jdbc:mysql://localhost:3306/acl", "login", "password", 2, 7, "C:\\mysql\\log\\error.log", 1.0);
}
catch (IOException e5)
{
error = e5.getMessage();
}
}

Statement stmt = null;
Connection conn = null;

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
res.setContentType ("text/html");
PrintWriter out = res.getWriter();
try
{
conn = myBroker.getConnection();
stmt = conn.createStatement();
String query = "SELECT * FROM acl";
out.println("<html><head><title>My First JDBC-Servlet");
out.println("</title></head><body>");
out.println("<p>This text came from a servlet.</p>");
out.println("<p>" + error + "<p>");
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
String s = rs.getString("loginid");
String n = rs.getString("password");
out.println("<p>" + s + " " + n + "<p>");
}
out.println("</body></html>");
}
catch(SQLException e)
{
System.err.println (e.getMessage());
System.exit(2);
}
catch(Exception e)
{
System.err.println (e.getMessage());
}
finally
{
try
{
if(stmt != null)
{
stmt.close();
}
}
catch(SQLException e)
{
}
myBroker.freeConnection(conn);
}
out.close();
}
}

 
Subbu Aswathanarayan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frankying:
you may need to check if (myBroker != null)
myBroker.freeConnection
I didn't look through all your codes, but NullPointExcep always means the calling obj is null, this is why you need to check null obj first


Hi Frank,
Thanks for ur reply.ur reply partially solved my problemnow i dont get the nullpointerexception, but the browser window is blank.i am not getting any output.this probably means no connection to the database was created.any idea why this is happening.
Subbu
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!