美文网首页
MySQL 基础21 DBUtils的CRUD操作

MySQL 基础21 DBUtils的CRUD操作

作者: 小熊先生很不开心 | 来源:发表于2018-03-20 08:38 被阅读2次

1.1 DBUtils的添加操作

1.1.1 工具类的准备

public class JDBCUtils2 {
    // 创建一个连接池:但是这个连接池只需要创建一次即可。
    private static final ComboPooledDataSource dataSource = new ComboPooledDataSource();
    
    /**
     * 获得连接的方法
     * @throws SQLException 
     */
    public static Connection getConnection() throws SQLException{
        return dataSource.getConnection();
    }
    
    /**
     * 获得连接池:
     */
    public static DataSource getDataSource(){
        return dataSource;
    }
    
    /**
     * 释放资源的方法
     */
    public static void release(Statement stmt,Connection conn){
        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            
            stmt = null;
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }
    
    public static void release(ResultSet rs,Statement stmt,Connection conn){
        // 资源释放:
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            
            rs = null;
        }
        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            
            stmt = null;
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }
}

1.1.2 DBUtils的添加操作


@Test
    /**
     * 添加操作
     */
    public void demo1() throws SQLException{
        // 创建核心类:QueryRunner:
        QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
        queryRunner.update("insert into account values (null,?,?)", "ddd",10000);
    }

1.2 DBUtils的修改操作

@Test
    /**
     * 修改操作
     */
    public void demo2() throws SQLException{
        // 创建核心类:
        QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
        queryRunner.update("update account set name=?,money=? where id =?", "eee",20000,4);
    }

1.3 DBUtils的删除操作

@Test
    /**
     * 删除操作
     */
    public void demo3() throws SQLException{
        // 创建核心类:
        QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
        queryRunner.update("delete from account where id = ?", 3);
    }

1. 4DBUtils的查询操作

数据库和表自己工具JavaBean来创建

创建JavaBean

public class Account implements Serializable{
    
    private static final long serialVersionUID = -1204360090882362330L;
    private int id;
    private String name;
    private double  money;
    public Account() {
        super();
        // TODO Auto-generated constructor stub
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getMoney() {
        return money;
    }
    public void setMoney(double money) {
        this.money = money;
    }
    @Override
    public String toString() {
        return "Account [id=" + id + ", name=" + name + ", money=" + money
                + "]";
    }

    

}



ublic class DBUtilsDemo2 {

    @Test
    /**
     * 查询一条记录的操作
     */
    public void demo1() throws SQLException{
        // 创建核心类:
        QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
        // 执行查询:
        Account account = queryRunner.query("select * from account where id = ?", new ResultSetHandler<Account>(){

            @Override
            public Account handle(ResultSet rs) throws SQLException {
                Account account = new Account();
                if(rs.next()){
                    account.setId(rs.getInt("id"));
                    account.setName(rs.getString("name"));
                    account.setMoney(rs.getDouble("money"));
                }
                return account;
            }
            
        }, 1);
        System.out.println(account);
    }
    
    @Test
    /**
     * 查询多条记录
     */
    public void demo2() throws SQLException{
        // 创建核心类:
        QueryRunner queryRunner = new QueryRunner(JDBCUtils2.getDataSource());
        // 执行查询:
        List<Account> list = queryRunner.query("select * from account", new ResultSetHandler<List<Account>>(){
            @Override
            public List<Account> handle(ResultSet rs) throws SQLException {
                // 创建一个集合用于封装数据:
                List<Account> list = new ArrayList<Account>();
                while(rs.next()){
                    // 将结果集中的数据封装到对象中
                    Account account = new Account();
                    account.setId(rs.getInt("id"));
                    account.setName(rs.getString("name"));
                    account.setMoney(rs.getDouble("money"));
                    // 将这个对象存入到list集合:
                    list.add(account);
                }
                return list;
            }
        });
        // 使用集合:
        for (Account account : list) {
            System.out.println(account);
        }
    }
}

相关文章

  • MySQL 基础21 DBUtils的CRUD操作

    1.1 DBUtils的添加操作 1.1.1 工具类的准备 1.1.2 DBUtils的添加操作 1.2 DBUt...

  • golang第五天

    学习go操作mysql,crud测试 代码 总结 go操作mysql 打卡时间: 21:04

  • C++中使用MySQL

    原文地址: 不详 本节内容是我们的重点。学好了MySQL对CRUD的基础操作,那么如何跟C++代码结合起来呢? M...

  • DbUtils

    分类说明名称DbUtils全称Commons DbUtils功能数据库操作组件 Common DbUtils是由A...

  • MySQL的基本语法

    MySQL的基本语法 CRUD的操作: C:create 增 R:read 查 U:update 修改 D:de...

  • java49(DBUtils)

    DBUtils: DBUtils是java编程中的数据库操作实用工具,小巧简单使用 DBUtils...

  • NO3:Node.js + mysql+Sequelize实现注

    在上一篇的基础上,继续升级! NO2:Node JS + MySQL CRUD Workshop : Insert...

  • MySQL 基础20 DBUtils介绍

    1.1 DBUtils的概述   Commons DbUtils是Apache组织提供的一个对JDBC进行简单封装...

  • DBUtils

    DBUtils介绍 apache 什么是dbutils,它的作用DBUtils是java编程中的数据库操作实用工...

  • 32 使用QueryRunner完成数据库操作

    本节将演示使用DbUtils中的QueryRunner完成数据库的操作。 1、前提步骤 已经安装好mysql数据库...

网友评论

      本文标题:MySQL 基础21 DBUtils的CRUD操作

      本文链接:https://www.haomeiwen.com/subject/cnorqftx.html