第三周任务书

1.本周实现的任务

管理员信息的增删改查

(1)DButil类 实现与数据库的连接和最基本的参数定义

 

···javapublic class DBUtil {
    private final static String driver="com.mysql.jdbc.Driver";
    private final String url ="jdbc://localhost:3306/test";
    private final String username = "root";
    private final String password = "123456";
    private Connection conn;
    private static int span=2;//定义每页商品的数量
    public int getSpn() {
        return span;
    }
    public static void setSpan(int i) {
        span =i;
    }
    static{
        try{
            Class.forName(driver);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }
    public Connection getConnection(){
        try{
            conn=DriverManager.getConnection(url, username, password);
                    }catch(SQLException e){
                        e.printStackTrace();
                    }
        return conn;
    }
public ResultSet executeQuery(String sql){
    ResultSet rs=null;
    try{
        Statement stmt=conn.createStatement();
        rs=stmt.executeQuery(sql);
    }catch(SQLException e){
        e.printStackTrace();
    }
    return rs;
}
public int executeUpdate(String sql){
    int result=0;
    try{
        Statement st=conn.createStatement();
        result=st.executeUpdate(sql);
        st.close();
    }catch(SQLException e){
        e.printStackTrace();
    }
    return result;
}
    public void closeAll(Connection conn, Statement stmt, ResultSet rs) {
        
        try{
            if(rs!=null){
                rs.close();
            }
            if(stmt!=null){
                stmt.close();
            }
            if(conn!=null){
                conn.close();
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        
    }
    public static int getTotalPages(String sql){
        int totalpage=1;
        DBUtil db=new DBUtil();
        Connection con=db.getConnection();
        Statement st=null;
        ResultSet rs=null;
        try{
            st=con.createStatement();
            rs=st.executeQuery(sql);
            rs.next();
            int rows=rs.getInt(1);
            totalpage=rows/span;
            if(rows%span!=0)
            {
                totalpage++;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        db.closeAll(con, st, rs);
        return totalpage;
        
    }
    public static List<String []>getPageContent(int page,String sql)
    {
        List<String []>lists=new ArrayList<String []>();
        DBUtil db=new DBUtil();
        Connection con=db.getConnection();
        Statement st=null;
        ResultSet rs=null;
        try{
            st=con.createStatement();
            rs=st.executeQuery(sql);
            ResultSetMetaData rsmt=rs.getMetaData();
            int count=rsmt.getColumnCount();
            int start=(page-1)*span;
            if(start!=0){
                rs.absolute(start);
            }int temp=0;
            while(rs.next()&&temp<span)
            {temp++;
            String[] str=new String[count];
            for(int i=0;i<str.length;i++){
                str[i]=rs.getString(i+1);
            }
            lists.add(str);
            }
            db.closeAll(con, st, rs);
        }catch(SQLException e){
            e.printStackTrace();
        }
        return lists;
    }
    public static List<String []>getInfoArr(String sql)
    {
        List<String []>vtemp=new ArrayList<String []>();
        try{
            DBUtil db=new DBUtil();
            Connection con=db.getConnection();
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery(sql);
            ResultSetMetaData rsmt=rs.getMetaData();
            int count=rsmt.getColumnCount();
            while(rs.next())
            {
                String[] str=new String[count];
                for(int i=0;i<count;i++){
                    str[i]=rs.getString(i+1);
                    
                }
                vtemp.add(str);
            }
            db.closeAll(con, st, rs);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return vtemp;
    }
    public static List<String> getInfo(String sql)
    {
        List<String> vclass=new ArrayList<String>();
        try{
            DBUtil db=new DBUtil();
            Connection con=db.getConnection();
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery(sql);
            while(rs.next())
            {
                String str=rs.getString(1);
                vclass.add(str);
                }
            db.closeAll(con, st, rs);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return vclass;        
    }
    public static boolean isLegal(String sql){
        boolean legal=false;
        DBUtil db=new DBUtil();
        Connection conn=db.getConnection();
        Statement stmt;
        ResultSet rs;
        try{
            stmt=conn.createStatement();
            rs=stmt.executeQuery(sql);
            if(rs.next()){
                legal=true;
            }
            db.closeAll(conn, stmt, rs);        
    }catch(SQLException e){
        e.printStackTrace();
    }
    return legal;
}
}

2.IAdminDAOImpl类 定义管理员所需的各种参数和实现增删改查的功能的方法

  1 ```
  2 public class IAdminDAOImpl implements IAdminDao {
  3 
  4     @Override
  5     public int addAdmin(Admin admin) {
  6         int row=0;
  7         int id=admin.getAdminID();
  8         String name=admin.getAdminName();
  9         String pwd=admin.getPassword();
 10         String level=admin.getAdminLevel();
 11         String sql="insert adminofo(AdminID,AdminName,Password,AdminLevel)value("+id+"','"+name+"','"+pwd+"','"+level+"')";
 12         DBUtil db=new DBUtil();
 13         Connection conn=db.getConnection();
 14         Statement stmt=null;
 15         try{
 16             stmt=conn.createStatement();
 17             row=stmt.executeUpdate(sql);
 18             db.closeAll(conn,stmt,null);
 19         }catch(SQLException e){
 20             e.printStackTrace();
 21         }
 22         return row;
 23     }
 24 
 25     @Override
 26     public Admin findAdmin(String name) {
 27         Admin admin=null;
 28         String sql="select * from where AdminName='"+name+"'";
 29         DBUtil db=new DBUtil();
 30         Connection conn=db.getConnection();
 31         Statement stmt=null;
 32         ResultSet rs=null;
 33         try{
 34             stmt=conn.createStatement();
 35             rs=stmt.executeQuery(sql);
 36             if(rs.next()){
 37                 int AdminID=rs.getInt(1);
 38                 String AdminName=rs.getString(2);
 39                 String Password=rs.getString(3);
 40                 String AdminLevel=rs.getString(4);
 41                 admin=new Admin(AdminID,AdminName,Password,AdminLevel);
 42             }
 43             db.closeAll(conn,stmt,rs);
 44         }catch(SQLException e){
 45             e.printStackTrace();
 46         }
 47         return admin;
 48     }
 49 
 50     @Override
 51     public int getAdminId() {
 52         int id=0;
 53         DBUtil db=new DBUtil();
 54         Connection conn=db.getConnection();
 55         Statement stmt=null;
 56         ResultSet rs=null;
 57         String sql="select Max(Aid)from admininfo";
 58         try{
 59             stmt=conn.createStatement();
 60             rs=stmt.executeQuery(sql);
 61             if(rs.next()){                id=rs.getInt(1);
 62             }
 63             db.closeAll(conn,stmt,rs);
 64         }catch(SQLException e){
 65             e.printStackTrace();
 66         }
 67         id++;
 68         return id;
 69     }
 70 public boolean isLegal(String sql){
 71     boolean legal=false;
 72     DBUtil db=new DBUtil();
 73     Connection conn=db.getConnection();
 74     Statement stmt=null;
 75     ResultSet rs=null;
 76     try{
 77         stmt=conn.createStatement();
 78         rs=stmt.executeQuery(sql);
 79         if(rs.next()){                
 80             legal=true;
 81         }
 82         db.closeAll(conn,stmt,rs);
 83     }catch(SQLException e){
 84         e.printStackTrace();
 85     }
 86     return legal;
 87 }
 88     @Override
 89     public List<Admin> getAllAdmin() {
 90     List<Admin>admins=new ArrayList<Admin>();
 91     String sql="select * from admininfo";
 92     DBUtil db=new DBUtil();
 93     Connection conn=db.getConnection();
 94     Statement stmt=null;
 95     ResultSet rs=null;
 96     try{
 97         stmt=conn.createStatement();
 98         rs=stmt.executeQuery(sql);
 99         Admin admin=null;
100         while(rs.next()){
101             int AdminID=rs.getInt(1);
102             String AdminName=rs.getString(2);
103             String Password=rs.getString(3);
104             String AdminLevel=rs.getString(4);
105             admin=new Admin(AdminID,AdminName,Password,AdminLevel);
106             admins.add(admin);
107             }
108         db.closeAll(conn,stmt,rs);
109     }catch(SQLException e){
110         e.printStackTrace();
111     }
112     
113         return admins;
114     }
115 
116     @Override
117     public int updateAdmin(String sql) {
118         DBUtil db=new DBUtil();
119         Connection conn=db.getConnection();
120         Statement stmt;
121         int row=0;
122         try{
123             stmt=conn.createStatement();
124             row=stmt.executeUpdate(sql);
125             db.closeAll(conn,stmt,null);
126         }catch(SQLException e){
127             e.printStackTrace();
128         
129     }return row;
130     }
131 ```

3.AddAdmin类 实现管理员添加

 1 public class AddAdmin {
 2 protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{
 3     String AdminName=request.getParameter("AdminName").trim();
 4     String Password=request.getParameter("Password").trim();
 5     IAdminDao dao=new IAdminDAOImpl();
 6     int id=dao.getAdminId();
 7     String level="formal";
 8     String temp="select AdminID from where AdminName='"+AdminName+"'";
 9     boolean flag=dao.isLegal(temp);
10     String msg="";
11     if(flag){
12         msg="sorry,the user has been exit";
13     }else{
14         Admin admin=new Admin(id,AdminName,Password,level);
15         int i=dao.addAdmin(admin);
16         if(i==1){
17             msg="sucess!";
18         }
19         else{
20             msg="sorry,final!!";
21         }
22     }
23     request.setAttribute("msg",msg);
24     request.getRequestDispatcher("/error.jsp").forward(request,response);
25 }
26 }
View Code

4.addadmin.jsp文件实现添加界面

<body>
<from action="./AddAdmin"method="post"name="addform">
<tr>
<td>please enter the admin name:</td>
<td><input name="AdminName"></td>
</tr>
<tr>
<td><br/>please enter the password:</td>
<td><br/><input type="password" name="Password"/></td>
</tr>
<tr>
<td align="right">
<br/><input type="submit" value="confirm"onclick="check()"/>

界面展示

5.本周小结

这周基本写好了管理员操作的各种方法,就差最后一步的具体实现像管理员登陆、密码修改类和登陆jsp还没写,下周补充完整,同时下周争取把活动类和活动界面做出来

posted @ 2017-03-19 20:17  gong.xiaoting  Views(204)  Comments(2)    收藏  举报