|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
* 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 |
|
#3
|
||||
|
||||
|
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
__________________
~ishnid; Have you tried: [ search.cpan.org | perldoc | Java API | mysql.com | google ] Apostrophes are NOT used for possessive pronouns or for noun plurals, including acronyms. |
|
#4
|
|||
|
|||
|
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; } } |
|
#5
|
||||
|
||||
|
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 |
|
#6
|
|||
|
|||
|
Exceptions tutorial
http://java.sun.com/docs/books/tuto...ial/exceptions/ |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Compile error |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|