0
点赞
收藏
分享

微信扫一扫

实现批量删除和模糊查询

GG_lyf 2022-11-18 阅读 76


实现批量删除和模糊查询

1、实现批量删除:

package test_path;

import org.junit.Test;

import java.sql.*;

public class MoHuAndDeleteTest {

@Test
public void testMoMu(){
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
String mohu="a";
try{
//1、注册驱动
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1", "root", "root");
ps=conn.prepareStatement("select * from t_user where uname like concat('%',?,'%')");
ps.setObject(1,mohu);
rs = ps.executeQuery();
while (rs.next()){
System.out.println(rs.getInt(1)+","+rs.getString(2)+","+rs.getString(3));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}
}

实现批量删除和模糊查询_sql

实现批量删除和模糊查询_mysql_02


实现批量删除和模糊查询_sql_03

2、实现模糊查询:

package test_path;

import org.junit.Test;

import java.sql.*;

public class MoHuAndDeleteTest {

@Test
public void testMoMu(){
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
String ids="1,2,3";
try{
//1、注册驱动
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1", "root", "root");
ps=conn.prepareStatement("delete from t_user where id in(?)");
ps.setObject(1,ids);
int i = ps.executeUpdate();
System.out.println("结果:"+i);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}
}

实现批量删除和模糊查询_sql_04


可以达到删除效果,但是永远删除的是第一行数据

实现批量删除和模糊查询_sql_05


总结:

模糊查询,批量删除里面不能用占位符

实现批量删除和模糊查询_sql_06


举报

相关推荐

0 条评论