• Java 使用 Dbutils 工具类库 操作mysql


    Dbutils 官网http://commons.apache.org/proper/commons-dbutils/

    结合我个人写的JDBCUtils 写的 Dbutils 的使用demo . JDBCUtils 传送门:http://www.cnblogs.com/wwcherish/p/8037395.html

      1 package JDBCDemo;
      2 
      3 import java.sql.SQLException;
      4 import java.util.ArrayList;
      5 import java.util.HashMap;
      6 import java.util.List;
      7 import java.util.Map;
      8 
      9 import javax.xml.bind.annotation.W3CDomHandler;
     10 
     11 import org.apache.commons.dbutils.DbUtils;
     12 import org.apache.commons.dbutils.QueryRunner;
     13 import org.apache.commons.dbutils.handlers.ArrayHandler;
     14 import org.apache.commons.dbutils.handlers.ArrayListHandler;
     15 import org.apache.commons.dbutils.handlers.BeanHandler;
     16 import org.apache.commons.dbutils.handlers.BeanListHandler;
     17 import org.apache.commons.dbutils.handlers.ColumnListHandler;
     18 import org.apache.commons.dbutils.handlers.MapHandler;
     19 import org.apache.commons.dbutils.handlers.MapListHandler;
     20 import org.apache.commons.dbutils.handlers.ScalarHandler;
     21 
     22 import com.mysql.jdbc.Connection;
     23 
     24 public class dbutilsdemo1 {
     25     private static Connection connection  = JDBCUtils.getConnection();
     26     private static QueryRunner qRunner = new QueryRunner();
     27     public static void main(String[] args) {
     28         try {
     29             //insert();
     30             //delete();
     31             //ArraryHandlerdemo();
     32 //            beanListHandlerDemo();
     33             //ColumnListHandlerDemo();
     34             //mapHandlerDemo();
     35             mapListHandlerDemo();
     36         } catch (Exception e) {
     37             // TODO: handle exception
     38             System.out.println(e);
     39         }
     40         
     41     }
     42     public static void insert() throws SQLException {
     43         //insert  delete update 
     44         String sql = "insert into zhangwu (name,money) values (?,?);";
     45         Object[] object = {"打麻将支出",100};
     46         int row = qRunner.update(connection,sql,object);
     47         DbUtils.closeQuietly(connection);
     48         if (row>0) {
     49             System.out.println("插入数据成功");
     50         }else {
     51             System.out.println("插入数据失败");
     52         }
     53         
     54     }
     55     
     56     public static void update() throws SQLException {
     57         String sql = "update zhangwu set money=? where id=?;";
     58         Object[] object = {500,4};
     59         int row = qRunner.update(connection,sql,object);
     60         DbUtils.closeQuietly(connection);
     61         if (row>0) {
     62             System.out.println("修改数据成功");
     63         }else {
     64             System.out.println("修改数据失败");
     65         }
     66     }
     67     
     68     public static void delete() throws SQLException {
     69         String sql = "delete from  zhangwu  where id=?;";
     70         Object[] object = {100};
     71         int row = qRunner.update(connection,sql,object);
     72         DbUtils.closeQuietly(connection);
     73         if (row>0) {
     74             System.out.println("删除数据成功");
     75         }else {
     76             System.out.println("删除数据失败");
     77         }
     78     }
     79     
     80     public static void ArraryHandlerdemo () throws SQLException {
     81         //返回1条数据集
     82         //ArraryHandler
     83         String sql = "select * from  zhangwu;";
     84         Object[] res = qRunner.query(connection, sql, new ArrayHandler());
     85         for(Object object : res) {
     86             System.out.print(object+"	");
     87         }
     88         
     89     }
     90     public static void ArraryListHandlerDemo() throws SQLException {
     91         //返回整个数据集
     92         String sql = "select * from  zhangwu;";
     93         List<Object[]>  res = qRunner.query(connection, sql, new ArrayListHandler());
     94         for(Object[] objs : res) {
     95             for(Object oo : objs) {
     96                 System.out .print(oo  + "	");
     97             }
     98             System.out.println("");
     99         }
    100     }
    101     public static void beanHandler() throws SQLException {
    102         //返回1条数据集
    103         String sql = "select * from  zhangwu;";
    104         Zhangwu zw = qRunner.query(connection,sql,new BeanHandler<Zhangwu>(Zhangwu.class));
    105         System.out.println(zw);
    106     }
    107     public static void beanListHandlerDemo() throws SQLException{
    108         //返回整个数据集
    109         String sql = "select * from  zhangwu;";
    110         List<Zhangwu> zwlist= qRunner.query(connection, sql, new BeanListHandler<Zhangwu>(Zhangwu.class));
    111         for(Zhangwu zwchild : zwlist) {
    112             System.out.println(zwchild.toString());
    113         }
    114     }
    115     public static void ColumnListHandlerDemo()  throws SQLException {
    116         //单列结果查询
    117         String sql = "select distinct name from  zhangwu";
    118         List<Object> zwlist= qRunner.query(connection, sql, new ColumnListHandler<Object>("name"));
    119         for(Object zwchild : zwlist) {
    120             System.out.println(zwchild.toString());
    121         }
    122     }
    123     public static void scalarHandlerDemo() throws SQLException {
    124         //单结果集查询
    125         String sql = "select count(*) from  zhangwu;";
    126         Long num =  qRunner.query(connection, sql, new ScalarHandler<Long>());
    127         System.out.println(num);
    128     }
    129     public static void mapHandlerDemo() throws SQLException {
    130         //结果集第一行数据 封装到map集合中
    131         String sql = "select * from  zhangwu;";
    132         Map< String,Object >  map= qRunner.query(connection,sql,new MapHandler());
    133         for(String key : map.keySet()) {
    134             System.out.println(key + "..." + map.get(key));
    135         }
    136     }
    137     public static void mapListHandlerDemo() throws SQLException {
    138         // 封装到map集合中 适合少量数据
    139         String sql = "select * from  zhangwu;";
    140         List<Map<String, Object>>  listmap = qRunner.query(connection, sql, new MapListHandler());
    141         for(Map<String, Object> mapchild : listmap) {
    142             for(String key : mapchild.keySet()) {
    143                 System.out.print(key + " = " + mapchild.get(key) + "  ");
    144             }
    145             System.out.println("");
    146         }
    147     }
    148 }
  • 相关阅读:
    剑指offer---二叉搜索树的第k个结点
    剑指offer---把数组排成最小的数
    剑指offer---连续子数组的最大和
    剑指offer---最小的K个数
    Navicat for MySQL(Ubuntu)过期解决方法
    Ubuntu 无法应用原保存的显示器配置
    ubuntu 18.04 install gitlab-ce
    Flask 使用过程
    python版本 3.7.4rc1 (stable) / 3.8.0b1 (pre-release) / 3.9.0a0 (in development)
    Windows10 and MySQL使用
  • 原文地址:https://www.cnblogs.com/wwcherish/p/8037705.html
Copyright © 2020-2023  润新知