自学内容网 自学内容网

JDBC封装day02

JDBC封装与设计模式

DAO模式的应用

Data Access Object(数据存取对象) 持久层
创建包
com.dao ----放接口文件 UserDao
BaseDao(数据库工具类)
com.dao.impl----放接口实现类 UserDaoImpl
com.entity ----放实体类 User

封装JDBC

/**
 * 数据库工具类
 */
public class BaseDao {

    Connection conn = null;
    PreparedStatement ps = null;
    //获取Conn对象 打开数据库链接
    public boolean getConn() {
        boolean bool = false;//默认 false 未打开数据库
        try {
            //加载驱动  方言
            Class.forName("com.mysql.jdbc.Driver");
            //准备数据库连接路径
            String url = "jdbc:mysql://127.0.0.1:3306/xxshop?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull";
            //用户名与密码
            String username = "root";
            String userpwd = "root";
            //根据路径,用户名,密码 使用DriverManager获取数据库connection连接
            conn = DriverManager.getConnection(
                    url,username,userpwd);
            bool = true;//已经打开
        } catch (Exception e) {
            e.printStackTrace();
            bool = false ;//已经打开
        }
            return  bool;
    }

    /**
     * 添加,修改,删除数据
     * @param sql
     * @param objs
     * @return
     */
    public int executeUpdate(String sql,Object objs[])
    {
        int res = 0;//初始化执行结果  失败0
        try {
            if(getConn())//打开数据库链接
            {
                ps = conn.prepareStatement(sql);
                if(objs!=null){
                    for (int i = 0; i < objs.length; i++) {
                        ps.setObject((i+1),objs[i]);
                    }
                }
                res = ps.executeUpdate();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource();//关闭数据源
        }
        return res;
    }
    /**
     * 查询
     * @param sql
     * @param objs
     * @return
     */
    public ResultSet executeSQL(String sql,Object objs[]){

        ResultSet rs = null;
        try {
            if(getConn())//打开数据库链接
            {
                ps = conn.prepareStatement(sql);
                //判断是否有参数
                if (objs != null) {
                    //循环封装参数
                    for (int i = 0; i < objs.length; i++) {
                        ps.setObject((i + 1), objs[i]);
                    }
                }
                rs = ps.executeQuery();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource();//释放资源
        }

        return rs;
    }





    //关闭资源
    public void closeResource(){
        try {
            if(ps!=null)
            {
                ps.close();
            }
            if(conn!=null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}

调用工具类

实现类 继承 工具类(BaseDao)

查询:ResultSet rs = this.executeSQL(SQL语句,Object数组<参数数组>)

增,删,改: int i = this.executeUpdate(SQL语句,Object数组<参数数组>)

使用配置文件存储连接信息(properties文件)

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/xxshop?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.pwd=root

   Properties properties = new Properties();
//读取properties文件  BaseDao为当前所在类
 InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("jdbc.properties");
            //将文件信息转换成properties对象
            properties.load(is);
            //通过getProperty(KEY)方法获取属性值
            String driver =  properties.getProperty("jdbc.driver");


原文地址:https://blog.csdn.net/QAZ412803/article/details/142476354

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!