Android的Java的ODBC连接 [英] Android Java ODBC Connection

查看:244
本文介绍了Android的Java的ODBC连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里与Android的另一个问题...
我试图与code连接到我的mysql数据库:

i am here with another problem about android... I am trying to connect to my mysql database with that code:

  public void testDB() {
    TextView tv = (TextView)this.findViewById(R.id.textView1);
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(url, user, pass);
        /* System.out.println("Database connection success"); */

        String result = "Database connection success\n";
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select * from users");
        ResultSetMetaData rsmd = rs.getMetaData();

        while(rs.next()) {
            result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
            result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
            result += rsmd.getColumnName(3) + ": " + rs.getString(3) + "\n";
        }
        tv.setText(result);
    }
    catch(Exception e) {
        e.printStackTrace();
        tv.setText(e.toString());
    }   

}

在AVD我得到这个错误:

In the AVD i get this error:

10-23 16:49:08.103: W/System.err(12939): java.sql.SQLException: Unable to connect to any hosts due to exception: android.os.NetworkOnMainThreadException

我觉得有什么不对劲的库,是真的吗?
但我tryied这对bluestack模拟器,与Android姜饼我想和连接工作好...
是否有人知道我该怎么办?

I think there's something wrong in the libs, is that true? But i tryied this on bluestack emulator, with android gingerbread i think and the connection work good... Does someone know how can i do?

推荐答案

你得到的 NetworkOnMainThreadException 。随着蜂窝和转发,你不能做对你的主线程,这意味着你需要移动你的code为独立的线程任何网络任​​务。我建议一个 AsyncTask的

You're getting a NetworkOnMainThreadException. With Honeycomb and forward, you can't do any networking tasks on your main thread, which means you need to move your code off into a separate thread. I'd suggest an AsyncTask.

这篇关于Android的Java的ODBC连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆