Android - 无法通过点击监听器连接到数据库

时间:2017-11-14 21:03:22

标签: java android jdbc

我有一个AlertDialog显示,提示用户输入文字,如果" Ok"按下按钮,我希望它调用连接到数据库的方法。

数据库方法有效,因为我尝试连接到点击侦听器外部的数据库,并且工作正常。

这是我得到的例外:

RetrieveFolder() {
        AlertDialog.Builder inputBox = new AlertDialog.Builder(activity);
        inputBox.setTitle(getResources().getString(R.string.enter_share_code));
        EditText etInput = new EditText(activity);
        etInput.setMaxLines(1);
        inputBox.setView(etInput);

        inputBox.setPositiveButton("Ok", (dialog, whichButton) -> {
            input = etInput.getText().toString();
            if (folderExistsAndIsValid(input) && !alreadyShared(input)) {
                StudE.sendToDatabase("INSERT INTO tblSHARED VALUES(?, ?);", username, input);
                StudE.toast(getResources().getString(R.string.folder_retrieved_successfully), activity);

                finish();
                startActivity(getIntent());
            }
        });

        inputBox.setNegativeButton("Cancel", (dialog, whichButton) -> {});
        inputBox.show();
}

这是显示对话框的方法:

W/System.err: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
W/System.err:     at java.lang.reflect.Constructor.newInstance0(Native Method)
W/System.err:     at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
W/System.err:     at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
W/System.err:     at com.mysql.jdbc.Util.getInstance(Util.java:408)
W/System.err:     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918)
W/System.err:     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
W/System.err:     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
W/System.err:     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
W/System.err:     at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2275)
W/System.err:     at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2024)
W/System.err:     at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:779)
W/System.err:     at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
W/System.err:     at java.lang.reflect.Constructor.newInstance0(Native Method)
W/System.err:     at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
W/System.err:     at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
W/System.err:     at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389)
W/System.err:     at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:569)
W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:219)
W/System.err:     at woodward.stude.StudE.makeConnection(StudE.java:91)
W/System.err:     at woodward.stude.StudE.queryDatabase(StudE.java:145)
W/System.err:     at woodward.stude.FlashcardFoldersActivity$RetrieveFolder.folderExistsAndIsValid(FlashcardFoldersActivity.java:305)
W/System.err:     at woodward.stude.FlashcardFoldersActivity$RetrieveFolder.lambda$new$0$FlashcardFoldersActivity$RetrieveFolder(FlashcardFoldersActivity.java:291)
W/System.err:     at woodward.stude.FlashcardFoldersActivity$RetrieveFolder$$Lambda$0.onClick(Unknown Source:0)
W/System.err:     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
W/System.err:     at android.os.Looper.loop(Looper.java:164)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6494)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
W/System.err: Caused by: android.os.NetworkOnMainThreadException
W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1450)
W/System.err:     at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:102)
W/System.err:     at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90)
W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:787)
W/System.err:     at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:188)
W/System.err:     at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
W/System.err:     at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2196)
W/System.err:     at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2229)
W/System.err:   ... 22 more

完整堆栈跟踪:

{0.1;0.2;0.3;0.7}

2 个答案:

答案 0 :(得分:1)

您应该尝试在后台线程上进行连接。在主线程上连接到网络:NetworkOnMainThreadException

您可以在此处找到问题的答案: ANSWER

答案 1 :(得分:1)

new Thread(new Runnable() {
    @Override
    public void run() {
        StudE.sendToDatabase("INSERT INTO tblSHARED VALUES(?, ?);", username, input);
    }
}).start();

StudE.toast(getResources().getString(R.string.folder_retrieved_successfully), activity);

创建新线程并在其上运行数据库连接应该有效。