DAO是Data Access Object數(shù)據(jù)訪問接口,數(shù)據(jù)訪問:顧名思義就是與數(shù)據(jù)庫打交道。夾在業(yè)務(wù)邏輯與數(shù)據(jù)庫資源中間。
在核心J2EE模式中是這樣介紹DAO模式的:為了建立一個健壯的J2EE應(yīng)用,應(yīng)該將所有對數(shù)據(jù)源的訪問操作抽象封裝在一個公共API中。用程序設(shè)計的語言來說,就是建立一個接口,接口中定義了此應(yīng)用程序中將會用到的所有事務(wù)方法。在這個應(yīng)用程序中,當(dāng)需要和數(shù)據(jù)源進行交互的時候則使用這個接口,并且編寫一個單獨的類來實現(xiàn)這個接口在邏輯上對應(yīng)這個特定的數(shù)據(jù)存儲。
為了實現(xiàn)DAO,寫了一個簡單的范例。
UserDAO.java 是DAO方法實現(xiàn)的接口
UserDAOFactory.java 是DAO工廠,創(chuàng)建UserDAO的實例
UserDAOImpl.java 是UserDAO接口的實現(xiàn)類,對UserDAO里的增刪改查操作進行具體的實現(xiàn)
User.java 是一個JavaBean
UserDAOTest.java 進行DAO操作,里面包含main方法
daoFactory.properties 為UserDAOFactory使用的配置文件
package cn.iego.wudi.dao;
import cn.iego.wudi.domin.User;
public interface UserDAO {
public void addUser(User user);
public User getUser(int userId);
public User findUser(String loginName, String password);
public void update(User user);
public void delete(User user);
}
import cn.iego.wudi.domin.User;
public interface UserDAO {
public void addUser(User user);
public User getUser(int userId);
public User findUser(String loginName, String password);
public void update(User user);
public void delete(User user);
}
import cn.iego.wudi.domin.User;
import cn.iego.wudi.jdbc.jdbcUtils;
public class UserDAOImpl implements UserDAO {
@Override
public void addUser(User user) {
ResultSet rs = null;
Connection conn;
try {
conn = jdbcUtils.getConnection();
String sql = "insert into user (name,birthday,money) values (?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, user.getName());
ps.setDate(2, new Date(user.getBirthday().getTime()));
ps.setFloat(3, user.getMoney());
ps.executeUpdate();
jdbcUtils.free(rs, ps, conn);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void delete(User user) {
ResultSet rs = null;
try {
Connection conn = jdbcUtils.getConnection();
Statement st = conn.createStatement();
st.executeUpdate("delete from user where id=" + user.getId());
jdbcUtils.free(rs, st, conn);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public User findUser(String loginName, String password) {
User user = null;
ResultSet rs = null;
PreparedStatement ps = null;
Connection conn = null;
try {
conn = jdbcUtils.getConnection();
String sql = "select id,name,birthday,money from user where name = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, loginName);
rs = ps.executeQuery();
user = mappingUser(rs);
} catch (Exception e) {
e.printStackTrace();
} finally {
jdbcUtils.free(rs, ps, conn);
}
return user;
}
@Override
public User getUser(int userId) {
User user = null;
ResultSet rs = null;
PreparedStatement ps = null;
Connection conn = null;
try {
conn = jdbcUtils.getConnection();
String sql = "select id,name,birthday,money from user where id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, userId);
rs = ps.executeQuery();
user = mappingUser(rs);
} catch (Exception e) {
e.printStackTrace();
} finally {
jdbcUtils.free(rs, ps, conn);
}
return user;
}
@Override
public void update(User user) {
ResultSet rs = null;
Connection conn = null;
PreparedStatement ps = null;
try {
conn = jdbcUtils.getConnection();
String sql = "update user set name=?,birthday=?,money=? where id = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getName());
ps.setDate(2, new Date(user.getBirthday().getTime()));
ps.setFloat(3, user.getMoney());
ps.setInt(4, user.getId());
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
jdbcUtils.free(rs, ps, conn);
}
public User mappingUser(ResultSet rs) {
User user = null;
try {
while (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setBirthday(rs.getDate("birthday"));
user.setMoney(rs.getFloat("money"));
}
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
}
import java.io.InputStream;
import java.util.Properties;
public class UserDAOFactory {
static UserDAO userDao = null;
static UserDAOFactory instance = new UserDAOFactory();
private UserDAOFactory() {
Properties properties = new Properties();
InputStream inStream = null;
try {
inStream = UserDAOFactory.class.getClassLoader().getResourceAsStream("daoFactory.properties");
properties.load(inStream);
System.out.println(properties.getProperty("userDaoClass"));
Class<?> clazz = Class.forName(properties.getProperty("userDaoClass"));
userDao = (UserDAO) clazz.newInstance();
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public static UserDAOFactory getInstance() {
return instance;
}
public UserDAO getUserDAO() {
return userDao;
}
}
import java.util.Date;
public class User {
public User(){
super();
}
public User(String name, Date birthday, float money) {
this.name = name;
this.birthday = birthday;
this.money = money;
}
private int id;
private String name;
private Date birthday;
private float money;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public float getMoney() {
return money;
}
public void setMoney(float money) {
this.money = money;
}
}
import cn.iego.wudi.dao.UserDAO;
import cn.iego.wudi.dao.UserDAOFactory;
import cn.iego.wudi.domin.User;
public class UserDAOTest {
/**
* @param args
*/
public static void main(String[] args) {
UserDAO userDao = UserDAOFactory.getInstance().getUserDAO();
User user = null;
//user = new User("dao name",new Date(),1000.0f);
//userDao.addUser(user);
//user = userDao.findUser("dao name", “”);
user = userDao.getUser(4);
//userDao.delete(user);
//user.setMoney(25000.35f);
//userDao.update(user);
//user = userDao.getUser(4);
System.out.println(user.getId()+"\t"+user.getName()+"\t"+user.getBirthday()+"\t"+user.getMoney());
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public final class jdbcUtils {
private jdbcUtils() {
}
private static String url = "jdbc:mysql://localhost:3306/jdbc";
private static String user = "root";
private static String password = "";
private static Connection conn = null;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection() throws Exception {
try {
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
return conn;
}
public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (st != null) {
st.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
聯(lián)系客服