Android学习之链接mysql数据库

来源:互联网 发布:土著勇士升级数据 编辑:程序博客网 时间:2024/06/05 03:03

和java连接数据库一样,需要mysql-connector-java-版本号-bin.jar包导入到工程libs文件夹里面去

安卓里面连接数据库要放到一个线程里面去,因为主线程不能进行耗时操作

//数据库地址密码    String url = "jdbc:mysql://ip地址:3306/数据库名";    String username = "root";    String password = "***";    private void Connect() {        //initVideos();        currIndex = 0;        //连接数据库线程        new Thread(new Runnable() {            private Connection con = null;            @Override            public void run() {                // TODO Auto-generated method stub                try {                    Class.forName("com.mysql.jdbc.Driver");                    con = DriverManager.getConnection(url, username, password);                } catch (SQLException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                } catch (ClassNotFoundException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                try {                    testConnection(con);    //测试数据库连接                } catch (SQLException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }            public void testConnection(Connection con1) throws SQLException {                try {                    String sql = "sql语句";                    Statement stmt = con1.createStatement();        //创建Statement                    ResultSet rs = stmt.executeQuery(sql);          //ResultSet类似Cursor                    //<code>ResultSet</code>最初指向第一行                    Bundle bundle = new Bundle();                    while (rs.next()) {                        //System.out.println("db:"+rs.getString("name"));                        //System.out.println("db:"+rs.getString("address"));                        bundle.clear();                        bundle.putString("name", rs.getString("name"));                        bundle.putString("address", rs.getString("address"));                        bundle.putString("date",rs.getString("time"));                        bundle.putInt("v_or_m",rs.getInt("v_or_m"));                        Message msg = new Message();                        msg.what = UPDATE_list;                        msg.setData(bundle);
                        myHandler.sendMessage(msg);                        try{                            Thread.sleep(150);//防止sendMessage传送速度过慢,稍微延时                        }catch (Exception e){                            e.printStackTrace();                        }                    }                    rs.close();                    stmt.close();                } catch (SQLException e) {                } finally {                    if (con1 != null)                        try {                            con1.close();                        } catch (SQLException e) {                        }                }            }        }).start();    }
另外,如果想让读出来的数据显示在界面上,要用到Handler这个类,因为线程里面不能够更改UI

Handler myHandler = new Handler(){        @Override        public void handleMessage(Message msg){            super.handleMessage(msg);            // TODO Auto-generated method stub            switch(msg.what) {                case UPDATE_list: {                    Bundle data = new Bundle();                    data = msg.getData();                    ...                }break;            }        }    };



原创粉丝点击