九色国产,午夜在线视频,新黄色网址,九九色综合,天天做夜夜做久久做狠狠,天天躁夜夜躁狠狠躁2021a,久久不卡一区二区三区

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
使用DAO工廠模式

數(shù)據(jù)訪問接口

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,寫了一個簡單的范例。

項目文件結(jié)構(gòu)

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);
}

Java語言: UserDAO.java
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);
}

Java語言: UserDAOImpl.java
package cn.iego.wudi.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

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;
    }
}

Java語言: UserDAOFactory.java
package cn.iego.wudi.dao;

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;
    }

}

Java語言: User.java
package cn.iego.wudi.domin;

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;
    }
}

Java語言: UserDAOTest.java
package cn.iego.wudi.jdbc;

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());
    }

}

Java語言: jdbcUtils.java
package cn.iego.wudi.jdbc;

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();
                    }
                }
            }
        }
    }
}

配置文件: daoFactory.properties
userDaoClass=cn.iego.wudi.dao.UserDAOImpl
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Java分層開發(fā)BaseDao
技術(shù)文章-對DAO編寫單元測試
Jsp中分頁功能的實現(xiàn)
jsp課程(7)
JDBC數(shù)據(jù)庫通用DAO
尚硅谷JDBC學(xué)習(xí)筆記
更多類似文章 >>
生活服務(wù)
熱點新聞
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服