这是运行结果显示,如图,
1、连接mysql的代码——工具类
package com.example.testcourse.util; import android.util.Log; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.HashMap; public class JDBCUtils { public static Connection conn;//连接对象 public static Statement stmt;//命令集 public static PreparedStatement ps;//预编译命令集 public static ResultSet rs;//结果集 private static final String TAG = "mysql11111"; static { try { Class.forName("com.mysql.jdbc.Driver"); Log.v(TAG, "加载JDBC驱动成功"); } catch (ClassNotFoundException e) { Log.e(TAG, "加载JDBC驱动失败"); e.printStackTrace(); } } public static Connection getConn() { try { conn= DriverManager.getConnection("jdbc:mysql://192.168.43.178:3306/mobile?useUnicode=true&characterEncoding=utf-8","root","123456789"); Log.d(TAG, "数据库连接成功"); }catch (Exception exception){ Log.d(TAG, "数据库连接失败"); exception.printStackTrace(); } return conn; } // 关闭数据库操作对象: public static void closeAll() { try { if (conn != null) { conn.close(); conn = null; } if (stmt != null) { stmt.close(); stmt = null; } if (ps != null) { ps.close(); ps = null; } if (rs != null) { rs.close(); rs = null; } } catch (Exception ex) { ex.printStackTrace(); } }
}
2、用户对象——User
package com.example.testcourse.entity; public class User { private int id; private String userName; private String password; private String phone; private String address; public User() { } public User( String userName, String password, String phone, String address) { this.userName = userName; this.password = password; this.phone = phone; this.address = address; } public User(int id, String userName, String password, String phone, String address) { this.id = id; t