|
//UserBean.java
package net.z1w.DAO; import net.z1w.DAO.*; /** *UserBean **/ class UserBean{ private int ID; private String name; private String password; /** *構(gòu)造方法 **/ public UserBean(){ } public void setID(int ID){ this.ID=ID; } public int getID(){ return ID; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setPassword(String password){ this.password=password; } public String getPassword(){ return password; } public String toString(){ return "ID:"+ID+" name:"+name+" password:"+password; } }
//UserDAO.java
package net.z1w.DAO; import java.sql.*; import java.util.*; import net.z1w.DAO.*; /** *UserDAO接口,定義了User的數(shù)據(jù)庫操作 **/ public interface UserDAO{ /** *添加用戶 * @return either (1) the row count for INSERT, UPDATE, * or DELETE statements or (2) 0 for SQL statements * that return nothing **/ public int addUser() throws SQLException; /** *修改用戶 * @return either (1) the row count for INSERT, UPDATE, * or DELETE statements or (2) 0 for SQL statements * that return nothing **/ public int updateUser() throws SQLException; /** *刪除用戶 * @return either (1) the row count for INSERT, UPDATE, * or DELETE statements or (2) 0 for SQL statements * that return nothing **/ public int deleteUser() throws SQLException; /** *查詢用戶 *@return 返回查詢得到的User **/ public UserBean searchUser() throws SQLException; /** *查詢所有用戶 * @return 返回List類型,其中存放著所有用戶 **/ public List searchAllUser() throws SQLException; /** *刪除所有用戶 * @return either (1) the row count for INSERT, UPDATE, * or DELETE statements or (2) 0 for SQL statements * that return nothing **/ public int deleteAllUser() throws SQLException; }
//UserDAOOperate.java
package net.z1w.DAO; import java.sql.*; import java.util.*; import net.z1w.DAO.*; /** *UserDAO的實現(xiàn)類 **/ public class UserDAOOperate implements UserDAO{ /** *用戶Bean **/ private UserBean user; /** *構(gòu)造方法 * @param user UserBean類型,表示一個用戶 **/ public UserDAOOperate(UserBean user){ this.user=user; } /** *添加一個用戶 * @return either (1) the row count for INSERT, UPDATE, * or DELETE statements or (2) 0 for SQL statements * that return nothing **/ public int addUser() throws SQLException{ int result=0; String sql="insert into Users values(?,?,?)"; Connection conn=ConnectionPool.getConnection(); PreparedStatement stat=conn.prepareStatement(sql); stat.setInt(1,user.getID()); stat.setString(2,user.getName()); stat.setString(3,user.getPassword()); result=stat.executeUpdate(); stat.close(); conn.close(); return result; } /** *修改用戶 **/ public int updateUser() throws SQLException{ int result=0; Connection conn=ConnectionPool.getConnection(); String sql="update Users set ID=?,name=?,password=? where ID=?"; PreparedStatement stat=conn.prepareStatement(sql); stat.setInt(1,user.getID()); stat.setString(2,user.getName()); stat.setString(3,user.getPassword()); stat.setInt(4,user.getID()); result=stat.executeUpdate(); stat.close(); conn.close(); return result; } /** *刪除用戶 **/ public int deleteUser() throws SQLException{ int result=0; Connection conn=ConnectionPool.getConnection(); String sql="delete from Users where ID=?"; PreparedStatement stat=conn.prepareStatement(sql); stat.setInt(1,user.getID()); result=stat.executeUpdate(); stat.close(); conn.close(); return result; } /** *查詢用戶 * @return 返回查詢得到的User對象 **/ public UserBean searchUser() throws SQLException{ UserBean userRes=new UserBean(); Connection conn=ConnectionPool.getConnection(); String sql="select ID,name,password from users where ID=?"; PreparedStatement stat=conn.prepareStatement(sql); stat.setInt(1,user.getID()); ResultSet res=stat.executeQuery(); if(res.next()){ userRes.setID(res.getInt("ID")); userRes.setName(res.getString("name")); userRes.setPassword(res.getString("password")); }else{ userRes=null; } res.close(); stat.close(); conn.close(); return userRes; } /** *查詢所有用戶 * @return 返回List類型,其中存放著查詢到的用戶 **/ public List searchAllUser() throws SQLException{ List result=new ArrayList(); Connection conn=ConnectionPool.getConnection(); String sql="select * from users"; Statement stat=conn.createStatement(); ResultSet res=stat.executeQuery(sql); while(res.next()){ UserBean userRes=new UserBean(); userRes.setID(res.getInt("ID")); userRes.setName(res.getString("name")); userRes.setPassword(res.getString("password")); result.add(userRes); } res.close(); stat.close(); conn.close(); return result; } /** *刪除所有用戶 * @return either (1) the row count for INSERT, UPDATE, * or DELETE statements or (2) 0 for SQL statements * that return nothing **/ public int deleteAllUser() throws SQLException{ int result=0; Connection conn=ConnectionPool.getConnection(); Statement stat=conn.createStatement(); String sql="delete from users"; result=stat.executeUpdate(sql); stat.close(); conn.close(); return result; } }
//DAOFactory.java
package net.z1w.DAO; import java.sql.*; import net.z1w.DAO.*; /** *DAOFactory **/ public class DAOFactory{ private DAOFactory(){ } /** * UserDAO工廠方法 * @param user UserBean類型,代表一個用戶 * @return 返回UserDAO接口 **/ public static UserDAO getUserDAO(UserBean user){ return new UserDAOOperate(user); } } //ConnectionPool.java
package net.z1w.DAO; import java.sql.*; /** *連接池類 **/ public class ConnectionPool{ /** * 連接池中獲得一個空閑連接 * @return 返回Connection類型連接對象 **/ public static Connection getConnection() throws SQLException{ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }catch(ClassNotFoundException classE){ System.out.println("驅(qū)動程序加載失敗"); classE.printStackTrace(); } String odbcSourceName="BlogDB"; String url="jdbc:odbc:"+odbcSourceName; String user=""; String password=""; Connection conn=DriverManager.getConnection(url,user,password); return conn; } }
//TestDAO.java
package net.z1w.DAO; import java.util.*; import java.sql.*; import net.z1w.DAO.*; /** *測試DAO **/ public class TestDAO{ public static void main(String[] args){ UserBean user=new UserBean(); user.setID(1); user.setName("ZhangSan"); user.setPassword("123456"); try{ UserDAO dao=DAOFactory.getUserDAO(user); for(int i=0;i<10;i++){ dao.addUser(); System.out.println("添加了一個用戶:"+user); user.setID(i+2); user.setName("Zhang"+i*987/34); user.setPassword("#$^"+i*i*3+"sdf$%"); } dao.deleteUser(); System.out.println("刪除了一個用戶:"+user); dao.addUser(); System.out.println("添加一個用戶:"+user); user.setName("LiSi"); dao.updateUser(); System.out.println("修改了一個用戶:"+user); System.out.println("將要查詢ID=5的用戶."); user.setID(5); UserBean newUser=dao.searchUser(); System.out.println("查詢到了一個用戶:"+newUser); List list=dao.searchAllUser(); System.out.println("查詢所有用戶信息,如下:"); Iterator i=list.iterator(); while(i.hasNext()){ System.out.println(i.next()); } dao.deleteAllUser(); System.out.println("刪除所有用戶成功。Over~~~!"); }catch(SQLException sqlE){ System.out.println("SQL語句出錯"); sqlE.printStackTrace(); } } } 呵呵,昨天用了一下午寫的。DAO的全稱是數(shù)據(jù)訪問對象,通過DAO可以封裝數(shù)據(jù)庫的底層操作,使開發(fā)人員不必關(guān)心對象向表關(guān)系的轉(zhuǎn)換,專心于業(yè)務(wù)邏輯的開發(fā)。DAO屬于持久層,具體詳細的內(nèi)容我就不介紹了,大家可以去查一些相關(guān)的資料,網(wǎng)上有很多。
當(dāng)然DAO只是數(shù)據(jù)訪問模式的一種,還有DTO,微軟的ADO等等,還有Hibernate呵呵。。。DAO適合做一些小型或中型的系統(tǒng)。
|