0

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();
    }

}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Aayush Goyal
  • 391
  • 1
  • 8
  • 21
  • 2
    What do you think that error message means? Could you rephrase it? Here's my attempt: "you must catch InstantiationException. Or you must declare that it can be thrown.". Read http://docs.oracle.com/javase/tutorial/essential/exceptions/. It's important that you understand what this message means, and how exceptions work, but note that this line of code should not be necessary. – JB Nizet Apr 30 '16 at 13:24

3 Answers3

2

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.

Vampire
  • 35,631
  • 4
  • 76
  • 102
1

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

suulisin
  • 1,414
  • 1
  • 10
  • 17
1

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • As far as I know there was a use case for calling `newInstance()` with a very old MySQL driver (beginning 2000s(?)) that had a bug where the driver registration did not occur in a static initializer as required by the JDBC specification, but in an instance initializer. So you had to call `newInstance()` to get it to register. That bug has been fixed for years (decades ;) now, though. – Mark Rotteveel Apr 30 '16 at 14:26