Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-05-2007, 05:16 PM
Member
 
Join Date: Jul 2007
Posts: 39
Rep Power: 0
coco is on a distinguished road
Default I have 3 errors after compiling
Hi, I have this code:

Code:
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/accounts?user=&password="); 
		}
		catch(SQLException e) {
			displaySQLErrors(e); 
		}
	}
	
	public void executeSQL() {
		try {
			Statement statement = connection.createStatement(); 
			
			ResultSet rs = statement.executeQuery("SELECT * FROM acc_acc"); 
			
			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(); 
	}
}
I have 3 errors after compiling:
Code:
C:\Program Files\Xinox Software\JCreator LE\MyProjects\Hello.java:15: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
the second:
C:\Program Files\Xinox Software\JCreator LE\MyProjects\Hello.java:15: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
the third:
C:\Program Files\Xinox Software\JCreator LE\MyProjects\Hello.java:17: exception java.sql.SQLException is never thrown in body of corresponding try statement.
How can i solve it?
Thanks.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 08-07-2007, 08:39 AM
Member
 
Join Date: Jul 2007
Posts: 40
Rep Power: 0
toby is on a distinguished road
Default
Class.forName() doesn't throw an SQLException, but it might throw a ClassNotFoundException if the class doesn't exist in it's classpath. So instead of catching an SQLException you should be catching a ClassNotFoundException (or something above it in the hierarchy like Exception).
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-18-2007, 10:32 AM
Member
 
Join Date: Jul 2007
Location: bangalore,india
Posts: 19
Rep Power: 0
varunthecool is on a distinguished road
Default instead of
catch(SQLException e)
use catch(Exception e)
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with these errors oceansdepth New To Java 3 04-16-2008 05:55 PM
Errors I don't understand MattyB New To Java 4 04-02-2008 12:55 AM
Handling SQL Errors and Warnings Java Tip Java Tips 0 02-12-2008 10:37 AM
Trying to catch thread errors yelllow4u New To Java 2 08-07-2007 03:52 PM
Errors in constructor ai_2007 Advanced Java 0 07-01-2007 06:35 PM


All times are GMT +2. The time now is 11:34 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org