-2

i am using netbeans 8.2 and mysql 8.0 and inport mysql connector 8.0.11 to the library file as well for my following java codes,

package jdbc;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class JDBC {

    public static void main(String[] args) {
        String SQL="Insert into Customer Values('C111','Somadasa','Matugama',34000)";
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3308/ThogaKade", "root", "ftwxy");
        Statement stm=connection.createStatement();
        stm.executeUpdate(SQL);
    }

}

but following lines have been highlighted Class.forName("com.mysql.jdbc.Driver"); Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3308/ThogaKade", "root", "mysql");

with the message 'unreported exception ClassNotFoundException; must be caught or declared to be thrown ----' how could fix this problem?

sign458
  • 55
  • 8

3 Answers3

1

You can specify that main throws the Exception(s). Like,

public static void main(String[] args) throws SQLException, ClassNotFoundException {
    String SQL = "Insert into Customer Values('C111','Somadasa','Matugama',34000)";
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager
            .getConnection("jdbc:mysql://localhost:3308/ThogaKade", "root", "ftwxy");
    Statement stm = connection.createStatement();
    stm.executeUpdate(SQL);
}

Or, you can use a try-catch (preferably with a try-with-Resources) like,

public static void main(String[] args) {
    String SQL = "Insert into Customer Values('C111','Somadasa','Matugama',34000)";
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e1) {
        e1.printStackTrace();
    }
    try (Connection connection = DriverManager
            .getConnection("jdbc:mysql://localhost:3308/ThogaKade", "root",
            "ftwxy"); Statement stm = connection.createStatement()) {
        stm.executeUpdate(SQL);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

The advantage of a try-with-Resources is it will close() your Connection and Statement(s) correctly. Finally, you haven't actually needed to explicitly load a JDBC driver with Class.forName(String) since Java 6.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

Just add throws in method signature, like this:

public static void main(String[] args) throws ClassNotFoundException{
   //method body
}
DimXenon
  • 189
  • 4
  • 7
0

this is because Class.forName and DriverManager.getConnection throws Exception that needs to be handled.
The simplest approach to fix this error is to make your main method throw Exception.
Something like this:

public static void main(String[] args) throws Exception {
    // your code here
}  

Another approach would be to enclose your code inside a try-catch block.
Something like this:

public static void main(String[] args) {
    String SQL="Insert into Customer Values('C111','Somadasa','Matugama',34000)";
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3308/ThogaKade", "root", "ftwxy");
        Statement stm=connection.createStatement();
        stm.executeUpdate(SQL);
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    }
}

Hope this helps

S1LENT WARRIOR
  • 11,704
  • 4
  • 46
  • 60