Conn.java连接数据库
public ResultSet executeQuery(String sql) throws SQLException
{
try {
con=DriverManager.getConnection(url, user, pwd);
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE ,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}catch(SQLException e1) {
System.err.println(e1.getMessage());
}
if(rs!=null) {
return rs;
}
else
{
return null;
}
}
SearchAll.java 调用连接数据库方法和查询语句方法返回rs
public ResultSet Search() throws SQLException
{
String sql="select * from Good";
ResultSet rs=con.executeQuery(sql);
if(rs!=null){
//输出看一下是否有值
while(rs.next())
System.out.println(rs.getString("destination"));
return rs;
}
else {
System.out.println("rs==null");
return null;
}
}
}
SearchServlet.java 获取SearchAll中的rs保存到list中传给jsp
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
//TODO Auto-generater method stub
ArrayList<SearchAll> list=new ArrayList<SearchAll>();
response.setContentType("text/html;charset=utf-8");//设置响应MIME类型
request.setCharacterEncoding("utf-8");
java.io.PrintWriter out=response.getWriter();
SearchAll s=new SearchAll();
ResultSet rs=null;
try {
rs = s.Search();
if(rs!=null) {
while(rs.next()) {
s.setGoodName(rs.getString("goodname"));
s.setGoodId(rs.getString("goodId"));
s.setNumber(rs.getString("number"));
s.setInName(rs.getString("inName"));
s.setDestination(rs.getString("destination"));
s.setIntime(rs.getString("intime"));
s.setOutcheck(rs.getString("outcheck"));
list.add(s);
}}
else
System.out.println("null");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.setAttribute("list",list);
request.getRequestDispatcher("/Root/CheckTable.jsp").forward(request, response);
}
}