gpt4 book ai didi

java - Android - 无法从点击监听器连接到数据库

转载 作者:行者123 更新时间:2023-12-02 02:29:52 25 4
gpt4 key购买 nike

我有一个 AlertDialog,它显示并提示用户输入文本,如果按下“确定”按钮,我希望它调用连接到数据库的方法。

数据库方法可以工作,因为我尝试连接到点击监听器之外的数据库,并且工作正常。

这是我得到的异常:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

这是显示对话框的方法:

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

最佳答案

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);

创建一个新线程并在其上运行数据库连接应该可以工作。

关于java - Android - 无法从点击监听器连接到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47295146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com