my dog learned polymorphism*
The moose likes JDBC and the fly likes this code not compiling! why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "this code not compiling! why?" Watch "this code not compiling! why?" New topic
Author

this code not compiling! why?

joseph okon
Ranch Hand

Joined: Dec 07, 2004
Posts: 63
package mysql;


import java.sql.*;

public class Hello {

Connection connection;

private void displaySQLErrors(SQLException e) {
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());
}
public Hello() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();

} catch (SQLException e) {
System.err.println("Unable to find and load driver");
System.exit(1);
}
}
public void connectToDB() {
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3308/accounts?user=root&password=idoreyin");
} catch(SQLException e) {
displaySQLErrors(e);
}
}

public void executeSQL() {
try {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(
"SELECT * FROM mytable");

while (rs.next()) {

System.out.println(rs.getString(1));
}

rs.close();
statement.close();
connection.close();
}
catch(SQLException e) {
displaySQLErrors(e);
}
}
public static void main(String[] args) {
Hello hello = new Hello();
hello.connectToDB();
hello.executeSQL();
}
}
saurav sarkar
Ranch Hand

Joined: Jan 07, 2007
Posts: 180

let us know where exactly the error is coming.
paste the stacktrace and please put the code in the code tags.


Be Objectively Oriented.Explore the power of OOPs.
My Blog, Eclipse EMF Query committer.
joseph okon
Ranch Hand

Joined: Dec 07, 2004
Posts: 63
Unreported Exception java.sql.SQLException is never thrown in the body of corresponding try statement

this is the compilation error; i have throw the error but still it wouldn't compile
need help
thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You can only catch checked exception if they are declared as throwable from the method you are calling.

Have a look at this code:

Re-read the method forName of the Class class. What exceptions does it throw?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Bill Johnston
Ranch Hand

Joined: Nov 17, 2005
Posts: 201
Following up on what Paul said, if, on line 16, you simply remove the SQL in front of Exception, making it 'catch(Exception e)', you will find that it will compile fine.

~Bill


~Bill
joseph okon
Ranch Hand

Joined: Dec 07, 2004
Posts: 63
paul and bill you guys have made my day
thanks
Bill Johnston
Ranch Hand

Joined: Nov 17, 2005
Posts: 201
Hey, sure thing.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19726
    
  20

Originally posted by Bill Johnston:
Following up on what Paul said, if, on line 16, you simply remove the SQL in front of Exception, making it 'catch(Exception e)', you will find that it will compile fine.

~Bill

NO! DON'T!

Using "catch (Exception e)" is BAD practive, and should never be used unless a method explicitly is declared to throw Exception and not one or more subclasses (which, frankly, should hardly ever occur).

When you catch all exceptions like this, you will not know what exception it was. Instead, just catch only those exceptions that are actually thrown. In this case, it's SQLException for the SQL stuff and ClassNotFoundException for the Class.forName() call.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bill Johnston
Ranch Hand

Joined: Nov 17, 2005
Posts: 201
Clarification ... with emphasis on "Following up on what Paul said", I was NOT suggesting that he actually code like this, but rather that he code it like Paul said. What I meant was exactly what I said, nothing more, nothing less ... just that if he did that he would "see" that the code was NOT throwing an SQLException specifically.
Shawn Stark
Greenhorn

Joined: Jan 02, 2008
Posts: 7
A great way to see what exceptions you are throwing.


catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}

This only works if you are using swing components.
But if you want to catch the exception no matter what it is
and dont want to close your program so you can continue to test other functions this will work. Of course if you can't compile this will not work for you. When exceptions are thrown you can narrow them down and create a more appropriate catch.

You are trying to catch the wrong exception when checking to see if your driver exsists. example below\/

try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(url);
Statement stmnt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
contacts = stmnt.executeQuery(allSQL);
contacts.last();

last = contacts.getRow();
}

catch(ClassNotFoundException ex){
System.err.println("Cannot find the database driver classes.");
System.err.println(ex);
}

Also notice how you are setting your statement if you want to update delete or add to rs you may want your create statement to look like mine.
When adding or inserting to your resultSet underlying methods will create the sql statements to the database changing both database and resultSet.

This is where I run into a problem.
[ January 03, 2008: Message edited by: Shawn Stark ]
 
 
subject: this code not compiling! why?