Android:如何在不使用PHP或任何Web服务的情况下将数据发送到远程数据库?

时间:2013-04-01 07:28:23

标签: android mysql jdbc

我正在尝试将数据从android应用程序发送到localhost MySQL数据库。我在JAVA中创建了应用程序并且工作正常。但是相同的代码在android中不起作用。问题是什么下面是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)

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:0)

我通过将localhost替换为10.0.2.2

来解决