How to fix "unreported exception InstantiationException; must be caught or declared to be thrown"

2.6k Views Asked by At

I am building a simple HelloWorld application in NetBeans 8.1. It has to work simple wherein as soon as it will compile and run it will load all the data of a table acc in the database accounts using MySQL.

I have included the Connector/J jar file in my project library and made a connection to MySQL but the Class.forName(com.mysql.jdbc.Driver).newInstance(); line in piece of code is showing "unreported exception InstantiationException; must be caught or declared to be thrown" and I exactly don't know what to do. I am kind of stuck here.

Here's my code

package learning_jdbc;

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 (ClassNotFoundException e) {
            System.out.println("Class not found.");
        }
    }

    public void connectToDB() {
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/accounts","root","thejoker");
        } catch(SQLException e) {
            displaySQLErrors(e);
        }
    }

    public void executeSQL() {
        try (Statement statement = connection.createStatement();
                    ResultSet rs = statement.executeQuery("SELECT * FROM acc")) {
            while (rs.next()) {
                System.out.println(rs.getString(1));
            }
            connection.close();
        } catch(SQLException e) {
            displaySQLErrors(e);
        }
    }

    public static void main(String[] args) {
        Hello hello = new Hello();
        hello.connectToDB();
        hello.executeSQL();
    }

}
3

There are 3 best solutions below

0
On BEST ANSWER

If you call .newInstance(), the constructor that is called could throw some exception. As this is via reflection and thus it is not known whether that constructor throws a checked exception or not, the reflective .newInstance() call throws a checked exception anyway and you have to handle the case that the constructor maybe threw an exception.

But why do you do a .newInstance() call at all? As far as I remember a Class.forName should be enough as then the class is loaded and its static initializer will register the class in the DriverManager.

1
On

You never needed the newInstance() part, and you haven't needed the Class.forName() part since JDBC 4 appeared in 2007.

Just remove it.

However there is nothing in your question that couldn't have been solved by a good look at the Javadoc for Class.newInstance(). Checked exceptions have to be handled somehow.

0
On

please with jave 7 and and above you dont even need that call.You can completely remove Class.forName("com.mysql.jdbc.Driver").newInstance();