package com.mxy.jdbc;
import com.mxy.jdbc.utils.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 任务:
* 第一:测试DBUtil是否好用
* 第二:模糊查询怎么写
*/
public class JDBCTest12 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//获取连接
conn = DBUtil.getConnection();
//获取预编译的数据库操作对象
//错误的写法
/*String sql = "select name from user where name like '_?%'";
ps = conn.prepareStatement(sql);
ps.setString(1, "A");*/
String sql = "select name from user where name like ?";
ps = conn.prepareStatement(sql);
ps.setString(1, "_子%");
rs = ps.executeQuery();
while(rs.next()){
System.out.println(rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
//释放资源
DBUtil.close(conn, ps, rs);
}
}
}
JDBC 使用工具类 模糊搜索
最新推荐文章于 2022-11-05 13:20:24 发布