简体   繁体   English

登录中的异常::无法创建与数据库服务器的连接

[英]Exception in Login:: Could not create connection to database server

I'm working on an android application (API level 19) that connects to a database on AWS and I keep getting the following error: 我正在使用连接到AWS上的数据库的android应用程序(API级别19),并且不断收到以下错误:

    04-29 09:24:12.095: I/System.out(1711): Exception in Login::Could not create connection to database server.
    04-29 09:24:12.095: W/System.err(1711): com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

I have a class called DataBaseConnection that handles the connection: 我有一个名为DataBaseConnection的类来处理连接:

public class DataBaseConnection {

Connection connect = null;

public Connection getDataBaseConnection(){
    if(connect == null){
        try {
            Class.forName("com.mysql.jdbc.Driver");// loading MySQL driver
            connect = DriverManager.getConnection("jdbc:mysql://cmpe277.c38qsf0avgvg.us-west-1.rds.amazonaws.com:3306/CMPE277?user=root&password=password");
            //Set up connection with DB, username, password
        } catch (Exception e) {
            System.out.println("Exception in Login::"+e.getMessage());
            e.printStackTrace();
        }
    }
    return connect;
}
}

And the onClick method to connect to the database and insert values looks like this: 连接数据库并插入值的onClick方法如下所示:

//onClick Method for Sign Up
public void signUp(View view){

    DataBaseConnection dbConn = new DataBaseConnection();
    Connection conn = dbConn.getDataBaseConnection();

    EditText usernameEntry = (EditText) findViewById(R.id.username);
    username = usernameEntry.getText().toString();

    EditText password1Entry = (EditText) findViewById(R.id.password1);
    password1 = password1Entry.getText().toString();

    EditText password2Entry = (EditText) findViewById(R.id.password2);
    password2 = password2Entry.getText().toString();

    //Check That the two Typed Passwords Match
    if(password1.equals(password2)){
        query = "INSERT INTO USER VALUES (username,password1)";
        try {
                if(conn != null){
                    stmt = conn.createStatement();
                    stmt.executeQuery(query);
                }
        } 
        catch (Exception e) {
                System.out.println("Error in execute query::"+e.getMessage());
                e.printStackTrace();
        }
    }
}

I have used this class (DataBaseConnection) within a Java application without problems but when I try to implement it in an android app I run into errors. 我已经在Java应用程序中使用了此类(DataBaseConnection),没有问题,但是当我尝试在android应用程序中实现该类时,我遇到了错误。

Found the solution. 找到了解决方案。 Simply ran the connection to the database in a separate thread using AsyncTask 只需使用AsyncTask在单独的线程中运行与数据库的连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 异常无法创建与数据库服务器的连接 - exception could not create connection to database server .MySQLNonTransientConnectionException: Could not create connection to database server - .MySQLNonTransientConnectionException: Could not create connection to database server 无法通过 Docker 创建与数据库服务器的连接 - Could not create connection to database server via Docker 无法创建与数据库服务器MySQL 8.0的连接 - Could not create connection to database server, MySQL 8.0 无法创建到 MySQL 数据库服务器的 JDBC 连接 - Could not create JDBC connection to MySQL database server 无法建立与数据库服务器的连接-Java MySQL Connector - Could not create connection to database server - java mysql connector SQLException: 无法创建到数据库服务器的连接。 SQLState: 08001 - SQLException: Could not create connection to database server. SQLState: 08001 “无法创建与数据库服务器的连接” netbeans谷歌云sql错误 - “Could not create connection to database server” netbeans google cloud sql error 无法建立与数据库服务器的连接-Eclipse MySQL错误 - Could not create connection to database server - Eclipse MySQL error 调用Driver#connect时出错,并且无法创建与数据库服务器的连接 - Error calling Driver#connect and Could not create connection to database server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM