Druid的增删改
1.构造一个Student类
封装姓名、年龄、id和电话等属性
public class Student { private String name; private String age; private String id; private String phone; public Student() { } public Student(String name, String age, String id, String phone) { this.name = name; this.age = age; this.id = id; this.phone = phone; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age='" + age + '\'' + ", id='" + id + '\'' + ", phone='" + phone + '\'' + '}'; } }
注意:一定要使用无参的构造函数,否则使用增删改查的时候可能会报错
2.增
public int addInformation(Student student) { Connection conn = null; try { conn = JDBCUtils.getConnection(); String sql = "insert into student values(?,?,?,?)"; QueryRunner runner = new QueryRunner(); int update = runner.update(conn, sql, student.getName(), student.getAge(), student.getId(), student.getPhone()); return update; } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtils.closeResource(conn); } return 0; }
返回操作的个数
3.删
public int deleteInformation(String name) { Connection conn = null; try { conn = JDBCUtils.getConnection(); String sql = "delete from student where `name`=?"; QueryRunner runner = new QueryRunner(); int update = runner.update(conn, sql, name); return update; } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtils.closeResource(conn); } return 0; }
4.改
public int changeInformation(Student student, String name) { Connection conn = null; try { conn = JDBCUtils.getConnection(); String sql = "update student set `name`=?, age=?, id=?, phone=? where name=?"; QueryRunner runner = new QueryRunner(); int update = runner.update(conn, sql, student.getName(), student.getAge(), student.getId(), student.getPhone(), name); return update; } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtils.closeResource(conn); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南