一、重構簡單的CRUD 1.JDBC工具類 1.因為在crud中都包含一些相同的代碼所以可以提取出來,抽取代碼重構為工具類。 2.將工具類設置為static靜態類,方便調用,不需要new對象。 二、使用預編譯sql語句 1.預編譯sql語句的好處 1.效率高,預編譯對象把一些格式固定的SQL編譯後, ...
一、重構簡單的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 }