(1)连接池的配置步骤:

  1. 导入连接池相关jar包,如c3p0或druid。
  2. 在配置文件中配置连接池的相关参数,如数据库连接URL、用户名、密码等。
  3. 在代码中通过连接池获取连接。

(2)数据库操作类的封装:

示例代码:

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;

public class DbUtil {
    private static DataSource dataSource; // 数据源

    // 初始化数据源
    static {
        // 读取连接池配置文件,如c3p0-config.xml或druid.properties
        // 创建数据源
        // dataSource = ...
    }

    // 获取连接
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    // 关闭连接
    public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    // 基本查询
    public static ResultSet query(String sql) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            return rs;
        } catch (SQLException e) {
            throw e;
        } finally {
            close(conn, ps, null);
        }
    }

    // 分页查询
    public static ResultSet query(String sql, int start, int pageSize) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql + " limit ?, ?");
            ps.setInt(1, start);
            ps.setInt(2, pageSize);
            rs = ps.executeQuery();
            return rs;
        } catch (SQLException e) {
            throw e;
        } finally {
            close(conn, ps, null);
        }
    }

    // 预编译查询
    public static ResultSet query(String sql, Object... params) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
            rs = ps.executeQuery();
            return rs;
        } catch (SQLException e) {
            throw e;
        } finally {
            close(conn, ps, null);
        }
    }

    // 预编译增删改操作
    public static int update(String sql, Object... params) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
            int result = ps.executeUpdate();
            conn.commit();
            return result;
        } catch (SQLException e) {
            if (conn != null) {
                conn.rollback();
            }
            throw e;
        } finally {
            close(conn, ps, null);
        }
    }

    // 事务和预编译增删改操作
    public static int[] batchUpdate(String sql, Object[][] params) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = getConnection();
            conn.setAutoCommit(false);
            ps = conn.prepareStatement(sql);
            for (Object[] param : params) {
                for (int i = 0; i < param.length; i++) {
                    ps.setObject(i + 1, param[i]);
                }
                ps.addBatch();
            }
            int[] result = ps.executeBatch();
            conn.commit();
            return result;
        } catch (SQLException e) {
            if (conn != null) {
                conn.rollback();
            }
            throw e;
        } finally {
            close(conn, ps, null);
        }
    }
}

(3)封装类测试:

示例代码:

import java.sql.ResultSet;
import java.sql.SQLException;

public class Test {
    public static void main(String[] args) {
        try {
            // 建表
            DbUtil.update("create table user(id int primary key, name varchar(20), age int)");

            // 插入数据
            DbUtil.update("insert into user(id, name, age) values(?, ?, ?)", 1, "张三", 20);
            DbUtil.update("insert into user(id, name, age) values(?, ?, ?)", 2, "李四", 30);
            DbUtil.update("insert into user(id, name, age) values(?, ?, ?)", 3, "王五", 40);

            // 查询数据
            ResultSet rs = DbUtil.query("select * from user");
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                int age = rs.getInt("age");
                System.out.println(id + " " + name + " " + age);
            }

            // 更新数据
            DbUtil.update("update user set age = ? where id = ?", 25, 1);

            // 删除数据
            DbUtil.update("delete from user where id = ?", 2);

            // 分页查询数据
            rs = DbUtil.query("select * from user", 1, 2);
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                int age = rs.getInt("age");
                System.out.println(id + " " + name + " " + age);
            }

            // 预编译查询数据
            rs = DbUtil.query("select * from user where id = ?", 1);
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                int age = rs.getInt("age");
                System.out.println(id + " " + name + " " + age);
            }

            // 预编译增删改数据
            DbUtil.update("update user set age = ? where id = ?", 30, 1);

            // 事务和预编译增删改数据
            Object[][] params = { { 2, "李四", 35 }, { 3, "王五", 45 } };
            DbUtil.batchUpdate("update user set name = ?, age = ? where id = ?", params);

            // 删除表
            DbUtil.update("drop table user");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
用java构建数据库的操作类要求实现功能:1操作类应支持数据库连接池。写出连接池的配置步骤。2完成数据库操作类的封装。要求:通过连接池获得连接;封装时至少应实现基本查询、支持分页的查询、支持预编译的查询、支持预编译的增删改操作、支持事务和预编译的增删改操作。3封装类测试:构建数据库表实现对表的增删改查基本操作。

原文地址: https://www.cveoy.top/t/topic/8np 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录