gpt4 book ai didi

android - MySQLNonTransientConnectionException : Could not create connection to database server

转载 作者:可可西里 更新时间:2023-11-01 08:34:54 24 4
gpt4 key购买 nike

我正在处理一个使用 JDBC 连接到 Amazon RDS 上的 MySQL 数据库的奇怪问题。我在同一网络上有两部不同的 Android 手机运行相同的代码。一个连接没有任何问题。另一个给我以下异常:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1014)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2547)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(DriverManager.java:175)
at java.sql.DriverManager.getConnection(DriverManager.java:209)
at edu.ucsb.cs.epsilon.ucsb360.DatabaseManager.Connect(DatabaseManager.java:32)
at edu.ucsb.cs.epsilon.ucsb360.Global.applicationResumed(Global.java:31)
at edu.ucsb.cs.epsilon.ucsb360.MainActivity.onResume(MainActivity.java:54)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1236)
at android.app.Activity.performResume(Activity.java:4625)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2834)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2889)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:5045)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1190)
at java.net.InetAddress.lookupHostByName(InetAddress.java:394)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:245)
at java.net.InetAddress.getAllByName(InetAddress.java:220)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:249)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:305)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2461)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)

数据库只显示 1 个 Activity 连接,所以我知道这不是因为打开的连接太多。我试过重新启动服务器,但没有帮助。我使用的是 MySQL JDBC Connector/J 版本 5.1.23。能用的手机运行的是 Android 2.3.5 版本,不能用的手机运行的是 Android 4.0.4 版本。有没有人知道什么地方可能出了问题?

谢谢!

最佳答案

Caused by: android.os.NetworkOnMainThreadException

您不能在主线程上进行网络 IO。将其移至 AsyncTask 或线程。

关于android - MySQLNonTransientConnectionException : Could not create connection to database server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16051493/

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