问题
android 开发过程中, 使用jdbc连接mysql 出现该错误 Caused by: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 3306) from /:: (port 37472):
详细错误
2022-09-05 19:56:55.951 22490-22631/? W/System.err: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
2022-09-05 19:56:55.951 22490-22631/? W/System.err:
2022-09-05 19:56:55.951 22490-22631/? W/System.err: The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2022-09-05 19:56:55.951 22490-22631/? W/System.err: at java.lang.reflect.Constructor.newInstance0(Native Method)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:989)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:341)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2251)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at java.lang.reflect.Constructor.newInstance0(Native Method)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at java.sql.DriverManager.getConnection(DriverManager.java:580)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at java.sql.DriverManager.getConnection(DriverManager.java:218)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at com.example.electronicmall.Mapper.MYSQLDBOpenHelper.getConn(MYSQLDBOpenHelper.java:20)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at com.example.electronicmall.test.All.AllLoginActivity$3.run(AllLoginActivity.java:194)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at java.lang.Thread.run(Thread.java:929)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: Caused by: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 3306) from /:: (port 37472): connect failed: ECONNREFUSED (Connection refused)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:143)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:142)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at java.net.Socket.connect(Socket.java:621)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
2022-09-05 19:56:55.952 22490-22631/? W/System.err: ... 15 more
2022-09-05 19:56:55.953 22490-22631/? W/System.err: Caused by: android.system.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
2022-09-05 19:56:55.953 22490-22631/? W/System.err: at libcore.io.Linux.connect(Native Method)
2022-09-05 19:56:55.953 22490-22631/? W/System.err: at libcore.io.ForwardingOs.connect(ForwardingOs.java:95)
2022-09-05 19:56:55.953 22490-22631/? W/System.err: at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:136)
2022-09-05 19:56:55.953 22490-22631/? W/System.err: at libcore.io.ForwardingOs.connect(ForwardingOs.java:95)
2022-09-05 19:56:55.953 22490-22631/? W/System.err: at libcore.io.IoBridge.connectErrno(IoBridge.java:157)
2022-09-05 19:56:55.953 22490-22631/? W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:135)
2022-09-05 19:56:55.953 22490-22631/? W/System.err: ... 23 more
2022-09-05 19:56:55.953 22490-22631/? I/QarthLog: [PatchStore] createDisableExceptionQarthFile
2022-09-05 19:56:55.953 22490-22631/? I/QarthLog: [PatchStore] create disable file for com.example.electronicmall uid is 10254
2022-09-05 19:56:55.954 22490-22631/? E/AndroidRuntime: FATAL EXCEPTION: Thread-3
Process: com.example.electronicmall, PID: 22490
java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference
at com.example.electronicmall.test.All.AllLoginActivity$3.run(AllLoginActivity.java:198)
at java.lang.Thread.run(Thread.java:929)
2022-09-05 19:56:55.970 22490-22490/? W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@70c5d0d
2022-09-05 19:56:55.975 22490-22631/? I/Process: Sending signal. PID: 22490 SIG: 9
解决
将JDBC的url链接中的ip地址由localhost改为局域网内本机的ip( w i n + R win + R win+R → \rightarrow → i p c o n f i g ipconfig ipconfig → \rightarrow →无线局域网适配器 WLAN: → \rightarrow →IPv4 地址)
原因
若JDBC中url中的ip地址为localhost, 安卓虚拟机认为是本机(127.0.0.1),若将ip改为10.0.2.2或局域网内本机的ip,即可在安卓虚拟机上连接电脑的本地数据库。
参考文献
[https://www.jianshu.com/p/10799e77e532](安卓连接mysql报错Caused by: java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 3…)
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈