我正在尝试将数据从 android 应用程序发送到 localhost 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)