移动案例开发-展示从MySQL中查询的信息
创始人
2025-05-28 12:00:54

将从MySQL数据库中查询出来的信息展示出来

引入:使用Handler进行处理,在更新数据的同时更新UI

注意:每次修改之后需要卸载虚拟机中的app再去安装就不会出现bug,不然的话有bug、

1.权限

不联网的话访问不到mysql数据库中的信息。

 

2.数据库连接帮助类

需要采用自己的ip。注意不是localhost,不然就是访问手机自己的ip地址了,并不是电脑的,一定要注意。。。。

public class JDBCConnection {//	创建用于jdbc连接的公用的类
//	先定义几个用于连接的属性
//	1.驱动的名称private final static String driverName="com.mysql.jdbc.Driver";private final static String url="jdbc:mysql://128.45.69.88:3306/testdb?useSSL=false&serverTimezone=GMT";private final static String dbUser="root";private final static String dbPswd="root";//  静态的代码块static {try {Class.forName(driverName);} catch (ClassNotFoundException e) {            e.printStackTrace();}}//	2.创建连接的对象public static Connection getConnection() throws SQLException {Connection conn=null;
        conn= DriverManager.getConnection(url,dbUser,dbPswd);return conn;}//	3.关闭所有的资源对象public static void close(Connection conn, Statement state, ResultSet rs) throws SQLException {if(rs!=null) {
            rs.close();}if(state!=null) {
            state.close();}if(conn!=null) {
            conn.close();}}}

3.主布局文件


 
     
     
     
 
    
        
        
        
    

4.主布局文件的配置文件

public class MainActivity extends AppCompatActivity {protected static   final Integer SUCCESS=1;protected static  final Integer FAIL=0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//查询所有数据的线程new Thread(){@Overridepublic void run() {Connection conn = null;Statement stmt = null;ResultSet rs = null;//采用Message,注意不是new进行创建Message message = handler.obtainMessage();List list = new ArrayList<>();try {
                    conn = JDBCConnection.getConnection();// 3.编写sql并执行sql语句String sql = "select * from user";
                    stmt = conn.createStatement();// 4.遍历结果集
                    rs = stmt.executeQuery(sql);while (rs.next()) {Integer id = rs.getInt("id");String name = rs.getString("name");String sex = rs.getString("sex");System.out.println(id + "---" + name + "---" + sex + "---");
                        list.add(new User(id, name, sex));}//设置成功消息和数据
                    message.what = SUCCESS;
                    message.obj = list;//关闭资源JDBCConnection.close(conn, stmt, rs);} catch (Exception e) {
                    message.what = 0;
                    message.obj = FAIL;System.out.println("发生错误!" + e);}finally {//发生消息
                    handler.sendMessage(message);}}}.start();}
//处理主线程中的信息private Handler handler=new Handler(){@Overridepublic void handleMessage( Message msg) {if (msg.what==SUCCESS){TextView txtId1=findViewById(R.id.txtId1);TextView txtName1=findViewById(R.id.txtName1);TextView txtSex1=findViewById(R.id.txtSex1);TextView txtId2=findViewById(R.id.txtId2);TextView txtName2=findViewById(R.id.txtName2);TextView txtSex2=findViewById(R.id.txtSex2);List list=(List )msg.obj;
            txtId2.setText(list.get(1).getId()+"");
            txtName2.setText(list.get(1).getName()+"");
            txtSex2.setText(list.get(1).getSex()+"");
            txtId1.setText(list.get(0).getId()+"");
            txtName1.setText(list.get(0).getName()+"");
            txtSex1.setText(list.get(0).getSex()+"");Toast.makeText(MainActivity.this, "成功"+msg.obj, Toast.LENGTH_SHORT).show();}}
};}

5.效果图:

 

相关内容

热门资讯

今年我省粮食产量达515.56... (来源:辽宁日报)转自:辽宁日报 图为在中储粮(盘锦)储运有限公司,装运粮食的重型卡车排起长队...
国家发展改革委部署促进投资止跌... (来源:辽宁日报)转自:辽宁日报 新华社北京12月13日电 (记者魏玉坤) 记者13日从全国发展和改...
江苏省实施《中华人民共和国森林... (来源:新华日报) 目 录 第一章 总则 第二章 森林、林木和林地权属管理...
姜堰数字化产品讲“活”理论 (来源:新华日报) □ 本报记者 卢佳乐 通讯员 姜宣 “王教授,您约我‘喝茶论道’,...
联合国维和部队在苏丹遇袭 6人... 转自:财联社【联合国维和部队在苏丹遇袭 6人死亡】财联社12月14日电,当地时间13日,苏丹武装部队...