欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
昨天寫(xiě)的UserDao - Figol‘s Blog

//UserBean.java

package net.z1w.DAO;
import net.z1w.DAO.*;
/**
 *UserBean
 **/
class UserBean{
 private int ID;
 private String name;
 private String password;
 /**
  *構造方法
  **/
 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的數據庫操作
 **/
public interface UserDAO{
 /**
  *添加用戶(hù)
   * @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;
 /**
  *修改用戶(hù)
   * @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;
 /**
  *刪除用戶(hù)
   * @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;
 /**
  *查詢(xún)用戶(hù)
  *@return 返回查詢(xún)得到的User
  **/
 public UserBean searchUser() throws SQLException;
 /**
  *查詢(xún)所有用戶(hù)
  * @return 返回List類(lèi)型,其中存放著(zhù)所有用戶(hù)
  **/
 public List searchAllUser() throws SQLException;
 /**
  *刪除所有用戶(hù)
   * @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的實(shí)現類(lèi)
 **/
public class UserDAOOperate implements UserDAO{
  /**
   *用戶(hù)Bean
   **/
  private UserBean user;
  /**
   *構造方法
   * @param user UserBean類(lèi)型,表示一個(gè)用戶(hù)
   **/
  public UserDAOOperate(UserBean user){
   this.user=user; 
  }
  /**
   *添加一個(gè)用戶(hù)
   * @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;
  }
  /**
   *修改用戶(hù)
   **/
  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;
  }
  /**
   *刪除用戶(hù)
   **/ 
  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;
  }
  /**
   *查詢(xún)用戶(hù)
   * @return 返回查詢(xún)得到的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;
  }
  /**
   *查詢(xún)所有用戶(hù)
   * @return 返回List類(lèi)型,其中存放著(zhù)查詢(xún)到的用戶(hù)
   **/
  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;
  }
  /**
  *刪除所有用戶(hù)
   * @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工廠(chǎng)方法
   * @param user UserBean類(lèi)型,代表一個(gè)用戶(hù)
   * @return 返回UserDAO接口
   **/
  public static UserDAO getUserDAO(UserBean user){
   return new UserDAOOperate(user);
  }
  
 }
 
//ConnectionPool.java

package net.z1w.DAO;
import java.sql.*;
/**
 *連接池類(lèi)
 **/
public class ConnectionPool{
 /**
  * 連接池中獲得一個(gè)空閑連接
  * @return 返回Connection類(lèi)型連接對象
  **/
 public static Connection getConnection() throws SQLException{
  try{
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
  }catch(ClassNotFoundException classE){
   System.out.println("驅動(dòng)程序加載失敗");
   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("添加了一個(gè)用戶(hù):"+user);
    user.setID(i+2);
    user.setName("Zhang"+i*987/34);
    user.setPassword("#$^"+i*i*3+"sdf$%");
   }
   
   dao.deleteUser();
   System.out.println("刪除了一個(gè)用戶(hù):"+user);
   dao.addUser();
   System.out.println("添加一個(gè)用戶(hù):"+user);
   user.setName("LiSi");
   dao.updateUser();
   System.out.println("修改了一個(gè)用戶(hù):"+user);
   System.out.println("將要查詢(xún)ID=5的用戶(hù).");
   user.setID(5);
   UserBean newUser=dao.searchUser();  
   System.out.println("查詢(xún)到了一個(gè)用戶(hù):"+newUser);
   List list=dao.searchAllUser();
   System.out.println("查詢(xún)所有用戶(hù)信息,如下:");
   Iterator i=list.iterator();
   while(i.hasNext()){
  
    System.out.println(i.next());
   }
   dao.deleteAllUser();
   System.out.println("刪除所有用戶(hù)成功。Over~~~!");
    
  }catch(SQLException sqlE){
   System.out.println("SQL語(yǔ)句出錯");
   sqlE.printStackTrace();
  }
 } 
}
呵呵,昨天用了一下午寫(xiě)的。DAO的全稱(chēng)是數據訪(fǎng)問(wèn)對象,通過(guò)DAO可以封裝數據庫的底層操作,使開(kāi)發(fā)人員不必關(guān)心對象向表關(guān)系的轉換,專(zhuān)心于業(yè)務(wù)邏輯的開(kāi)發(fā)。DAO屬于持久層,具體詳細的內容我就不介紹了,大家可以去查一些相關(guān)的資料,網(wǎng)上有很多。

當然DAO只是數據訪(fǎng)問(wèn)模式的一種,還有DTO,微軟的ADO等等,還有Hibernate呵呵。。。DAO適合做一些小型或中型的系統。

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
使用DAO工廠(chǎng)模式
Jsp Servlet JavaBean JDBC登陸實(shí)例
第三十天-加強2-多表查詢(xún)&JDBC&連接池&DBUtils&綜合案例【悟空教程】
MyBatis實(shí)踐之DAO與Mapper
Spring:JdbcTemplate使用指南
JAVA操作數據庫方式與設計模式應用
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久