如何修复“未报告的异常实例化异常;必须被捕获或声明被抛出“[副本]”

时间:2022-11-29 20:18:44

This question already has an answer here:

这个问题已经有了答案:

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.

我正在NetBeans 8.1中构建一个简单的HelloWorld应用程序。它必须简单地工作,只要编译并运行它就会使用MySQL在数据库帐户中加载表acc的所有数据。

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.

我已经将连接器/J jar文件包含在我的项目库中,并连接到MySQL,但连接到Class.forName(com.mysql.jdbc.Driver).newInstance();一行代码显示“未报告的异常实例化异常;必须被抓住或宣布被扔出去“我完全不知道该怎么做。我被困在这里了。

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 个解决方案

#1


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.

如果调用. newinstance(),则调用的构造函数可能抛出一些异常。由于这是通过反射实现的,因此不知道构造函数是否抛出一个已检查的异常,因此无论如何,反射的. newinstance()调用都会抛出一个已检查的异常,您必须处理构造函数可能抛出一个异常的情况。

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.

但是为什么要调用。newinstance()呢?就我所记得的课程而言。forName应该足够,因为类被加载,它的静态初始化器将在DriverManager中注册类。

#2


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

请使用jave 7及以上版本,你甚至都不需要那个电话。可以完全删除Class.forName(“com.mysql.jdbc.Driver”).newInstance();

#3


1  

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

您永远不需要newInstance()部分,而且您还不需要Class.forName()部分,因为JDBC 4出现于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.

然而,您的问题中没有什么问题是不可能通过仔细查看类. newinstance()中的Javadoc而得到解决的。检查过的异常必须以某种方式处理。

#1


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.

如果调用. newinstance(),则调用的构造函数可能抛出一些异常。由于这是通过反射实现的,因此不知道构造函数是否抛出一个已检查的异常,因此无论如何,反射的. newinstance()调用都会抛出一个已检查的异常,您必须处理构造函数可能抛出一个异常的情况。

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.

但是为什么要调用。newinstance()呢?就我所记得的课程而言。forName应该足够,因为类被加载,它的静态初始化器将在DriverManager中注册类。

#2


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

请使用jave 7及以上版本,你甚至都不需要那个电话。可以完全删除Class.forName(“com.mysql.jdbc.Driver”).newInstance();

#3


1  

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

您永远不需要newInstance()部分,而且您还不需要Class.forName()部分,因为JDBC 4出现于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.

然而,您的问题中没有什么问题是不可能通过仔细查看类. newinstance()中的Javadoc而得到解决的。检查过的异常必须以某种方式处理。