Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 26th, 2003, 07:03 AM
bmcnicoll bmcnicoll is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 12 bmcnicoll User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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?

Reply With Quote
  #2  
Old March 26th, 2003, 10:32 AM
zcrar70's Avatar
zcrar70 zcrar70 is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 49 zcrar70 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
* 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

Reply With Quote
  #3  
Old March 26th, 2003, 01:26 PM
ishnid's Avatar
ishnid ishnid is online now
kill 9, $$;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Sep 2001
Location: Dublin, Eire
Posts: 5,574 ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 4 Days 13 h 7 m 23 sec
Reputation Power: 1411
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

Reply With Quote
  #4  
Old March 26th, 2003, 02:34 PM
bmcnicoll bmcnicoll is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 12 bmcnicoll User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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;


}
}

Reply With Quote
  #5  
Old March 26th, 2003, 02:52 PM
ishnid's Avatar
ishnid ishnid is online now
kill 9, $$;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Sep 2001
Location: Dublin, Eire
Posts: 5,574 ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 4 Days 13 h 7 m 23 sec
Reputation Power: 1411
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

Reply With Quote
  #6  
Old March 26th, 2003, 04:57 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Compile error


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway