局部变量conn的值没有被使用

The value of the local variable conn is not used

正如我在主题中提到的,我正面临警告 "The value of the local variable conn is not used"

我清楚地在我的编码中使用了该变量,但它向我显示了那种类型的错误。 如果您能就此提出建议,我们将不胜感激。

[代码]

package jdbc;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class example {
    public static void main(String[] ar) {


        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Success to find Driver");
        }catch(ClassNotFoundException e) {
            System.err.println("error = Failed to find driver");
        }//*Find MYSQL driver

        Connection conn = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/class", "root", "****");
            System.out.println("Connected to MySql");
        } catch (SQLException e) {
            System.err.println("error = Failed to Create connection");
        }//* Creating Connection

        }
    }

[运行]

之后的结果

成功找到驱动程序 error = 创建连接失败

在您的代码中,您已经分配了对 conn 的引用,但您没有在任何地方使用该对象,因此 警告

要消除警告,请在某处使用它(可能在某处执行 sysout)或添加 @SuppressWarnings("unused") 注释