JDBC_5 DBUtils[通俗易懂]

JDBC_5 DBUtils[通俗易懂]DBUtilscommons-dbutils是Apache组织提供的一个开源JDBC工具类库,封装了针对于数据库的增删改查操作APIQueryRunnerResulSetHandlerDbutils插入举例Connection conn = null; try { QueryRunner runner = new QueryRunner(); conn = JBBCUtils.getConnections3();

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

DBUtils


commons-dbutils是Apache组织提供的一个开源JDBC工具类库,封装了针对于数据库的增删改查操作

API

  • QueryRunner
  • ResulSetHandler
  • Dbutils

插入举例

Connection conn = null;
        try { 
   
            QueryRunner runner = new QueryRunner();
            conn = JBBCUtils.getConnections3();
            String sql = "insert into customers(name,emial,birth) values(?,?,?)";
            int insertCount = runner.update(conn,sql,"蔡旭坤","caixukun@163.com","1993-03-01");
            System.out.println(insertCount);
        } catch (SQLException throwables) { 
   
            throwables.printStackTrace();
        }finally { 
   
            JDBCUtils.closeResource(conn,null);
        }

更新


    public static void main(String[] args) { 
   

        Connection conn = null;
        try { 
   
            QueryRunner runner = new QueryRunner();
            conn = JBBCUtils.getConnections3();
            String sql = "select id,name,email,birth from customers where id < ?";
            BeanListHandler<Customer> handler = new BeanListHandler<Cunstomer>(customer.class);
            List<Customer> query = runner.query(conn.sql, handler, 23);
            System.out.println(insertCount);
        } catch (SQLException throwables) { 
   
            throwables.printStackTrace();
        }finally { 
   
            JDBCUtils.closeResource(conn,null);
        }
    }

查询表中特殊值

public static void main(String[] args) { 
   

        Connection conn = null;
        try { 
   
            QueryRunner runner = new QueryRunner();
            conn = JBBCUtils.getConnections3();
            String sql = "select count(*) from customers";
            ScalarHandler handler = new ScalarHandler();
            
            Long count = (Long)runner.query(conn.sql, handler);
            System.out.println(count);
        } catch (SQLException throwables) { 
   
            throwables.printStackTrace();
        }finally { 
   
            JDBCUtils.closeResource(conn,null);
        }
    }

自定义ResultSetHandler

public static void main(String[] args) { 
   

        Connection conn = null;
        try { 
   
            QueryRunner runner = new QueryRunner();
            conn = JBBCUtils.getConnections3();
            String sql = "select id,name,email,birth from customers where id = ?";
            ResultSetHandler<Customers> resultSetHandler = new ResultSetHandler<Customers>() { 
   
                @Override
                public Customers handle(ResultSet rs) throws SQLException { 
   
                    if(resultSet.next()){ 
   
                        int id = rs.getInt(1);
                        String name = rs.getString(2);
                        String email = rs.getString(3);
                        Date birth = rs.getDate(4);
                        Customer customer = new Cunstomer(id,name,email,birth);
                        return customer;
                    }
                    return null;
                }
            };

            Long count = (Long)runner.query(conn.sql, handler);
            System.out.println(count);
        } catch (SQLException throwables) { 
   
            throwables.printStackTrace();
        }finally { 
   
            JDBCUtils.closeResource(conn,null);
        }
    }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/168984.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 外地人在北京的真实状态(漫画)

    这就是外地人在北京的真实状态! 北京这个城市看似中国的首都,实则在这里很不容易,但是感谢阿Q的存在,能把这种苦逼包装成一种高大上的状态。早上每天5:30起床睡眠不足,我们包装成早期…

    2021年6月21日
    185
  • lspci 安装_ipswme下载的系统如何安装

    lspci 安装_ipswme下载的系统如何安装在原版的busy-box做出来的文件系统中,lspci命令只有两个选项:ynq>lspci-hlspci:invalidoption–‘h’BusyBoxv1.20.1(2012-11-2713:37:12MST)multi-callbinary.Usage:lspci[-mk]ListallPCIdevices-m

    2022年9月10日
    0
  • python 图片重命名_python文件改名

    python 图片重命名_python文件改名由于两个文件夹下的图片名字是一样的,但是我想让另一个文件夹接在一个文件夹之后重新命名,也就是从732.jpg开始递增命名。想到以后可能还会经常遇到这种情况,所以还是保存一下,以后就懒得再重新写了。”’图像批量重命名”’importosstart=732#开始的序号image_dir=’./output/’#源图片路径images_list=os.listdir(image_dir)nums=len(os.listdir(image_dir))print

    2022年9月5日
    1
  • 理解HTTP和TCP

    理解HTTP和TCP TCP协议对应于传输层,而HTTP协议对应于应用层,从本质上来说,二者没有可比性。Http协议是建立在TCP协议基础之上的,当浏览器需要从服务器获取网页数据的时候,会发出一次Http请求。Http会通过TCP建立起一个到服务器的连接通道,当本次请求需要的数据完毕后,Http会立即将TCP连接断开,这个过程是很短的。所以Http连接是一种短连接,是一种无状态的连接。所谓的无状态,是指浏览器每次向服…

    2022年9月14日
    1
  • 初笔,JAVA.HelloWorld代码详解「建议收藏」

    初笔,JAVA.HelloWorld代码详解「建议收藏」HelloWorld.java//文件名publicclassHelloWorld{  publicstaticvoidmain(String[]args){    System.out.println(“HelloWorld!!!”);}}详解:publicclassHelloWorld:class:翻译过来就叫:类,可以理解为是JAVA中一种文件的名字….

    2022年5月28日
    30
  • Android 用ListView实现排序「建议收藏」

    Android 用ListView实现排序「建议收藏」点击“单价”按钮或“数量信息”按钮,可按据升序或降序进行排序。布局没什么好说的在这里插入代码片<LinearLayoutxmlns:android=”http://schemas.android.com/apk/res/android”xmlns:app=”http://schemas.android.com/apk/res-auto”xmlns:tools=”http://schemas.android.com/tools”android:layout_widt

    2022年9月2日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号