简单的CRUD(二)

一、重构简单的CRUD

1.JDBC工具类

1.因为在crud中都包含一些相同的代码所以可以提取出来,抽取代码重构为工具类。

2.将工具类设置为static静态类,方便调用,不需要new对象。

 1 public class JDBCUtil {
 2     private static String driver = "com.mysql.jdbc.Driver";
 3     private static String url = "jdbc:mysql://localhost:3306/station";
 4     private static String username = "root";
 5     private static String password = "admin";
 6     public static Connection getConnection(){
 7         /**
 8          * 1.对加载和创建连接的重构
 9          * 2.把参数提取出来
10          * 3.返回connection
11          */
12         Connection connection=null;
13         try {
14             Class.forName(driver);
15             connection = DriverManager.getConnection(url, username, password);
16         } catch (Exception e) {
17             e.printStackTrace();
18         }
19         return connection;
20     }
21 
22     /**
23      * 对关闭资源的异常代码的重构
24      * @param connection
25      * @param preparedStatement
26      */
27     public static void close(PreparedStatement preparedStatement,Connection connection){
28         try {
29             if (preparedStatement!=null){
30                 preparedStatement.close();
31             }
32         } catch (SQLException e) {
33             e.printStackTrace();
34         }finally {
35             try{
36                 if (connection!=null){
37                     connection.close();
38                 }
39             }catch (SQLException e){
40                 e.printStackTrace();
41             }
42         }
43     }
44 
45     /**
46      * 重构close
47      * @param preparedStatement
48      * @param connection
49      */
50     public static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection){
51         try {
52             if (resultSet!=null){
53                 resultSet.close();
54             }
55         } catch (SQLException e) {
56             e.printStackTrace();
57         }finally {
58             close(preparedStatement, connection);
59         }
60     }
61 }

二、使用预编译sql语句

1.预编译sql语句的好处

  1.效率高,预编译对象把一些格式固定的SQL编译后,存放在内存池中即数据库缓冲池,当我们再次执行相同的SQL语句时就不需要预编译的过程了,只需DBMS运行SQL语句。所以当你需要执行Statement对象多次的时候,PreparedStatement对象将会大大降低运行时间,特别是的大型的数据库中,它可以有效的也加快了访问数据库的速度。(反复使用一个sql语句时)

  2.提高了代码的可读性和维护性,将参数与SQL语句分离出来,这样就可以方便对程序的更改和延续,同样,也可以减少不必要的错误。

  3.开源防止SQL注入。

2.用户实体类

 1 public class LoginUser {
 2     // 创建注册用户属性
 3     private Integer id;
 4     private String username;
 5     private String password;
 6 
 7     public Integer getId() {
 8         return id;
 9     }
10 
11     public void setId(Integer id) {
12         this.id = id;
13     }
14 
15     public String getUsername() {
16         return username;
17     }
18 
19     public void setUsername(String username) {
20         this.username = username;
21     }
22 
23     public String getPassword() {
24         return password;
25     }
26 
27     public void setPassword(String password) {
28         this.password = password;
29     }
30 }

3、用户dao接口

 1 public interface ILoginUserDao {
 2     /**
 3      * 保存用户
 4      * @param loginUser
 5      */
 6     void save(LoginUser loginUser);
 7 
 8     /**
 9      * 通过用户名id查询用户信息
10      * @param id
11      * @return
12      */
13     LoginUser getLoginUserById(Integer id);
14 }

4、用户dao实现

 1 public class LoginUserDao implements ILoginUserDao {
 2     @Override
 3     public void save(LoginUser loginUser) {
 4         /**
 5          * 0.导入驱动包
 6          * 1.加载 2.连接 提取到了JDBCUtil工具类
 7          * 3.创建预编译语句
 8          * 4.执行sql语句
 9          * 5.释放资源 提取到了JDBCUtil工具类
10          */
11         Connection connection = null;
12         PreparedStatement preparedStatement = null;
13         try {
14             //调用工具类中的getConnection,返回连接
15             connection = JDBCUtil.getConnection();
16             String sql = "INSERT INTO loginuser (username, password) VALUES (?, ?)";
17             preparedStatement = connection.prepareStatement(sql);
18             preparedStatement.setString(1, loginUser.getUsername());
19             preparedStatement.setString(2, loginUser.getPassword());
20             //执行sql语句
21             preparedStatement.executeUpdate();
22         } catch (Exception e) {
23             e.printStackTrace();
24         }finally {
25             //释放资源
26             JDBCUtil.close(preparedStatement, connection);
27         }
28     }
29 
30   @Override
31     public LoginUser getLoginUserById(Integer id) {
32         LoginUser loginUser = new LoginUser();
33         Connection connection = null;
34         PreparedStatement preparedStatement = null;
35         ResultSet resultSet = null;
36         try {
37             //调用工具类中的getConnection,返回连接
38             connection = JDBCUtil.getConnection();
39             String sql = "SELECT id, username, password FROM loginuser where id = ?";
40             preparedStatement = connection.prepareStatement(sql);
41             preparedStatement.setInt(1, id);
42             //执行sql语句
43             resultSet = preparedStatement.executeQuery();
44             while (resultSet.next()){
45                 int id1 = resultSet.getInt("id");
46                 String userName = resultSet.getString("username");
47                 String password = resultSet.getString("password");
48                 //封装对象
49                 loginUser.setId(id1);
50                 loginUser.setUsername(userName);
51                 loginUser.setPassword(password);
52             }
53         } catch (Exception e) {
54             e.printStackTrace();
55         }finally {
56             //释放资源
57             JDBCUtil.close(resultSet, preparedStatement, connection);
58         }
59         return loginUser;
60     }
原文地址:https://www.cnblogs.com/zt19994/p/8284684.html