Hi,
Any ideas why this code to load the MM.MySQL driver and connect to a MySQL DB works fine from a JSP, but reports the error below when trying to load from a servlet?
Class.forName("org.gjt.mm.mysql.Driver");
java.sql.Connection connection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/sparrowhawk?user=admin&password=test");
java.sql.Statement statement = connection.createStatement();
String sql = "select 1 from people where email = '" + request.getParameter("passwordField") + "';" ;
java.sql.ResultSet rows = statement.executeQuery(sql);
The error stack from the servlet container (Tomcat 4) is:
C:\Program Files\tomcat\webapps\jdw\WEB-INF\classes>javac login.java
login.java:27: unreported exception java.lang.ClassNotFoundException; must be caught or declared to
Class.forName("org.gjt.mm.mysql.Driver");
^
login.java:28: unreported exception java.sql.SQLException; must be caught or declared to be thrown
java.sql.Connection connection = java.sql.DriverManager.getConnection("jdbc:mysql://localho
dmin&password=test");
^
login.java:29: unreported exception java.sql.SQLException; must be caught or declared to be thrown
java.sql.Statement statement = connection.createStatement();
^
login.java:31: unreported exception java.sql.SQLException; must be caught or declared to be thrown
java.sql.ResultSet rows = statement.executeQuery(sql);
^
login.java:33: unreported exception java.sql.SQLException; must be caught or declared to be thrown
rows.last();
^
login.java:34: unreported exception java.sql.SQLException; must be caught or declared to be thrown
int numRows = rows.getRow();
^
login.java:46: unreported exception java.sql.SQLException; must be caught or declared to be thrown
statement.close();
^
login.java:47: unreported exception java.sql.SQLException; must be caught or declared to be thrown
connection.close();
^
8 errors
Thanks,
Mike
Discussions
Web tier: servlets, JSP, Web frameworks: Loading a driver from servlet. How to vs. in a JSP?
-
Loading a driver from servlet. How to vs. in a JSP? (4 messages)
- Posted by: Mike Stevens
- Posted on: August 28 2002 00:23 EDT
Threaded Messages (4)
- Loading a driver from servlet. How to vs. in a JSP? by Bhagvan K on August 28 2002 00:43 EDT
- Loading a driver from servlet. How to vs. in a JSP? by Mike Stevens on August 28 2002 11:49 EDT
-
Loading a driver from servlet. How to vs. in a JSP? by Lasse Koskela on August 29 2002 08:58 EDT
- Loading a driver from servlet. How to vs. in a JSP? by Mike Stevens on September 04 2002 11:55 EDT
-
Loading a driver from servlet. How to vs. in a JSP? by Lasse Koskela on August 29 2002 08:58 EDT
- Loading a driver from servlet. How to vs. in a JSP? by Mike Stevens on August 28 2002 11:49 EDT
-
Loading a driver from servlet. How to vs. in a JSP?[ Go to top ]
- Posted by: Bhagvan K
- Posted on: August 28 2002 00:43 EDT
- in response to Mike Stevens
If you set the error page to a jsp, it will take you to error page when an exception is being thrown. whereas you need to take care of the exception handling explicitly in the servlet code. Another option is to make the doGet or doPost method throw Exception and need not do the exception handling. -
Loading a driver from servlet. How to vs. in a JSP?[ Go to top ]
- Posted by: Mike Stevens
- Posted on: August 28 2002 11:49 EDT
- in response to Bhagvan K
So are you saying the error in the servlet (which was actually from javac while comiling it) was becuase I don't have exception code there in case there is an exception, not actually becuase there was an exception?
I'm afraid I'm still confused (but thatnks for your reply).
Mike -
Loading a driver from servlet. How to vs. in a JSP?[ Go to top ]
- Posted by: Lasse Koskela
- Posted on: August 29 2002 08:58 EDT
- in response to Mike Stevens
That's exactly what he's saying.
When the container is compiling the JSP page into a servlet, it wraps your code with some proprietary code.
One of these additions made by the JSP compiler is to include a try-catch block around your code and the logic for forwarding to the defined error page in case an exception is thrown by your code.
When you're writing a servlet, there is no other compilation than the javac compiler, which complains about you not catching an exception that can be thrown. -
Loading a driver from servlet. How to vs. in a JSP?[ Go to top ]
- Posted by: Mike Stevens
- Posted on: September 04 2002 23:55 EDT
- in response to Lasse Koskela
Thanks for the clarification. I found this out the hard way by hunting through some other code with tray/catch included...
What rules are there about when try/catch are required, and when you can get by without them? The only documentation I've seen about it was pretty loose...
Thanks for all the help everyone,
Mike