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

时间:2018-07-30 02:05:33

标签: java android sql

我已设法找出我所犯的两个错误,因此将更新我的问题。

首先,我之前的错误:

java.lang.ClassCastException: Bootstrap method returned null
只需将以下行添加到build.graddle(app)依赖项即可解决

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)

在进行进一步更改时,我将保持最新状态。再说一次,如果问题很明显,我事先表示歉意。感谢您的帮助!

0 个答案:

没有答案