JDBC-封装工具类

本文介绍了如何使用JDBCUtils和Druid工具类进行数据库操作,包括JDBC连接的获取、预编译SQL执行以及资源的管理和关闭。两个示例分别展示了DML(数据操作语言)和SELECT操作。
摘要由CSDN通过智能技术生成

工具类封装

public class JDBCUtils {
    // 定义相关属性
    private static String user;
    private static String password;
    private static String url;
    private static String driver;

    // 在静态代码块初始化
    static {
        try {
            Properties properties = new Properties();
            properties.load(new FileInputStream("src\\mysql.properties"));
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String url = properties.getProperty("url");
            String driver = properties.getProperty("driver");

        } catch (IOException e) {
            // 将编译异常转成运行异常
            // 这时调用者可以选择捕获该异常或者选择默认处理
            throw  new RuntimeException(e);
        }
    }

    // 连接数据库
    public static Connection getConnection(){
        try {
            Class.forName(driver);
            return DriverManager.getConnection(url,user,password);
        } catch (Exception e) {
            throw  new RuntimeException(e);
        }

    }

    // 关闭相关资源
    // 如果需要关闭就传入资源,不然就传入null
    public static void close(ResultSet set, Statement statement, Connection connection){
        try {
            if(set != null){
                set.close();
            }
            if(statement != null){
                statement.close();
            }
            if(connection != null){
                connection.close();
            }
        } catch (SQLException e){
            throw new RuntimeException(e);
        }

    }
}

调用

public class JDBCUtils_Use {
    @Test
    public void testDML() throws Exception{
        Connection connection = JDBCUtils.getConnection();

        String sql = "update news set content = ? where id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,"ybj is a man");
        preparedStatement.setInt(2,3);

        int rows = preparedStatement.executeUpdate();
        System.out.println(rows > 0 ? "true" : "false");

        JDBCUtils.close(null,preparedStatement,connection);
    }

    @Test
    public void testSelect() throws Exception{
        Connection connection = JDBCUtils.getConnection();
        String sql = "select * from news";
        // select中列名不可以用?做占位符,可以写明要查询的列select id,content from news
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while(resultSet.next()){
            int id = resultSet.getInt("id");
            String content = resultSet.getString("content");
            System.out.println(id + "\t" + content);
        }
        JDBCUtils.close(resultSet,preparedStatement,connection);
    }
}

Druid工具类

public class JDBCUtilsByDruid {
    private static DataSource ds;

    // 在静态代码块中完成ds初始化
    static {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("src\\druid.properties"));
            ds = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static Connection getConnection() throws SQLException {
        return  ds.getConnection();
    }

    // 不是关闭链接,是把connection放回连接池
    public static void closeConnection(ResultSet resultSet, Connection connection, Statement statement) {
        try {
            if(resultSet != null){
                resultSet.close();
            }
            if (connection != null){
                connection.close();
            }
            if(statement != null){
                statement.close();
            }
        }catch (SQLException e){
            throw new RuntimeException(e);
        }

    }
}

调用

public class JDBCUtilsDruid_Use {

    public void testDML() throws Exception{
        Connection connection = JDBCUtilsByDruid.getConnection();
        String sql = "insert into news set content = ? where id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,"cyt");
        preparedStatement.setInt(2,1);

        preparedStatement.executeUpdate();

        JDBCUtilsByDruid.closeConnection(null,connection,preparedStatement);
    }

    public void testSelect() throws Exception{
        Connection connection = JDBCUtilsByDruid.getConnection();

        String sql = "select * from news";

        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String content = resultSet.getString("content");
            System.out.println(id + "\t" + content);
        }

        JDBCUtilsByDruid.closeConnection(resultSet,connection,preparedStatement);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值