File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this code not compiling! why?

 
joseph okon
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 180
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let us know where exactly the error is coming.
paste the stacktrace and please put the code in the code tags.
 
joseph okon
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bill Johnston
Ranch Hand
Posts: 201
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
joseph okon
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
paul and bill you guys have made my day
thanks
 
Bill Johnston
Ranch Hand
Posts: 201
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, sure thing.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bill Johnston
Ranch Hand
Posts: 201
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic