zoukankan      html  css  js  c++  java
  • JDBC

     1     /**
     2      * Query the sql tool class of a single class.
     3      * @param sql sql
     4      * @param args args
     5      * @return class
     6      */
     7     public Tab getQuerySql(String sql, Object... args) {
     8         try (PreparedStatement ps = connection.prepareStatement(sql)) {
     9             for (int i = 0; i < args.length; i++) {
    10                 ps.setObject(i + 1, args[i]);
    11             }
    12             // Get ResultSet
    13             ResultSet resultSet = ps.executeQuery();
    14             // Get the metadata of the result set
    15             ResultSetMetaData metaData = resultSet.getMetaData();
    16             // Get the number of columns
    17             int columnCount = metaData.getColumnCount();
    18             if (resultSet.next()) {
    19                 Tab tab = new Tab();
    20                 for (int i = 0; i < columnCount; i++) {
    21                     // Get the column value of each column: through ResultSet
    22                     Object columnValue = resultSet.getObject(i + 1);
    23                     // Get the alias of the column through ResultSetMetaData
    24                     String columnLabel = metaData.getColumnLabel(i + 1);
    25                     // Through reflection, assign the specified name of the object columnLabel to the specified columnValue
    26                     Field field = Tab.class.getDeclaredField(columnLabel);
    27                     field.setAccessible(true);
    28                     field.set(tab, columnValue);
    29                 }
    30                 return tab;
    31             }
    32         } catch (Exception throwables) {
    33             throwables.printStackTrace();
    34         }
    35         return null;
    36     }
    37 
    38     /**
    39      * Add, delete, modify and check a single class.
    40      * @param sql sql 
    41      * @param args args
    42      * @return boolean
    43      */
    44     public boolean executeSql(String sql, Object... args) {
    45         try (PreparedStatement ps = connection.prepareStatement(sql)) {
    46             for (int i = 0; i < args.length; i++) {
    47                 ps.setObject(i + 1, args[i]);
    48             }
    49             return ps.execute();
    50         } catch (Exception throwables) {
    51             throwables.printStackTrace();
    52         }
    53         return false;
    54     }
  • 相关阅读:
    C语言中static修饰符的意义
    网络字节序
    socket接口详解
    Python实现常见算法[3]——汉罗塔递归
    Python实现常见算法[2]——快速排序
    Python实现常见算法[1]——冒泡排序
    关于钛星设备主动推送报文的解析
    2015自我提升计划
    python 基础(函数 命名空间和作用域,嵌套和作用域链,闭包)
    python 基础(文件操作,注册,以及函数)
  • 原文地址:https://www.cnblogs.com/yhc-love-cl/p/15003687.html
Copyright © 2011-2022 走看看