簡體   English   中英

登錄中的異常::無法創建與數據庫服務器的連接

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

我正在使用連接到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.

我有一個名為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;
}
}

連接數據庫並插入值的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();
        }
    }
}

我已經在Java應用程序中使用了此類(DataBaseConnection),沒有問題,但是當我嘗試在android應用程序中實現該類時,我遇到了錯誤。

找到了解決方案。 只需使用AsyncTask在單獨的線程中運行與數據庫的連接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM