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

Discussions

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 {

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


    }catch(ClassNotFoundException cnfEx){
    System.err.println("Failed to load JDBC/ODBC driver.");
    cnfEx.printStackTrace();
    System.exit(1); //terminate program.
    }
    catch(SQLException sqlEx){
    System.err.println("Unable to connect to DB");
    sqlEx.printStackTrace(System.err);
    sqlEx.printStackTrace();
    }
    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

    /*** DatabaseConnection.java****/

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

    /*** Error Messages *****/
    java.sql.SQLException: General error
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6987)
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)
    at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3111)
    at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:338)
    at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:288)
    at manager.DatabaseConnection.dataInsert(DatabaseConnection.java:34)
    at servlet.DBInsertServlet.doGet(DBInsertServlet.java:77)
    at servlet.DBInsertServlet.doPost(DBInsertServlet.java:86)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
    at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
    at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
    at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
    at java.lang.Thread.run(Thread.java:534)
    /*** Error Messages *****/

    Help would be greatly appreciated.

    Thanking in advance
    AS
  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());
        stmt.executeUpdate();
    }

    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
    AS
  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