受保护的无效onCreate(捆绑的saveInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jdbc);
新线程(可运行).start();
}
Handler myHandler = new Handler(){
公共无效handleMessage(消息消息){
//TODO自动生成的方法存根
super.handleMessage(msg);
捆绑数据=新的捆绑();
数据= msg.getData();
//System.out.println('id:" + data.get(" id")。ToString());//输出列名称为" id"的第n行的值
Log.e(" TAG"," id:" + data.get(" id")。ToString());
TextView电视=(TextView)findViewById(R.id.jdbc);
//System.out.println('content:" + data.get(" content")。ToString());
}
};
Runnable runnable = new Runnable(){
private Connection con = null;
@Override
公共无效运行(){
//TODO自动生成的方法存根
尝试{
Class.forName(" com.mysql.jdbc.Driver");
//这里的参考代码需要修改,地址是数据IP,端口是端口号,数据库名称是数据名称,用户名称是数据库登录帐户,密码是数据库登录密码
con =//DriverManager.getConnection('jdbc:mysql://192.168.1.202:3306/b2b"," root","");
DriverManager.getConnection(" jdbc:mysql://http://192.168.1.100/phpmyadmin/index.php:8086/b2b",
用户名密码);
} catch(SQLException e){
//TODO自动生成的catch块
e.printStackTrace();
} catch(ClassNotFoundException e){
//TODO自动生成的catch块
e.printStackTrace();
}
尝试{
testConnection(con);//测试数据库连接
} catch(java.sql.SQLException e){
//TODO自动生成的catch块
e.printStackTrace();
}
}
public void testConnection(连接con1)抛出java.sql.SQLException {
尝试{
字符串sql =" select * from ecs_users";//查询表名称" oner_alarm"的所有内容
语句stmt = con1.createStatement();//创建语句
ResultSet rs = stmt.executeQuery(sql);//ResultSet与Cursor相似
//\ lt;代码\ ResultSet \ lt;/代码最初指向第一行
Bundle bundle =新的Bundle();
while(rs.next()){
bundle.clear();
bundle.putString(" id",rs.getString(" userid"));
//bundle.putString('content',rs.getString('content")));
消息msg =新消息();
msg.setData(捆绑);
myHandler.sendMessage(msg);
}
rs.close();
stmt.close();
} catch(SQLException e){
}最后{
如果(con1!= null)
尝试{
con1.close();
} catch(SQLException e){}
}
}
};