Android连接MySQL数据库并进行增删改查操作示例讲解
作者:jianning-wu 时间:2023-07-03 13:20:15
1.Android 连接MySQL数据库
public class DBOpenHelper {
private static String driver = "com.mysql.jdbc.Driver";//MySQL 驱动
private static String url = "jdbc:mysql://IP:3306/数据库";//MYSQL数据库连接Url
private static String user = "root";//用户名
private static String password = "root";//密码
/**
* 连接数据库
* */
public static Connection getConn(){
Connection conn = null;
try {
Class.forName(driver);//获取MYSQL驱动
conn = (Connection) DriverManager.getConnection(url, user, password);//获取连接
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 关闭数据库
* */
public static void closeAll(Connection conn, PreparedStatement ps){
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 关闭数据库
* */
public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs){
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
2.增删改查
public class DBService {
private Connection conn=null; //打开数据库对象
private PreparedStatement ps=null;//操作整合sql语句的对象
private ResultSet rs=null;//查询结果的集合
//DBService 对象
public static DBService dbService=null;
/**
* 构造方法 私有化
* */
private DBService(){
}
/**
* 获取MySQL数据库单例类对象
* */
public static DBService getDbService(){
if(dbService==null){
dbService=new DBService();
}
return dbService;
}
/**
* 获取要发送短信的患者信息 查
* */
public List<User> getUserData(){
//结果存放集合
List<User> list=new ArrayList<User>();
//MySQL 语句
String sql="select * from user";
//获取链接数据库对象
conn= DBOpenHelper.getConn();
try {
if(conn!=null&&(!conn.isClosed())){
ps= (PreparedStatement) conn.prepareStatement(sql);
if(ps!=null){
rs= ps.executeQuery();
if(rs!=null){
while(rs.next()){
User u=new User();
u.setId(rs.getString("id"));
u.setName(rs.getString("name"));
u.setPhone(rs.getString("phone"));
u.setContent(rs.getString("content"));
u.setState(rs.getString("state"));
list.add(u);
}
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
DBOpenHelper.closeAll(conn,ps,rs);//关闭相关操作
return list;
}
/**
* 修改数据库中某个对象的状态 改
* */
public int updateUserData(String phone){
int result=-1;
if(!StringUtils.isEmpty(phone)){
//获取链接数据库对象
conn= DBOpenHelper.getConn();
//MySQL 语句
String sql="update user set state=? where phone=?";
try {
boolean closed=conn.isClosed();
if(conn!=null&&(!closed)){
ps= (PreparedStatement) conn.prepareStatement(sql);
ps.setString(1,"1");//第一个参数state 一定要和上面SQL语句字段顺序一致
ps.setString(2,phone);//第二个参数 phone 一定要和上面SQL语句字段顺序一致
result=ps.executeUpdate();//返回1 执行成功
}
} catch (SQLException e) {
e.printStackTrace();
}
}
DBOpenHelper.closeAll(conn,ps);//关闭相关操作
return result;
}
/**
* 批量向数据库插入数据 增
* */
public int insertUserData(List<User> list){
int result=-1;
if((list!=null)&&(list.size()>0)){
//获取链接数据库对象
conn= DBOpenHelper.getConn();
//MySQL 语句
String sql="INSERT INTO user (name,phone,content,state) VALUES (?,?,?,?)";
try {
boolean closed=conn.isClosed();
if((conn!=null)&&(!closed)){
for(User user:list){
ps= (PreparedStatement) conn.prepareStatement(sql);
String name=user.getName();
String phone=user.getPhone();
String content=user.getContent();
String state=user.getState();
ps.setString(1,name);//第一个参数 name 规则同上
ps.setString(2,phone);//第二个参数 phone 规则同上
ps.setString(3,content);//第三个参数 content 规则同上
ps.setString(4,state);//第四个参数 state 规则同上
result=ps.executeUpdate();//返回1 执行成功
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
DBOpenHelper.closeAll(conn,ps);//关闭相关操作
return result;
}
/**
* 删除数据 删
* */
public int delUserData(String phone){
int result=-1;
if((!StringUtils.isEmpty(phone))&&(PhoneNumberUtils.isMobileNumber(phone))){
//获取链接数据库对象
conn= DBOpenHelper.getConn();
//MySQL 语句
String sql="delete from user where phone=?";
try {
boolean closed=conn.isClosed();
if((conn!=null)&&(!closed)){
ps= (PreparedStatement) conn.prepareStatement(sql);
ps.setString(1, phone);
result=ps.executeUpdate();//返回1 执行成功
}
} catch (SQLException e) {
e.printStackTrace();
}
}
DBOpenHelper.closeAll(conn,ps);//关闭相关操作
return result;
}
}
来源:https://blog.csdn.net/weixin_37730482/article/details/77984417
标签:Android,MySQL,增删改查
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#串口连接的读取和发送详解
2022-05-11 10:04:47
学习SpringBoot容器功能及注解原理
2023-11-24 22:06:17
![](https://img.aspxhome.com/file/2023/4/59044_0s.png)
eclipse创建java项目并运行的详细教程讲解
2022-09-27 20:15:13
![](https://img.aspxhome.com/file/2023/0/101970_0s.jpg)
Android防止按钮重复点击示例代码
2023-10-11 18:15:24
![](https://img.aspxhome.com/file/2023/8/138768_0s.gif)
Springboot整合MybatisPlus的实现过程解析
2021-06-14 02:47:06
java语言注解基础概念详解
2023-04-13 06:55:55
![](https://img.aspxhome.com/file/2023/2/102632_0s.jpg)
解析java基本数据类型传递与引用传递区别
2022-04-14 18:37:19
![](https://img.aspxhome.com/file/2023/8/113458_0s.png)
springcloud项目占用内存好几个G导致服务器崩溃的问题
2023-03-30 09:54:25
![](https://img.aspxhome.com/file/2023/6/61236_0s.png)
图文详解Java中的序列化机制
2021-06-15 12:11:49
![](https://img.aspxhome.com/file/2023/9/77059_0s.png)
Unity3D使用右键菜单打开工程
2023-07-18 05:08:46
![](https://img.aspxhome.com/file/2023/8/104358_0s.png)
C#实现获取不同对象中名称相同属性的方法
2021-11-04 19:26:32
springcloud微服务基于redis集群的单点登录实现解析
2023-05-27 17:10:32
Android 自定义Switch开关按钮的样式实例详解
2023-09-09 16:38:39
![](https://img.aspxhome.com/file/2023/2/138132_0s.png)
利用ThreadLocal实现一个上下文管理组件
2022-04-16 03:42:23
![](https://img.aspxhome.com/file/2023/2/119122_0s.png)
C#解决SQlite并发异常问题的方法(使用读写锁)
2023-08-03 04:06:02
SpringBoot中实现接收文件和对象
2022-02-08 10:16:53
![](https://img.aspxhome.com/file/2023/0/79280_0s.png)
JAVA8之函数式编程Function接口用法
2022-01-04 04:03:04
![](https://img.aspxhome.com/file/2023/6/63276_0s.jpg)
Android Studio 超级简单的打包生成apk的方法
2023-08-07 18:57:28
![](https://img.aspxhome.com/file/2023/6/118926_0s.png)
C#引用类型转换的常见方式总结
2022-03-02 16:53:58
Android开发笔记之如何正确获取WebView的网页Title
2022-06-04 07:02:41
![](https://img.aspxhome.com/file/2023/0/139500_0s.jpg)