使用JAVA程序封装数据库的增删改,查语句

使用JAVA程序封装数据库的增删改,查语句

  • 使用jdbc操作数据库时,需要书写sql语句,而且当操作的需求更改时,这个方法就不能使用了,但其中的大部分实现代码都是相同,所以我们自己封装一个方法,用来对所有的增删改操作都有效的方法
  • 实现封装功能使用到了可变参数
  • 封装增删改语句的代码如下:
  //创建连接对象
    private Conn conn = new Conn();

    //封装增删改语句
    public int operationSql(String sql, Object... objects) {
        //获取连接
        Connection connection = conn.getConnection();
        PreparedStatement preparedStatement = null;
        //定义统计变量
        int count = 0;
        try {
            preparedStatement = connection.prepareStatement(sql);
            //通过for循环设置sql语句中每个占位符的值
            for (int i = 0; i < objects.length; i++) {
                preparedStatement.setObject(i + 1, objects[i]);
            }
            count = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeSql(connection,preparedStatement,null);
        }
        return count;

    }
  • 在进行数据库操作之间必须要先获取到一个数据库连接。

  • 这里解释一下prepareStatement和它的父类Statement的使用和区别:
    1,PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程
    2,statement每次执行sql语句,相关数据库都要执行sql语句的编译,preparedstatement是预编译得, preparedstatement支持批处理的
    3,使用 Statement 对象。在对数据库只执行一次性存取的时侯,用 Statement 对象进行处理。PreparedStatement 对象的开销比Statement大
    4,因为prepareStatement是预编译的,所以它还可以预防SQL注入

  • executeUpdate()这个方法就是执行数据库增,删,改 操作的,会返回一个int值该值代表的指示受影响的行数(即更新计数)。

  • 封装查询语句的代码如下:

 public void selectSql(String sql, Object... objects) {
        //获取连接
        Connection connection = conn.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
              //通过for循环设置sql语句中每个占位符的值
            for (int i = 0; i < objects.length; i++) {
                preparedStatement.setObject(i+1,objects[i]);
            }
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                System.out.println(resultSet.getInt(1)+"\t"+resultSet.getString(2)+"\t"+resultSet.getInt(3));
            }
        } catch (SQLException e) {
            e.printStackTrace();

        }finally {
            closeSql(connection,preparedStatement,resultSet);
        }

    }
  • 同样,操作前必须要获取一个数据库连接
  • executeQuery()这个方法就是执行数据库查询操作的,并且它还会返回一个resultSet结果集。通过遍历获取集合中的数据
  • 大家注意到在每个try … catch 后面都有finally
  • 因为我们在使用结束后需要关闭资源,使用数据库连接池的话,可以不用考虑关闭连接的占用资源的问题。这里只是讲述一下关闭资源的额方法和流程。
  • 现在我们将它封装成一个方法,方法代码如下:
 public void closeSql(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        System.out.println("关闭成功!!");
    }

当资源对象不为空时,调用它的close()方法关闭资源。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值