JDBC id查询、修改、模糊查询

本文介绍了使用Java JDBC进行数据库操作的步骤,包括根据ID查询学生信息,详细讲解了如何加载数据库驱动、建立连接、执行查询和修改操作。在数据修改部分,分为三个步骤:查询原始数据、更新字段值、执行修改操作。同时,还阐述了如何实现模糊查询,以关键词匹配实现对学生信息的搜索,展示了如何处理SQL参数并结合用户输入进行查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据id查询学生信息的代码

JDBC操作数据库的步骤
//1.首先在项目根目录创建lib文件夹,放入jdbc驱动程序,然后Add As Library

//2.加载数据库驱动

//3.使用驱动管理器来获得连接---获得一个数据库连接对象Connection

//4.使用Connection创建PreparedStatement语句对象---PreparedStatement对象可以执行sql语句

//5.使用PreparedStatement对象执行SQL语句

//6.操作判断--增删改返回的是影响的行数(返回值是int),只有查询获得结果集(返回值ResultSet)

//7.回收资源

/** * 根据id查询用户的信息 * @param id :要查询的用户id编号 * @return :数据库中该用户所有的字段封装的Student对象 * @throws Exception */ 
public Student selectById(int id) throws Exception {
 //JDBC操作数据库的步骤 
//1.首先在项目根目录创建lib文件夹,放入jdbc驱动程序,然后Add As Library 
//2.加载数据库驱动,使用反射加载
 Class.forName(driver);
//3.使用驱动管理器来获得连接---获得一个数据库连接对象
Connection Connection con = DriverManager.getConnection(url, user, password);
 //4.使用Connection创建PreparedStatement预处理对象---PreparedStatement对象可以 对sql语句预处理,sql可以带 ? 
String sql="select * from student where stuId=?";
 PreparedStatement pstm = con.prepareStatement(sql);
 //5.使用PreparedStatement对象执行SQL语句,查询返回的是结果集,增删改返回的是影响的 行数(int) pstm.setObject(1,id);
 ResultSet rs = pstm.executeQuery();
 //6.操作判断--增删改返回的是影响的行数(返回值是int),只有查询获得结果集(返回值 ResultSet) //ResultSet结果集的游标默认指向的是表的标题行,需要让游标向下移动指向数据行。 完成修改操作的测试代码
 Student student=null; 
if (rs.next()){ //根据字段名称获取字段的值 
int stuId=rs.getInt("stuId");
 String stuName=rs.getString("stuName");
 String stuSex=rs.getString("stuSex");
 int stuAge=rs.getInt("stuAge"); 
String stuAddr=rs.getString("stuAddr");
 //把以上数据装载到Student对象中 
student=new Student(); 
student.setStuId(stuId);
 student.setStuName(stuName);
 student.setStuSex(stuSex);
 student.setStuAge(stuAge); 
student.setStuAddr(stuAddr);
 }
 //7.回收资源
 if(rs!=null){
 rs.close(); 
}
 if(pstm!=null){ 
pstm.close(); 
} 
if(con!=null){
 con.close(); 
} 
return student; }

通过调用ID查询的数据来进行修改 

第一步:根据id先查询原始数据 

//第二步:根据需要修改原始数据的字段值 (后期学习是用前端html页面修改数据,然后传输过 来新的数据) 

//第三步:修改字段值后把最新的数据执行数据库JDBC的修改操作

/** * 修改操作 * @throws Exception */
 @Test public void testUpdate() throws Exception { 
//===========修改的业务逻辑===================
 //第一步:根据id先查询原始数据 
Student student = selectById(4);
 System.out.println("修改前:"+student); 
//第二步:根据需要修改原始数据的字段值 (后期学习是用前端html页面修改数据,然后传输过 来新的数据)
 Scanner sc=new Scanner(System.in);
 System.out.println("请修改名字:");
 String newName=sc.next(); 
student.setStuName(newName);
 System.out.println("请修改性别:"); 
String newSex=sc.next();
 student.setStuSex(newSex);
 System.out.println("请修改年龄:");
int newAge=sc.nextInt();
student.setStuAge(newAge);
System.out.println("请修改地址:");
String newAddr=sc.next();
student.setStuAddr(newAddr);
System.out.println("修改后:"+student);
//第三步:修改字段值后把最新的数据执行数据库JDBC的修改操作
//1.导入jar包
//2.使用反射加载驱动程序
Class.forName(driver);
//3.使用驱动管理器获得数据库的连接
Connection con = DriverManager.getConnection(url, user, password);
//4.使用连接对象获取SQL的预处理对象
String sql="update student set stuName=?,stuSex=?,stuAge=?,stuAddr=?
where stuId=?";
PreparedStatement pstm = con.prepareStatement(sql);
//5.传参并执行SQL
pstm.setObject(1,student.getStuName());
pstm.setObject(2,student.getStuSex());
pstm.setObject(3,student.getStuAge());
pstm.setObject(4,student.getStuAddr());
pstm.setObject(5,student.getStuId());
int n = pstm.executeUpdate();
//6.判断执行结果
if(n>0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
//7.释放资源
if(pstm!=null){
pstm.close();
}
if(con!=null){
con.close();
}

模糊查询

模糊查询一般是通过一个输入框输入关键词,然后点击搜索进行检索,执行的是数据的模糊查询; 语句示例:select * from student where stuName like '%关键词%';

因为sql语句是 select * from student where stuName like ?;

所以传参的时候需要对关键词进行前后 % 的拼接: String keyword="a";

//后期是通过页面获取用户输入的关键词 pstm.setObject(1,"%"+keyword+"%");

//对关键词进行前后%的拼接

/** * 模糊查询 */
 @Test public void testSeach() throws Exception { 
//1.导入jar包 
//2.加载驱动 Class.forName(driver);
 //3.获取数据库连接 Connection con = DriverManager.getConnection(url, username, password); //4.获取预处理对象 String sql="select * from student where stuName like ?"; PreparedStatement pstm = con.prepareStatement(sql); 
//5.传参并执行sql String keyword="a";
 //后期是通过页面获取用户输入的关键词 pstm.setObject(1,"%"+keyword+"%");
 //对关键词进行前后%的拼接 ResultSet rs = pstm.executeQuery(); 
//6.解析结果集,模糊查询有可能查到很多条数据,所以定义集合存储 
List studentList=new ArrayList<>(); while (rs.next()){ 
Student student=new Student(); 
//通过rs根据字段名获取数据库表中的数据,然后把数据封装到Student对象中 student.setStuId(rs.getInt("stuId"));
 student.setStuName(rs.getString("stuName")); 
student.setStuSex(rs.getString("stuSex"));
 student.setStuAge(rs.getInt("stuAge")); 
student.setStuAddr(rs.getString("stuAddr"));
 //把当前的student对象存储到list集合中 
studentList.add(student);
 }
 //打印输出 
System.out.println(studentList); 
//7.资源的释放 if(rs!=null){
 rs.close();
 } if(pstm!=null){
 pstm.close(); 
} if(con!=null){ 
con.close(); 
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值