JDBC的模糊查询

1 模糊查询的基础版代码如下:
因为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<Student> 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();
}
}

2 模糊查询的进阶版代码如下:
在mysql中 concat() 函数使用来做字符串的拼接; select * from student where stuName like concat('%',?,'%') 的意思就是自动给 ? 拼接前后的 % ; 那么在传参的时候直接把关键词传参给 ? 即可,不在需要在传参的时候进行拼接。 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 concat('%',?,'%')";
PreparedStatement pstm = con.prepareStatement(sql);
//5.传参并执行sql
String keyword="a"; //后期是通过页面获取用户输入的关键词
pstm.setObject(1,keyword);
ResultSet rs = pstm.executeQuery(); //直接把关键词传参给预处理对象的 ?
//6.解析结果集,模糊查询有可能查到很多条数据,所以定义集合存储
List<Student> 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();
}
}

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("请修改年龄:");
模糊查询一般是通过一个输入框输入关键词,然后点击搜索进行检索,执行的是数据的模糊查询;
语句示例:select * from student where stuName like '%关键词%';
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();
}
}

删除

/**
* 删除操作
*/
@Test
public void testDel() throws Exception {
//1.导入驱动程序包
//2.通过反射加载驱动程序
Class.forName(driver);
//3.通过驱动管理器获得数据库的连接对象
Connection con = DriverManager.getConnection(url, username, password);
//4.通过数据库连接对象获取sql预处理对象
String sql="delete from student where stuId=?";
PreparedStatement pstm = con.prepareStatement(sql);
//5.1预处理对象的sql语句有 ? ,所以需要传参
int stuId=3; //实际开发是从前端页面获取要删除的id
pstm.setObject(1,stuId);
//5.2执行更新操作(增删改都是更新操作,返回的结果是影响的行数)
int n=pstm.executeUpdate();
//6判断影响的行数 n>0 表示删除成功,否则删除失败
if(n>0){
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
//7释放资源
if(pstm!=null){
pstm.close();
}
if(con!=null){
con.close();
}
}

添加

/**
* 添加操作
*/
@Test
public void testAdd() throws Exception {
//1.导入驱动包
//2.通过反射加载驱动程序
Class.forName(driver);
//3.通过驱动管理器获得数据库的连接对象
Connection con = DriverManager.getConnection(url, username, password);
//4.通过连接对象,获取SQ预处理对象
String sql="insert into student(stuName,stuSex,stuAge,stuAddr)
values(?,?,?,?)";
PreparedStatement pstm = con.prepareStatement(sql);
//实际开发是前端页面传递过来的数据,此处我们直接模拟一组数据
Student student=new Student();
student.setStuName("张三");
student.setStuSex("男");
student.setStuAge(18);
student.setStuAddr("南阳");
//5.1预处理对象的sql语句有 ? 所以需要进行传参
pstm.setObject(1,student.getStuName());
pstm.setObject(2,student.getStuSex());
pstm.setObject(3,student.getStuAge());
pstm.setObject(4,student.getStuAddr());
//5.2执行更新(增删改都叫做数据库的更新,更新返回的是影响的行数)
int n = pstm.executeUpdate();
//6判断影响的行数 n > 0 表示插入成功,否则插入失败
if(n>0){
System.out.println("插入数据成功");
}else{
System.out.println("插入数据失败");
}
//7释放资源
if(pstm!=null){
pstm.close();
}
if(con!=null){
con.close();
}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值