Thread: Compile error

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    12
    Rep Power
    0

    Compile error


    I am trying to connect to my database using the following code but get the following errors:
    C:\Program Files\Apache Group\Tomcat 4.1\webapps\ID-HCI-TIP\WEB-INF\classes\com\idhcitip\SessionCounter.java:109: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
    ^
    C:\Program Files\Apache Group\Tomcat 4.1\webapps\ID-HCI-TIP\WEB-INF\classes\com\idhcitip\SessionCounter.java:109: unreported exception java.lang.InstantiationException; must be caught or declared to be thrown
    Class.forName("org.gjt.mm.mysql.Driver").newInstance();


    private Connection getConnection() throws SQLException
    {
    Connection Conn = null;
    String host="localhost";
    String user="root";
    String pass="";
    String db="idhcitipcount";
    String conn;

    try
    {
    Class.forName("org.gjt.mm.mysql.Driver").newInstance();

    // create connection string
    conn = "jdbc:mysql://" + host + "/" + db + "?user=" + user + "&password=" +pass;

    // pass database parameters to JDBC driver
    Conn = DriverManager.getConnection(conn);
    }

    catch(SQLException e)
    {
    }

    return Conn;


    }
    Any ideas?
  2. #2
  3. Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    49
    Rep Power
    15
    * are you importing the DB driver files (all of them) into your page? as in
    Code:
    import "org.gjt.mm.mysql.Driver";
    * are your DB drivers in your tomcat commons/lib folder? they should be...

    Elie
  4. #3
  5. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,898
    Rep Power
    3887
    There's no need to import the sql driver. Java is telling you that the line "Class.forName("org.gjt.mm.mysql.Driver").newInstance();" can cause an "InstantiationException" or a "ClassNotFoundException". You'll have to catch those exceptions as follows:
    Code:
    catch (ClassNotFoundException e) {
       // do stuff with e (if you want)
    }
    catch (InstantiationException e) {
       // do stuff with this e (if you want)
    }
    Put that just above where you're catching your SQLException and it should be OK.
    ~ishnid
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    12
    Rep Power
    0
    I added the catches in but it has just created the same problem in the place where the old function was called.
    How do I stop the class trouble when calling the function as well?


    C:\Program Files\Apache Group\Tomcat 4.1\webapps\ID-HCI-TIP\WEB-INF\classes\idhcitip\SessionCounter.java:53: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
    conn = getConnection();


    private void updateDatabaseCount() throws SQLException, ClassNotFoundException
    {
    Connection conn = null;
    try
    {
    conn = getConnection();
    Statement stmt = conn.createStatement();
    stmt.execute("UPDATE session_count SET count = count + " + newSessions);
    newSessions = 0;
    }

    finally
    {
    try
    {
    conn.close();
    }
    catch(SQLException e)
    {
    }

    }
    }
    private Connection getConnection() throws SQLException, ClassNotFoundException
    {
    Connection Conn = null;
    String host="localhost";
    String user="root";
    String pass="";
    String db="idhcitipcount";
    String conn;

    try
    {
    Class.forName("org.gjt.mm.mysql.Driver").newInstance();

    // create connection string
    conn = "jdbc:mysql://" + host + "/" + db + "?user=" + user + "&password=" +pass;

    // pass database parameters to JDBC driver
    Conn = DriverManager.getConnection(conn);
    }

    catch (ClassNotFoundException e)
    {
    // do stuff with e (if you want)
    }
    catch (InstantiationException e)
    {
    // do stuff with this e (if you want)
    }

    catch(SQLException e)
    {
    }

    catch(Exception e)
    {
    }

    return Conn;


    }
    }
  8. #5
  9. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,898
    Rep Power
    3887
    You shouldn't be throwing AND catching the exceptions - the facility to throw exceptions is that you don't have to catch them in the method they occur in, though you will have to catch them wherever you called the method from.

    In other words, you should either catch the ClassNotFoundException, SQLException etc. at the end of the getConnection() method or throw them, in which case you will have to have the catch blocks for them wherever you called the method from (in this case, it's called from your updateDatabaseCount() method).

    ~ishnid
  10. #6
  11. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117

IMN logo majestic logo threadwatch logo seochat tools logo