爲了更明瞭的安全的操作數據庫數據,我們將用戶通過java操作數據庫管理系統更改數據庫表數據的整個過程歸納爲三層
-1.表示層:與用戶交互,向業務層發送請求得到結果
-2.業務層:用來實現業務邏輯,比如登錄註冊(處理複雜的邏輯)
-3.數據訪問層DAO層:實現對數據庫的各種操作(增刪改查)
我們在這裏主要講解dao層的分層開發(以oracle的emp表舉例)
empmgr包:dao類(BaseDao,EmployeeDao,EmployeeDaoImpl):實現具體的操作數據庫管理系統
entity包:存放實體類,根據數據庫emp表編寫對應實體類Employee
項目下新建一個文件:conn.properties,以鍵值對的形式存放數據庫配置文件(driver=oracle.jdbc.driver.OracleDriver;url=jdbc:oracle:thin:@localhost:1521:orcl;user=scott;pwd=tiger)
下面直接上代碼:
1.entity包下的實體類: Employee.java(省略)
2.dao包下的接口:EmployeeDao.java(根據業務需求編寫,一般是增刪改查)
pubic interface EmployeeDao{
//查詢所有員工信息
public List<Employee> selectAll();
//根據id查員工
public Employ selectById(int id);
//刪除指定員工
public boolean deleteById(int id);
//增加新員工
public boolean insertEmp(Employ emp);
//更改員工薪水 --因爲這個功能跟以上類似所以省略
public boolean updateEmp(int id,double sal);
}
3.dao子包 impl下的基類BaseDao.java(從EmployeeDaoImpl.java抽取的重複代碼)
public class BaseDao{
//從conn.properties讀取配置文件中,在靜態代碼塊中爲了只讀取一次
private static String fileName = "conn.properties";
private static String url;
private static String user;
private static String driver;
private static String pwd;
static{
InputStream is = Thread.currentThread.getclass().getResourceAsStream(fileName
);
//生命一個properties對象
Properties pro = new Properties();
//把輸入流is中的數據加載到pro中
pro.load(is);
//通過鍵讀取文件中值得內容
url = pro.getProperties(url);
user = pro.getpropertier(user);
pwd = pro.getProperties(pwd);
driver = pro.getProperties(driver);
}
//獲取連接
public Connection getConn(){
Class.forName(driver);
return DriverManager.getConnection(url,user,pwd);
}
//關閉資源
public void closeDBResourse(ResultSet rs,Connection conn , PreparedStatement pstmt){
if(rs!=null)rs.close();
if(conn!=null)conn.close();
if(pstmt!=null)pstmt.close();
}
//使用executeUpadate是抽取的共同部分
public void executeU(PrepaString sql,Object...params){
Connection conn = null;
PreparedStatement pstmt = null;
conn = getConn();
pstmt = conn.prepareStatement(sql);
for(int i=1;i<=params.length;i++){
pstmt.setObject(i,params[i-1]);
pstmt.executeUpdate();
}
closeDBResourse(null,pstmt,conn);
}
}
3.dao包下impl 實現類EmployeeDaoImpl.java,業務層操作此類完成對數據庫的訪問
public class EmployeeDaoImpl extends BaseDao implements EmployeeDao{
//查詢所有員工信息
public List<Employee> selectAll(){
Connection conn = getConn();
String sql = "select * from emp";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.execute();
List<Employee> empList = new ArrayList<Employee>();
while(rs.next()){
//根據構造方法獲取對應的值
Employee emp = new Employee(rs.getInt("empno")...);
empList.add(emp);
}
closeDBResourse(rs,pstmt,conn);
return empList;
}
//按照id查詢員工
public Employee selectById(int id){
Connection conn = genConn();
String sql = "select * from emp where empno = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,id);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
Employee emp = new Employee(rs.getInt(empno)...);
}
closeDBResourse(rs,pstmt,conn);
return emp;
}
//刪除指定員工信息
public boolean deleteById(int id){
String sql = "delete from emp where empno = ?";
executeU(sql,id);//這裏可以準備一個返回值判斷執行成功失敗
//return true;
}
//添加員工
public boolean insertEmp(Employee emp){
String sql = "insert into emp values(?,?,?,?,?,?)";
executeU(sql,emp.empno,emp.ename.......);
//return true;
}
}
以上代碼就是對jdbc 數據訪問層進行了簡單的封裝,從讀取preporties文件參數,到實現接口繼承抽象類,再到實現類的完成訪問功能,是一個典型的使用面向對象編程的步驟,包括了封裝繼承多態,對重複代碼的提取封裝,代碼分工明確,思路明瞭,在以後的學習中我們會慢慢體會到分層開發的代碼之美.