连接到MySQL从Android的使用JDBC

问题描述:

我用下面的code在本地主机从Android的连接的MySQL 。它只是显示在追赶节给出的动作。我不知道它是否是一个连接问题或没有。

I used the following code to connect MySQL in localhost from Android. It only displays the actions given in catch section . I do not know whether it is a connection problem or not.

package com.test1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Test1Activity extends Activity {
    /** Called when the activity is first created. */
    String str="new";
    static ResultSet rs;
    static PreparedStatement st;
    static Connection con;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final   TextView tv=(TextView)findViewById(R.id.user);

        try
        {
            Class.forName("com.mysql.jdbc.Driver");
              con=DriverManager.getConnection("jdbc:mysql://10.0.2.2:8080/example","root","");
            st=con.prepareStatement("select * from country where id=1");
            rs=st.executeQuery();
             while(rs.next())
             {
             str=rs.getString(2);


             }


            tv.setText(str);
            setContentView(tv);
        }
        catch(Exception e)
        {
            tv.setText(str);
        }
    }
}

在此code执行它会显示新的AVD。

When this code executes it displays "new" in the avd.

java.lang.management.ManagementFactory.getThreadMXBean, referenced from method com.mysql.jdbc.MysqlIO.appendDeadlockStatusInformation
Could not find class 'javax.naming.StringRefAddr', referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.storeTo
Could not find method javax.naming.Reference.get, referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.initializeFrom

任何人都可以提出一些解决方案?而在此先感谢

Can anyone suggest some solution? And thanks in advance

您不能访问MySQL数据库从Android的原生。编辑:其实你可以使用JDBC,但不推荐(或可能不工作?)......看到Android JDBC不工作:ClassNotFoundException的驾驶员

You can't access a MySQL DB from Android natively. Actually you may be able to use JDBC, but it is not recommended (or may not work?) ... see Android JDBC not working: ClassNotFoundException on driver

请参阅

http://www.helloandroid.com/tutorials/connecting-mysql-database

http://www.basic4ppc.com/forum/basic4android-getting-started-tutorials/8339-connect-android-mysql-database-tutorial.html

Android无法直接连接到数据库服务器。因此,我们   需要创建一个简单的Web服务,将传递请求到   数据库将返回的响应。

Android cannot connect directly to the database server. Therefore we need to create a simple web service that will pass the requests to the database and will return the response.

http://$c$concloud.blogspot.com/2012/03/android-mysql-client.html

对于大多数[好]的用户,这可能是罚款。但是想象一下,你会得到一个黑客,获取一抱你的程序。我反编译我自己的应用程序和它的可怕我所看到的。如果他们得到您的用户名/密码到您的数据库和肆虐?坏的。

For most [good] users this might be fine. But imagine you get a hacker that gets a hold of your program. I've decompiled my own applications and its scary what I've seen. What if they get your username / password to your database and wreak havoc? Bad.