Kindly help with unable to "connect to the Database" Problem


General J2EE: Kindly help with unable to "connect to the Database" Problem

  1. Hello All,

    Like many others I have gotten into the problem os unable to "connect to the database".I hadnt posted the exact error I was getting, may be thats the reason I didnt get a response in this forum.I did look into the forums , but could not get a suitable answer.

    It would be great if someone helps me out in this very helpful forum.I am giving you the code I have so far written

    public class DatabaseConnection {
    private Connection con;
    private Statement stmt;
    ProfInfoBean profinfobean = null;

    public ProfInfoBean dataInsert(String course, String sem, String inst){
    try {

                 con = DriverManager.getConnection ("jdbc:odbc:professorforminfo","","");
    stmt = con.createStatement();
    String sql ="insert into professorforminfo.profinfotable(course,semester,instructor)values('"+course+"','"+sem+"','"+inst+"')";

    }catch(ClassNotFoundException cnfEx){
    System.err.println("Failed to load JDBC/ODBC driver.");
    System.exit(1); //terminate program.
    catch(SQLException sqlEx){
    System.err.println("Unable to connect to DB");
    finally {
    try {
    if (con!=null) con.close();
    }catch(SQLException e){System.err.println(e.getMessage());}

    return profinfobean;


    Do excuse me if the format is not readable


    This is the error message is get as follows .....

    /*** Error Messages *****/
    java.sql.SQLException: General error
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(
    at sun.jdbc.odbc.JdbcOdbc.standardError(
    at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(
    at sun.jdbc.odbc.JdbcOdbcStatement.execute(
    at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(
    at manager.DatabaseConnection.dataInsert(
    at servlet.DBInsertServlet.doGet(
    at servlet.DBInsertServlet.doPost(
    at javax.servlet.http.HttpServlet.service(
    at javax.servlet.http.HttpServlet.service(
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(
    at org.apache.catalina.core.StandardWrapperValve.invoke(
    at org.apache.catalina.core.StandardValveContext.invokeNext(
    at org.apache.catalina.core.StandardPipeline.invoke(
    at org.apache.catalina.core.StandardContextValve.invokeInternal(
    at org.apache.catalina.core.StandardContextValve.invoke(
    at org.apache.catalina.core.StandardValveContext.invokeNext(
    at org.apache.catalina.core.StandardPipeline.invoke(
    at org.apache.catalina.core.StandardHostValve.invoke(
    at org.apache.catalina.core.StandardValveContext.invokeNext(
    at org.apache.catalina.valves.ErrorReportValve.invoke(
    at org.apache.catalina.core.StandardValveContext.invokeNext(
    at org.apache.catalina.core.StandardPipeline.invoke(
    at org.apache.catalina.core.StandardEngineValve.invoke(
    at org.apache.catalina.core.StandardValveContext.invokeNext(
    at org.apache.catalina.core.StandardPipeline.invoke(
    at org.apache.catalina.core.ContainerBase.invoke(
    at org.apache.coyote.tomcat5.CoyoteAdapter.service(
    at org.apache.coyote.http11.Http11Processor.process(
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(
    at org.apache.tomcat.util.threads.ThreadPool$
    /*** Error Messages *****/

    Help would be greatly appreciated.

    Thanking in advance
  2. I don't see anything obvious. I'm not sure if the lack of spaces in your sql statement is actually in the code or is just a result of posting it here, but I'd look into that. In fact, I would log the generated SQL statement and test that in your favorite SQL client.

    Your SQL is likely the cause of the problem. Generating SQL text is hardly ever the best way to interact with the database. You need to double up single quotes, etc, in the values being inserted here or you will have problems. It is better to use binding:

    public void insert(Connection con, Record obj)
            throws SQLException, BindException {
        PreparedStatement stmt = con.prepareStatement(
            "Insert into table values (?, ?)");
        int index = 1;
        stmt.setInt(++index, obj.getValue1());
        stmt.setInt(++index, obj.getValue2());

    Also, unrelated to your problem at hand, you should consider re-thinking your exception handling strategy. Your System.exit(1) in the exception block will prevent everything that happened before this to not clean up it's resource. You should decide at which level in your app exceptions can be handled and only handle them there. Everything else either catches and rethrows exceptions (if i needs to clean up a resource) or the function should be marked as throws YourException. Your second catch block just logs the error and continues like nothing is wrong. This is almost never a good practice.
  3. Hi,

       Thanks for the detailed message.It was really very thoughtful of you to have given a detailed response.Will work on what you have indicated.Hoping to learn..

    Thanks again
  4. I guess you'd be better of using the IPAddress in the url instead of the name. So you can try doing a ping or something like that..

    May be its not resolving the ipaddress based on the machine name you are providing in the URL