建議面向接口開發:先接口 -> 再實現類
爲 service、dao 層設置接口
interface 命名: IStudentService、IStudentDao
接口所在的包命名:xxx.service、xx.dao
implements 命名: StudentServiceImpl、StudentDaoImpl
實現類所在的包:xxx.service.impl、xx.dao.impl
接口 x = new 實現類();
例:IStudentDao studentDao = new StudentDaoImpl();
-
DBUtil:通用的數據庫幫助類,可以降低 Dao 層的代碼冗餘
- DBUtil 有很高的可移植性
Dao 層的增刪改中的 SQL 語句
關於查詢,在 DaoImp 中關閉 DBUtil 的 Connection,需要將 DBUtil 中的 Connection 寫爲全局變量
方法重構:將多個方法的共同代碼提煉出來,單獨寫在一個方法中,然後引入該方法即可
代碼結構:
DBUtil 代碼:
// 通用的數據庫操作方法
// 通用的增刪改查
// Object 數組的強大
package com.zjy.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.zjy.entity.Student;
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/threetiresample";
private static final String USER = "root";
private static final String PASSWORD = "root";
public static Connection connection = null;
public static PreparedStatement pst = null;
public static ResultSet rs = null;
// 獲取數據庫連接
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("org.gjt.mm.mysql.Driver"); // 加載數據庫驅動
return connection = DriverManager.getConnection(URL, USER, PASSWORD); // 獲取連接對象
}
// 構造語句
public static PreparedStatement creatPreparedStatement(String sql, Object[] params) throws ClassNotFoundException, SQLException {
pst = getConnection().prepareStatement(sql); // 使用prepareStatement接口
if(params != null) {
for(int i = 0; i < params.length; i++) {
pst.setObject(i+1, params[i]);
}
}
return pst;
}
// 關閉所有pst和conn連接
public static void closeAll(ResultSet rs, Statement st, Connection connecion) {
try {
if(pst != null) pst.close();
if(connection != null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
// 通用的增刪改
public static boolean executeUpdate(String sql, Object[] params) {
try {
// pst = 連接 + 構造SQL語句
pst = creatPreparedStatement(sql, params);
int result = pst.executeUpdate();
if(result > 0) {
return true;
} else {
return false;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
return false;
} catch (SQLException e) {
e.printStackTrace();
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
closeAll(null, pst, connection);
}
}
// 通用的查:返回值是一個集合 (Student, List<Student>, null)
// 使用集合可以表示任何類型
// 通用:表示適合於任何查詢
// 查詢的結果集傳遞結束後,不能進行關閉
public static ResultSet executeQuery(String sql, Object[] params) {
List<Student> students = new ArrayList<>();
Student studentbean = null;
try {
pst = creatPreparedStatement(sql, params);
rs = pst.executeQuery();
return rs;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
} catch (SQLException e) {
e.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
完整代碼我將會上傳到我的資源,可以免費下載。進入我的個人主頁,就可看到我上傳的資源。 ThreeTreeSample.zip 優化版。
說在最後的話:編寫實屬不易,若喜歡或者對你有幫助記得點贊+關注或者收藏哦~