}
// ps=ct.prepareStatement(\ start=(pageNow-1)*pageSize;
ps=ct.prepareStatement(\ rs=ps.executeQuery(); while(rs.next()){ //将rs中的每条记录封装到UserBean ub UserBean ub=new UserBean(); ub.setUserId(rs.getInt(1)); ub.setUserName(rs.getString(2)); ub.setPassword(rs.getString(3)); ub.setEmail(rs.getString(4)); ub.setGrade(rs.getInt(5)); //将ub放入arrayliat al.add(ub); } } catch(Exception ex){ ex.printStackTrace(); } finally{ this.close(); } return al; }
//验证用户
public boolean checkUser(String u,String p){ boolean b=false; try{ //得到链接 ConnDB cd=new ConnDB(); ct=cd.getConn(); ps=ct.prepareStatement(\ rs=ps.executeQuery(); if(rs.next()){ //说明用户存在 String dbPassword=rs.getString(1); if(dbPassword.equals(p)){ //真的合法 b=true; } } }
catch (Exception ex){ ex.printStackTrace(); }
finally{ this.close(); }
return b; }
//关闭资源
public void close(){ try{ if(rs!=null){ rs.close(); } if(ps!=null){ ps.close(); } if(ct!=null){ ct.close(); } }
catch (Exception ex){ ex.printStackTrace();
} } }
十、ServletContext
向ServletContext添加属性
ServletContext sc=this.getServletContext(); Sc.setAttribute(“myInfo”,”我是”);
得到ServletContext
ServletContext sc=this.getServletContext(); String info=(String)sc.getAttribute(“myInfo”);
如何在ServletContext操作文件 PrintWriter pw = res.getWriter(); //创建一个FileReader
FileReader f=new FileReader(“f:\\\\myCount.txt”); BufferedReader bw=new BufferedReader(f); //读出一行数据
String numVal=bw.readLine(); //一定要关闭文件流
Bw.close();
Pw.println(“读出”+numVal);
如何在ServletContext创建文件 PrintWriter pw = res.getWriter();
//创建一个FileWriter,从内存数据向磁盘文件(即写文件,输出流) FileWriter f=new FileWriter(“f:\\\\myCount.txt”); BufferedReader bw=new BufferedReader(f); //写入数据
Bw.writer(“123”); //一定要关闭文件流 Bw.close();
Pw.println(“已经写入了数据”);
网站计数器
LoginCl
Welcome
优化
servletContext 最后再写入文件 文件
LoginCl