gpt4 book ai didi

安卓 : How to send data to remote database without using PHP or any webservices?

转载 作者:可可西里 更新时间:2023-11-01 08:35:00 25 4
gpt4 key购买 nike

我正在尝试将数据从 android 应用程序发送到本地主机 MySQL 数据库。我在 JAVA 中创建了应用程序,它工作正常。但同样的代码在android中不起作用。什么问题。下面是安卓的代码。

public class MainActivity extends Activity{

private Connection conn = null;
private String url = "jdbc:mysql://localhost:3306/";
private String dbName = "testDB";
private String driver = "com.mysql.jdbc.Driver";
private String userName = "root";
private String password = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
Statement stmt = conn.createStatement();
String sql = "INSERT INTO test " +
"VALUES ('XYZ')";
stmt.executeUpdate(sql);

conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}


}

我得到的错误是:

04-01 12:52:31.114: W/System.err(910): java.sql.SQLException: Unable to connect to any hosts due to exception: java.net.SocketException: java.net.ConnectException: localhost/127.0.0.1:3306 - Connection refused
04-01 12:52:31.114: W/System.err(910): ** BEGIN NESTED EXCEPTION **
04-01 12:52:31.114: W/System.err(910): java.net.SocketException
04-01 12:52:31.114: W/System.err(910): MESSAGE: java.net.ConnectException: localhost/127.0.0.1:3306 - Connection refused
04-01 12:52:31.114: W/System.err(910): STACKTRACE:
04-01 12:52:31.124: W/System.err(910): java.net.SocketException: java.net.ConnectException: localhost/127.0.0.1:3306 - Connection refused
04-01 12:52:31.124: W/System.err(910): at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:143)
04-01 12:52:31.124: W/System.err(910): at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:225)
04-01 12:52:31.124: W/System.err(910): at com.mysql.jdbc.Connection.createNewIO(Connection.java:1805)
04-01 12:52:31.124: W/System.err(910): at com.mysql.jdbc.Connection.<init>(Connection.java:452)
04-01 12:52:31.124: W/System.err(910): at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
04-01 12:52:31.124: W/System.err(910): at java.sql.DriverManager.getConnection(DriverManager.java:191)
04-01 12:52:31.124: W/System.err(910): at java.sql.DriverManager.getConnection(DriverManager.java:226)
04-01 12:52:31.124: W/System.err(910): at com.example.getip.MainActivity.onCreate(MainActivity.java:138)
04-01 12:52:31.124: W/System.err(910): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-01 12:52:31.124: W/System.err(910): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-01 12:52:31.124: W/System.err(910): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-01 12:52:31.124: W/System.err(910): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-01 12:52:31.124: W/System.err(910): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-01 12:52:31.124: W/System.err(910): at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 12:52:31.124: W/System.err(910): at android.os.Looper.loop(Looper.java:123)
04-01 12:52:31.124: W/System.err(910): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-01 12:52:31.124: W/System.err(910): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 12:52:31.124: W/System.err(910): at java.lang.reflect.Method.invoke(Method.java:521)
04-01 12:52:31.134: W/System.err(910): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-01 12:52:31.134: W/System.err(910): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-01 12:52:31.134: W/System.err(910): at dalvik.system.NativeStart.main(Native Method)
04-01 12:52:31.134: W/System.err(910): ** END NESTED EXCEPTION **
04-01 12:52:31.144: W/System.err(910): at com.mysql.jdbc.Connection.createNewIO(Connection.java:1875)
04-01 12:52:31.144: W/System.err(910): at com.mysql.jdbc.Connection.<init>(Connection.java:452)
04-01 12:52:31.144: W/System.err(910): at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
04-01 12:52:31.156: W/System.err(910): at java.sql.DriverManager.getConnection(DriverManager.java:191)
04-01 12:52:31.156: W/System.err(910): at java.sql.DriverManager.getConnection(DriverManager.java:226)
04-01 12:52:31.156: W/System.err(910): at com.example.getip.MainActivity.onCreate(MainActivity.java:138)
04-01 12:52:31.156: W/System.err(910): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-01 12:52:31.156: W/System.err(910): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-01 12:52:31.156: W/System.err(910): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-01 12:52:31.156: W/System.err(910): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-01 12:52:31.156: W/System.err(910): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-01 12:52:31.156: W/System.err(910): at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 12:52:31.156: W/System.err(910): at android.os.Looper.loop(Looper.java:123)
04-01 12:52:31.156: W/System.err(910): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-01 12:52:31.156: W/System.err(910): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 12:52:31.164: W/System.err(910): at java.lang.reflect.Method.invoke(Method.java:521)
04-01 12:52:31.164: W/System.err(910): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-01 12:52:31.164: W/System.err(910): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-01 12:52:31.164: W/System.err(910): at dalvik.system.NativeStart.main(Native Method)

最佳答案

我通过将 localhost 替换为 10.0.2.2 来解决

关于安卓 : How to send data to remote database without using PHP or any webservices?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15739407/

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