Results 1 to 3 of 3
  1. #1
    coco is offline Member
    Join Date
    Jul 2007
    Posts
    39
    Rep Power
    0

    Default I have 3 errors after compiling

    Hi, I have this code:

    Java 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:
    Java 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.

  2. #2
    toby is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    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).

  3. #3
    varunthecool is offline Member
    Join Date
    Jul 2007
    Location
    bangalore,india
    Posts
    19
    Rep Power
    0

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •