ps:这个代码用的是韩顺平老师的servlet课程中的代码,稍作修改。
更新页面:其实重要的还是sql语句,一定要打牢基础。
//修改用户界面
package com.tsinghua;
import javax.servlet.http.*;
import java.io.*;
public class Update extends HttpServlet {
//处理get请求
//req: 用于获得客户端(浏览器)的信息
//res: 用于向客户端(浏览器)返回信息
public void doGet(HttpServletRequest req,HttpServletResponse res){
//业务逻辑
try {
//中文乱码
res.setContentType("text/html;charset=gbk");
PrintWriter pw=res.getWriter();
//修改有个表格,下面就是显示修改那个表格。
pw.println("<html>");
pw.println("<body bgcolor=#CED3FF>");
pw.println("<img src=imgs/1.GIF><hr><center>");
pw.println("<h1>修改用户界面h1>");
//跳转到更新用户处理的页面上。action就是跳转。
pw.println("<form action=UpdateCl>");
pw.println("<table border=1>");
//值是获取过来的,这个值的传递也不是很好,这种传递方式在地址栏中会看到,
//正确的是弄个数据bean什么的。
pw.println("<tr><td>idtd><td><input readonly name=uId type=text value="+req.getParameter("uId")+">td>tr>");
pw.println("<tr><td>nametd><td><input readonly type=text value="+req.getParameter("uName")+">td>tr>");
pw.println("<tr><td>passwdtd><td><input name=newPasswd type=text value="+req.getParameter("uPass")+">td>tr>");
pw.println("<tr><td>emailtd><td><input name=newEmail type=text value="+req.getParameter("uMail")+">td>tr>");
pw.println("<tr><td>gradetd><td><input name=newGrade type=text value="+req.getParameter("uGrade")+">td>tr>");
pw.println("<tr><td colspan=2><input type=submit value=修改用户>td>tr>");
pw.println("table>form>");
pw.println("center><hr><img src=imgs/mylogo.gif>");
pw.println("body>");
pw.println("html>");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
//处理post请求
//req: 用于获得客户端(浏览器)的信息
//res: 用于向客户端(浏览器)返回信息
public void doPost(HttpServletRequest req,HttpServletResponse res){
this.doGet(req,res);
}
}
上面也就是用于接受几个数据,真正的处理是在UpdateCl里面。
//处理修改某个用户
package com.tsinghua;
import javax.servlet.http.*;
import java.io.*;
public class UpdateCl extends HttpServlet {
//处理get请求
//req: 用于获得客户端(浏览器)的信息
//res: 用于向客户端(浏览器)返回信息
public void doGet(HttpServletRequest req,HttpServletResponse res){
//业务逻辑
try {
//中文乱码
res.setContentType("text/html;charset=gbk");
//调用userBeancl的删除用户的方法,完成删除
UserBeanCl ubc=new UserBeanCl();
//接收从wel.java中传递的id
//接受了id的值,之后调用UserBeanCl的方法去删除。
//下面看着挺长的,其实就是调用了updateUser函数,里面的参数,刚拿到,还没捂热乎就直接传到另一个函数中了。
if(ubc.updateUser(req.getParameter("uId"),
req.getParameter("newEmail"),req.getParameter("newPasswd"),req.getParameter("newGrade"))){
//删除成功!
//跳转到成功弄页面。
res.sendRedirect("Ok");
}else{
//失败
res.sendRedirect("Err");
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
//处理post请求
//req: 用于获得客户端(浏览器)的信息
//res: 用于向客户端(浏览器)返回信息
public void doPost(HttpServletRequest req,HttpServletResponse res){
this.doGet(req,res);
}
}
userBean:
这个数据结构是和数据库对应的,对应着表结构。
它的一个对象在项目中通常是传递数据的角色。
但是在这个项目中用他的对象传递数据没怎么用。
//这是一个UserBean <---->users表映射
//他的一个对象<---->users表的一条记录对应
//数据
package com.tsinghua;
public class UserBean {
private int userId;
private String userName;
private String passwd;
private String mail;
private int grade;
public void setUserId(int userId){
this.userId=userId;
}
public int getUserId(){
return this.userId;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public void setMail(String mail) {
this.mail = mail;
}
public void setGrade(int grade) {
this.grade = grade;
}
public String getUserName() {
return (this.userName);
}
public String getPasswd() {
return (this.passwd);
}
public String getMail() {
return (this.mail);
}
public int getGrade() {
return (this.grade);
}
}
//这是一个处理类(处理users表)<--->操作UserBean
//业务逻辑在这里
//这是一个很关键的代码,对数据库的操作都是这个类中提供的方法。
package com.tsinghua;
import java.sql.*;
import java.util.*;
public class UserBeanCl {
//业务逻辑
private Connection ct=null;
private PreparedStatement ps=null;
private ResultSet rs=null;
private int pageCount=0;//共有几页(计算)
//修改用户
public boolean updateUser(String id,String email,String passwd,String grade){
boolean b=false;
try {
//得到连接
ConnDB cd=new ConnDB();
ct=cd.getConn();
//这个处理类是要操作数据库的,总得有个东西去操作数据库吧?
String sql="update users set passwd='"+passwd+"' ,email='"+email+"'
,grade='"+grade+"' where userid='"+id+"'";
//打印
System.out.println ("sql===="+sql);
ps=ct.prepareStatement(sql);
int num=ps.executeUpdate();
if(num==1){
//删除成功!
b=true;
}
}
catch (Exception ex) {
ex.printStackTrace();
}finally{
this.close();
}
return b;
}
//删除用户
public boolean delUser(String id){
boolean b=false;
try {
//得到连接
ConnDB cd=new ConnDB();
ct=cd.getConn();
String sql="delete from users where userid='"+id+"'";
//其实套路都是一样的,写好一个sql语句去执行。
ps=ct.prepareStatement(sql);
int num=ps.executeUpdate();
if(num==1){
//删除成功!
b=true;
}
}
catch (Exception ex) {
ex.printStackTrace();
}finally{
this.close();
}
return b;
}
//返回pageCount;
public int getPageCount(){
return this.pageCount;
}
//分页显示
//分页要用ArrayList,因为用ResultSet的话,关闭数据库之后会失效。
public ArrayList getResultByPage(int pageNow,int pageSize){
ArrayList al=new ArrayList();
try {
int rowCount=0;//共有几条记录(查表)
//得到rowCount
ConnDB cd=new ConnDB();
ct=cd.getConn();
ps=ct.prepareStatement("select count(*) from users");
rs=ps.executeQuery();
if(rs.next()){
rowCount=rs.getInt(1);
}
//计算pageCount
if(rowCount%pageSize==0){
pageCount=rowCount/pageSize;
}else{
pageCount=rowCount/pageSize+1;
}
//注意第几页第几页是现查出来的,将数据pageNow传递进去现查。
ps=ct.prepareStatement("select top "+pageSize+" * from users where userId not in (select top "+pageSize*(pageNow-1)+" userId from users)");
rs=ps.executeQuery();
//注意此时得到的是一个ResultSet结果集(我的理解是个cursor工厂那种的东西)
//然后遍历其中的结果,每一条装到一个bean中。
//最后这一堆bean依次添加到ArrayList中。
//再说一次放到ArrayList中是因为rs关闭之后rs就没用有了。
while(rs.next()){
//将rs中的 每条记录封装到UserBean ub
UserBean ub=new UserBean();
ub.setUserId(rs.getInt(1));
ub.setUserName(rs.getString(2));
ub.setPasswd(rs.getString(3));
ub.setMail(rs.getString(4));
ub.setGrade(rs.getInt(5));
//将ub ,放入到ArrayList 中
al.add(ub);
}
}
catch (Exception ex) {
ex.printStackTrace();
}finally{
this.close();
}
return al;
//这个方法返回的是一个ArrayList。在wel界面用到。
}
//验证用户
public boolean checkUser(String u,String p){
boolean b=false;
try {
//得到连接
ConnDB cd=new ConnDB();
ct=cd.getConn();
ps=ct.prepareStatement("select top 1 passwd from users where username=?");
ps.setString(1,u);
rs=ps.executeQuery();
if(rs.next()){
String dbPasswd=rs.getString(1);
if(dbPasswd.equals(p)){
b=true;
}
}
}
catch (Exception ex) {
ex.printStackTrace();
}finally{
this.close();
}
return b;
}
//关闭资源
public void close(){
try {
if(rs!=null){
rs.close();
rs=null;
}
if(ps!=null){
ps.close();
ps=null;
}
if(ct!=null){
ct.close();
ct=null;
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}