从其他类导入时,java.sql.Connection不工作

从其他类导入时,java.sql.Connection不工作,java,android,sql,Java,Android,Sql,我已经找出了我犯过的两个错误,因此将更新我的问题 首先,我以前的错误: java.lang.ClassCastException: Bootstrap method returned null 只需将此行添加到build.graddle(应用程序)依赖项即可修复: implementation group: 'com.google.protobuf', name: 'protobuf-java', version: '3.6.0' 这很好,但是这并没有解决我所有的问题。这是我当前的注册按钮方

我已经找出了我犯过的两个错误,因此将更新我的问题

首先,我以前的错误:

java.lang.ClassCastException: Bootstrap method returned null
只需将此行添加到build.graddle(应用程序)依赖项即可修复:

implementation group: 'com.google.protobuf', name: 'protobuf-java', version: '3.6.0'
这很好,但是这并没有解决我所有的问题。这是我当前的注册按钮方法:

registerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                conn = Utils.getConnection();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
正如你所看到的,我只专注于获得联系,因为这似乎是问题的核心。在调试过程中,我还将getConnection()修改为以下内容:

public static Connection getConnection() {
    String driver = "com.mysql.cj.jdbc.Driver"; //com.mysql.jdbc.Driver doesn't work either
    String url = "url";
    String username = "user";
    String password = "pass";

    try {
        Class.forName(driver);
    } catch (ClassNotFoundException e) {
        System.out.println("Where is your MySQL JDBC Driver?");
        e.printStackTrace();
        return null;
    }

    System.out.println("MySQL JDBC Driver Registered!");
    Connection connection = null;

    try {
        connection = DriverManager.getConnection(url,username, password);
    } catch (SQLException e) {
        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        return null;
    }
    if (connection != null) {
        System.out.println("You made it, take control your database now!");
    } else {
        System.out.println("Failed to make connection!");
    }
    return connection;
}
这就是我现在收到的错误:

I/System.out: Connection Failed! Check output console
W/System.err: java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
W/System.err: Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
          The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Caused by: java.net.SocketException: socket failed: EACCES (Permission denied)

我将在进一步更改时保持此更新。同样,如果问题很明显,我提前道歉。谢谢你的帮助

registerUser(connection,…)
在哪里声明
connection
?@JohnJoe,我已经移动了不少连接,现在我将它声明为类变量。似乎它必须是其他的东西,因为我在任何地方都尝试过它。根据堆栈跟踪,您的
registerUser
方法似乎与问题的根源分离,因为它从未出现在那里。相反,异常发生在您的
getConnection
方法中。
stmt.execute(“插入到用户(第一个、最后一个、电子邮件、salt、密码)值(“+first.toLowerCase()+”、“+last.toLowerCase()+”、“+email.toLowerCase()+”、“+salt+”、“+hashedPassword+”)@Vulcan我同意,但我不知道如何修复它。