首页 > 其他 > 详细

dao代码模板

时间:2016-10-08 01:37:49      阅读:171      评论:0      收藏:0      [点我收藏+]

提供数据源以及回收资源的工具类DbUtils:

public class DbUtils {
    private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
    static {
        try {
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");// 指定连接的数据库的地址
            dataSource.setUser("root");
            dataSource.setPassword("123");
            dataSource.setAcquireIncrement(5);
            dataSource.setInitialPoolSize(20);
            dataSource.setMinPoolSize(2);
            dataSource.setMaxPoolSize(50);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 单例模式
    public static DataSource getDataSource() {
        return dataSource;
    }

    public static void close(Connection con, PreparedStatement ps, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
                rs = null;
            }
            if (ps != null) {
                ps.close();
                ps = null;
            }
            if (con != null) {
                con.close();
                con = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

提供数据库连接、处理事务的工具类TransactionUtils:

public class TransactionUtils {
    private static DataSource dataSource = DbUtils.getDataSource();
    private static Connection con = null;

    public static Connection getConnection() {
        try {
//            System.out.println(dataSource);
            con = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    public static void beginTx() {
        try {
            con.setAutoCommit(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void rollback() {
        try {
            con.rollback();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void commit() {
        try {
            con.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

一个测试dao类:

public class STDao {
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    public void update() {
        con = TransactionUtils.getConnection();
        System.out.println(con);
        TransactionUtils.beginTx();
        String sql = "delete from student";
        try {
            ps = con.prepareStatement(sql);
            ps.executeUpdate();
            sql = "insert into student (name,age) values (?,?)";
            ps = con.prepareStatement(sql);
            ps.setString(1, "xiaosi");
            ps.setObject(2, "yy");
            ps.executeUpdate();
            TransactionUtils.commit();
        } catch (Exception e) {
            e.printStackTrace();
            TransactionUtils.rollback();
        } finally {
            DbUtils.close(con, ps, rs);
        }
    }

    public static void main(String[] args) {
        STDao dao = new STDao();
        dao.update();
    }
}

以上代码是比较好的,数据源单例、在catch块中写回滚,在finally块中回收资源。

需要了解的是,如果我们的sql语句错误,那么在ps.executeUpdate();后就会抛出java.sql.SQLException异常,直接进入到catch块中,根本不会执行下面的提交事务代码,不提交事务的情况下执行回滚也不会报错,此时数据库数据不会改变。如果sql语句正确,提交事务后因为网路问题造成操作失败,则此时也会进入到catch块中,执行回滚,数据库数据也不会发生改变。所以说,数据层代码这样写最合适。

dao代码模板

原文:http://www.cnblogs.com/koushr/p/5873465.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!