前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jdbc fetchsize_Sharding-JDBC

jdbc fetchsize_Sharding-JDBC

作者头像
全栈程序员站长
发布2022-09-21 08:32:34
9130
发布2022-09-21 08:32:34
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

DBUtils


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

API

  • QueryRunner
  • ResulSetHandler
  • Dbutils

插入举例

代码语言:javascript
复制
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);
        }

更新

代码语言:javascript
复制
    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);
        }
    }

查询表中特殊值

代码语言:javascript
复制
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

代码语言:javascript
复制
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.cn/168819.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • DBUtils
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档