首先写一个SQL语句:
String sqlblor=“select * from b_station as s where s.SubordinateRegion LIKE ? or s.SubordinateName LIKE ? or s.StationUnit LIKE ? or s.StationName LIKE ? or s.StationCode LIKE ? limit ?,?”;
然后传入要查询的数据,判断传入的数据不能为空,是否进行模糊查询。
获取页面传过来的数据,
private void selectSt(HttpServletRequest request, HttpServletResponse response) throws IOException {
String find = request.getParameter(“isnot”);
String page = request.getParameter(“page”);
String limit = request.getParameter(“limit”);
List list = new ArrayList();
如果乱码的话,要转换一下格式,代码如下:
request.setCharacterEncoding(“UTF-8”);
response.setCharacterEncoding(“UTF-8”) response.setContentType(“text/html; charset=UTF-8”);
然后查询,查询成功则返回查询结果,否则提示从出错。
public stations pub(int id,String sql) {
stations station=new stations();
if (id==0) {
System.out.println(“ID不能为”+id);
return null;
}
try {
conn = DbUtil.getConnection();// 链接数据库
ps = conn.prepareStatement(sql);// 预执行数据库语句
ps.setInt(1, id);
rs=ps.executeQuery();//返回结果集
while (rs.next()) {
stations st=new stations();
st.setBillIid(rs.getInt("BillID"));
st.setDeparturn(rs.getTimestamp("DepartureDate"));
st.setStationid(rs.getInt("StationID"));
st.setStcode(rs.getString("StationCode"));
st.setStname(rs.getString("StationName"));
st.setStunit(rs.getString("StationUnit"));
st.setSubname(rs.getString("SubordinateName"));
st.setSubre(rs.getString("SubordinateRegion"));
st.setTicket(rs.getTimestamp("Ticket"));
st.setTicketgate(rs.getInt("TicketGate"));
st.setVehicleid(rs.getInt("VehicleID"));
station=st;
}
} catch (SQLException e) {
System.out.println("车站查询出错");
e.getStackTrace();
}finally {
DbUtil.close(conn, ps, rs);//关闭连接
}
return station;
这是我对查询的理解,不是很全面,有可能有错误,如果我理解错了请多多指教!
!](https://img-blog.csdnimg.cn/20201205221023249.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDU2MDc5Ng==,size_16,color_FFFFFF,t_70#pic_center)