• 总结学了 j2ee 一月半后的项目情况(3)


    源码:

    1、用户 CRUD servlet 

     1 package fafa.shop.controller;
     2 import java.io.IOException;
     3 import java.util.ArrayList;
     4 import javax.servlet.ServletException;
     5 import javax.servlet.http.HttpServlet;
     6 import javax.servlet.http.HttpServletRequest;
     7 import javax.servlet.http.HttpServletResponse;
     8 import fafa.shop.service.User;
     9 import fafa.shop.service.UserModel;
    10 @SuppressWarnings("serial")
    11 public class UserServ extends HttpServlet {
    12     public void doGet(HttpServletRequest request, HttpServletResponse response)
    13             throws ServletException, IOException {
    14         request.setCharacterEncoding("GBK") ;
    15             UserModel us = new UserModel() ;        
    16         String type= request.getParameter("type");
    17         if (type.equals("add")){
    18             String uid = null ; 
    19             String username = request.getParameter("username") ;
    20             String password = request.getParameter("password") ;
    21             String email = request.getParameter("email") ;
    22             User user = new User(uid, username, password, email) ;
    23             if (us.insertUser(user)){
    24                 ArrayList<User> all = us.getAll() ;
    25                 request.setAttribute("user", all) ;
    26                 request.getRequestDispatcher("/index.jsp").forward(request, response) ;
    27             }else{
    28                 request.setAttribute("error", "用户添加失败") ;
    29                 request.getRequestDispatcher("/index.jsp").forward(request, response) ;
    30             }
    31         }else if (type.equals("mod")){
    32             String uid = request.getParameter("uid") ;
    33             ArrayList<User> all = us.getOne(uid) ;
    34             if (all != null){
    35                 request.setAttribute("user", all) ;
    36                 request.getRequestDispatcher("/WEB-INF/update.jsp").forward(request, response) ;
    37             }else {
    38                 request.setAttribute("error", "用户不存在!!!") ;
    39                 request.getRequestDispatcher("/WEB-INF/update.jsp").forward(request, response) ;
    40             }
    41 
    42         }else if (type.equals("update")){
    43             String uid = request.getParameter("uid") ;
    44             String username = request.getParameter("username") ;
    45             String password = request.getParameter("password") ;
    46             String email = request.getParameter("email") ;
    47             User user = new User(uid, username, password, email) ;
    48             if (us.updateUser(user)){
    49                 request.setAttribute("updateSuccess", "数据更新成功!") ;
    50                 request.getRequestDispatcher("/index.jsp").forward(request, response) ;
    51             }else{
    52                 request.setAttribute("updateError", "数据更新失败!") ;
    53                 request.getRequestDispatcher("/index.jsp").forward(request, response) ;
    54             }
    55         }else if (type.equals("del")){
    56             String uid = request.getParameter("uid") ;
    57             
    58             if (us.delUser(uid)){
    59                 request.setAttribute("delSuccess", "数据删除成功!") ;
    60                 request.getRequestDispatcher("/index.jsp").forward(request, response) ;
    61             }else{
    62                 request.setAttribute("delError", "数据删除失败!") ;
    63                 request.getRequestDispatcher("/index.jsp").forward(request, response) ;
    64             }
    65         }
    66         
    67     }
    68     public void doPost(HttpServletRequest request, HttpServletResponse response)
    69             throws ServletException, IOException {
    70         this.doGet(request, response) ;
    71     }
    72 
    73 }

    2、user bean 类 

     1 package fafa.shop.service;
     2 
     3 public class User {
     4     private String uid ;
     5     private String username ;
     6     private String password ;
     7     private String email ;
     8     
     9     public User() {
    10         super();
    11     }
    12     public User(String uid ,String username, String password, String email) {
    13         super();
    14         this.uid = uid ;
    15         this.username = username;
    16         this.password = password;
    17         this.email = email;
    18     }
    19     public String getUid() {
    20         return uid;
    21     }
    22     public void setUid(String uid) {
    23         this.uid = uid;
    24     }
    25     public String getUsername() {
    26         return username;
    27     }
    28     public void setUsername(String username) {
    29         this.username = username;
    30     }
    31     public String getPassword() {
    32         return password;
    33     }
    34     public void setPassword(String password) {
    35         this.password = password;
    36     }
    37     public String getEmail() {
    38         return email;
    39     }
    40     public void setEmail(String email) {
    41         this.email = email;
    42     }
    43 }

    3、userModel 类 和 数据库连接类 

      1 package fafa.shop.service;
      2 
      3 import java.sql.*;
      4 import java.util.ArrayList;
      5 
      6 import fafa.shop.util.DBUtil;
      7 
      8 public class UserModel {
      9     private Connection conn = null ;
     10     private PreparedStatement ps = null ;
     11     private ResultSet rs = null ;
     12     
     13     //插入用户
     14     public boolean insertUser(User user){
     15         boolean flag = false ;
     16         try {
     17             this.conn = DBUtil.getConnection() ;
     18             String sql = "INSERT INTO user(uid,username,password,email) VALUES(?,?,?,?)" ;
     19             this.ps = this.conn.prepareStatement(sql) ;
     20             this.ps.setString(1, null) ;
     21             this.ps.setString(2, user.getUsername()) ;
     22             this.ps.setString(3, user.getPassword()) ;
     23             this.ps.setString(4, user.getEmail()) ;
     24             this.ps.executeUpdate() ;
     25             flag = true ;
     26         } catch (Exception e) {
     27             e.printStackTrace();
     28         }finally{
     29             try {
     30                 this.conn.close() ;
     31             } catch (SQLException e) {
     32                 e.printStackTrace();
     33             }
     34             try {
     35                 this.ps.close();
     36             } catch (SQLException e) {
     37                 e.printStackTrace();
     38             } 
     39         }
     40         
     41         return flag ;
     42     }
     43     
     44     //取得所有用户
     45     public ArrayList<User> getAll(){
     46         ArrayList<User> all = new ArrayList<User>() ;
     47         try {
     48             this.conn = DBUtil.getConnection() ;
     49             String sql = "SELECT uid, username, password, email FROM user" ;
     50             this.ps = this.conn.prepareStatement(sql) ;
     51             this.rs = this.ps.executeQuery() ;
     52             while(this.rs.next()){
     53                 User user = new User() ;
     54                 user.setUid(rs.getString("uid")) ;
     55                 user.setUsername(rs.getString("username")) ;
     56                 user.setPassword(rs.getString("password")) ;
     57                 user.setEmail(rs.getString("email")) ;
     58                 all.add(user) ;
     59             }
     60             
     61         } catch (Exception e) {
     62             e.printStackTrace();
     63         }finally{
     64             try {
     65                 this.conn.close() ;
     66                 this.ps.close() ;
     67                 this.rs.close() ;
     68             } catch (SQLException e) {
     69                 e.printStackTrace();
     70             }
     71         }
     72 
     73         return all ;
     74     }
     75     
     76     //取一条数据
     77     public ArrayList<User> getOne(String uid){
     78         ArrayList<User> all = new ArrayList<User>() ;
     79         try {
     80             this.conn = DBUtil.getConnection() ;
     81             String sql = "SELECT uid, username, password, email FROM user WHERE uid=?" ;
     82             this.ps = this.conn.prepareStatement(sql) ;
     83             this.ps.setString(1, uid) ;
     84             this.rs = this.ps.executeQuery() ;
     85             while(this.rs.next()){
     86                 User user = new User() ;
     87                 user.setUid(rs.getString("uid")) ;
     88                 user.setUsername(rs.getString("username")) ;
     89                 user.setPassword(rs.getString("password")) ;
     90                 user.setEmail(rs.getString("email")) ;
     91                 all.add(user) ;
     92             }
     93             
     94         } catch (Exception e) {
     95             e.printStackTrace();
     96         }finally{
     97             try {
     98                 this.conn.close() ;
     99                 this.ps.close() ;
    100                 this.rs.close() ;
    101             } catch (SQLException e) {
    102                 e.printStackTrace();
    103             }
    104         }
    105         return all ;
    106     }
    107     
    108     //更新数据
    109     public boolean updateUser(User user){
    110         boolean flag = false ;
    111         try {
    112             this.conn = DBUtil.getConnection() ;
    113             String sql = "UPDATE user SET username=?, password=?, email=? WHERE uid=?" ;
    114             this.ps = this.conn.prepareStatement(sql) ;
    115             this.ps.setString(1, user.getUsername()) ;
    116             this.ps.setString(2, user.getPassword()) ;
    117             this.ps.setString(3, user.getEmail()) ;
    118             this.ps.setString(4, user.getUid()) ;
    119             this.ps.executeUpdate() ;
    120             flag = true ;
    121         } catch (Exception e) {
    122             e.printStackTrace();
    123         }finally{
    124             try {
    125                 this.conn.close() ;
    126             } catch (SQLException e) {
    127                 e.printStackTrace();
    128             }
    129             try {
    130                 this.ps.close();
    131             } catch (SQLException e) {
    132                 e.printStackTrace();
    133             } 
    134         }
    135         return flag ;
    136     }
    137     
    138     //删除用户
    139     public boolean delUser(String uid){
    140         boolean flag = false ;
    141         try {
    142             this.conn = DBUtil.getConnection() ;
    143             String sql = "DELETE FROM user WHERE uid=?" ;
    144             this.ps = this.conn.prepareStatement(sql) ;
    145             this.ps.setInt(1, Integer.parseInt(uid)) ;
    146             this.ps.executeUpdate() ;
    147             flag = true ;
    148         } catch (Exception e) {
    149             e.printStackTrace();
    150         }finally{
    151             try {
    152                 this.conn.close() ;
    153             } catch (SQLException e) {
    154                 e.printStackTrace();
    155             }
    156             try {
    157                 this.ps.close();
    158             } catch (SQLException e) {
    159                 e.printStackTrace();
    160             } 
    161         }
    162         return flag ;
    163     }
    164 }
     1 package fafa.shop.util;
     2 
     3 import java.sql.*;
     4 
     5 public class DBUtil {
     6     private static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
     7     private static final String DBURL = "jdbc:mysql://localhost:3306/test" ;
     8     private static final String DBUSER = "root" ;
     9     private static final String DBPASS = "123456" ;
    10     
    11     private static Connection conn = null ;
    12     
    13     //外部取得连接
    14     public static Connection getConnection() throws Exception {
    15         Class.forName(DBDRIVER) ;
    16         conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS) ;
    17         return conn ;
    18     }
    19     
    20     //关闭连接
    21     public static void close(ResultSet rs, Statement ps, Connection ct){
    22         try {
    23             if (rs!=null){
    24                 rs.close() ;
    25             }
    26         } catch (SQLException e) {
    27             e.printStackTrace();
    28         }
    29         try {
    30             if (ps!=null){
    31                 ps.close() ;
    32             }
    33         } catch (SQLException e) {
    34             e.printStackTrace();
    35         }
    36         try {
    37             if (ct!=null){
    38                 ct.close() ;
    39             }
    40         } catch (SQLException e) {
    41             e.printStackTrace();
    42         }
    43     }
    44 }

     这样就完成成了数据的CRUD ,我现在是初学者啊,没办法只能这样练练手,希望能更快上手.

    本来是使用三种数据库 oracle、mssql 、mysql 完成的。

    以上还少了功能:分页、上传文件、支付、 等等。

    当然现在很多企业也不再会这样写代码了,基本都是用框架了。所以下面的学习中心转移到框架  和 基础加深了。

  • 相关阅读:
    LintCode 82. 落单的数
    LintCode 373. 奇偶分割数组
    LintCode 2. 尾部的零
    LintCode 413. 反转整数
    LintCode 13. Implement strStr()
    串匹配
    【剑指offer】面试题 57. 和为 S 的数字
    二分查找
    整除个数
    使用Eclipse创建Web Services
  • 原文地址:https://www.cnblogs.com/dafa/p/2771018.html
Copyright © 2020-2023  润新知