打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
昨天写的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{
 /**
  *添加用户
   * @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的实现类
 **/
public class UserDAOOperate implements UserDAO{
  /**
   *用户Bean
   **/
  private UserBean user;
  /**
   *构造方法
   * @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("驱动程序加载失败");
   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的全称是数据访问对象,通过DAO可以封装数据库的底层操作,使开发人员不必关心对象向表关系的转换,专心于业务逻辑的开发。DAO属于持久层,具体详细的内容我就不介绍了,大家可以去查一些相关的资料,网上有很多。

当然DAO只是数据访问模式的一种,还有DTO,微软的ADO等等,还有Hibernate呵呵。。。DAO适合做一些小型或中型的系统。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
使用DAO工厂模式
Jsp Servlet JavaBean JDBC登陆实例
第三十天-加强2-多表查询&JDBC&连接池&DBUtils&综合案例【悟空教程】
MyBatis实践之DAO与Mapper
Spring:JdbcTemplate使用指南
JAVA操作数据库方式与设计模式应用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服