DBUtil三种封装思想,BaseDao的封装

连接数据库和关闭数据库

连接数据库

  public Connection getConnection(){
        try {
            Class.forName(driver);
            connection= DriverManager.getConnection(url,jdbcuser,pwd);
        }catch (Exception e){
            e.printStackTrace();
        }
        return connection;
    }

关闭数据库

    public void closeResouce(ResultSet resultSet, PreparedStatement preparedStatement,Connection connection){
        if (resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (preparedStatement!=null){
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

实现增删改的basedao封装

public int executeUpdate(String sql,Object[] params){
        DBUtil db=new DBUtil();
        int num=0;
        Connection connection=db.getConnection();
        try {
            java.sql.PreparedStatement preparedStatement=connection.prepareStatement(sql);
            if (params!=null){
                for (int i=0;i<params.length;i++){
                    preparedStatement.setObject((i+1),params[i]);
                }
            }
            num=preparedStatement.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

实现查询的BaseDao封装

public ResultSet executeQuery(String sql,Object[] params){
        DBUtil db=new DBUtil();
        ResultSet resultSet=null;
        Connection connection=db.getConnection();
        try {
            PreparedStatement preparedStatement= (PreparedStatement) connection.prepareStatement(sql);
            if (params!=null){
                for (int i=0;i<params.length;i++){
                    preparedStatement.setObject((i+1),params[i]);
                }
            }
            resultSet=preparedStatement.executeQuery();
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultSet;
    }

获取db.propertues资源,static表示一打开就会自动加载

static {
        try {
            properties.load(DBUtil.class.getClassLoader().getResourceAsStream("db.properties"));
            driver=properties.getProperty("driver");
            url=properties.getProperty("url");
            jdbcuser=properties.getProperty("jdbcuser");
            pwd=properties.getProperty("pwd");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

解决维护性功能properties

private static String driver;
private static String url;
private static String jdbcuser;
private static String pwd;
public Connection connection=null;
public static Properties properties=new Properties();

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值