实现批量删除和模糊查询
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();
}
}
}
}
}
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();
}
}
}
}
}
可以达到删除效果,但是永远删除的是第一行数据
总结:
模糊查询,批量删除里面不能用占位符