简易的CRUD(二)简单的CRUD(二)

by admin on 2018年10月5日

平、重构简单的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 }

 

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语句

老二、使用预编译sql语句

1.预编译sql语句的便宜

  1.效率高,预编译对象将一些格式固定的SQL编译后,存放在内存池中虽数据库缓冲池,当我们重新实施同样的SQL语句时虽非需预编译的历程了,只需要DBMS运行SQL语句。所以当您要履行Statement对象往往底时,PreparedStatement对象将会见大大降低运行时,特别是的重型的数据库中,它可中之啊加快了访问数据库的速。(反复用一个sql语句时)

  2.增长了代码的可读性和维护性,将参数与SQL语句分离出来,这样便足以好对先后的更动和持续,同样,也足以减掉非必要之缪。

  3.开端源防止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 }

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 }

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     }

 

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     }

 

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图