zoukankan      html  css  js  c++  java
  • 四、元数据ParameterMetaData

    参考:https://www.jianshu.com/p/36d5d76342f1

    获得代表PreparedStatement元数据的ParameterMetaData对象,简单来说: 获得预编译SQL语句中 “?” 信息。

    Select * from user where name=? And password=?
    • 获得对象:PreparedStatement.getParameterMetaData()

    • ParameterMetaData类的常用方法:

      • getParameterCount()
        获得指定参数的个数

      • getParameterType(int param)
        获得指定参数的sql类型

        并不是所有数据库都支持,使用MySQL时,这个方法只会返回varchar的int值(即14)

      • getParameterTypeName(int param) --- 参数类型名称

    例:

     1      Connection conn = null;
     2         PreparedStatement ps = null;
     3         try {
     4             conn = JdbcUtil.getConnection();
     5             conn.setAutoCommit(false);
     6             ps = conn.prepareStatement(sql);
     7             //得到参数的个数
     8             ParameterMetaData pmd = ps.getParameterMetaData();
     9             //绑定参数
    10             for(int i = 0;i<pmd.getParameterCount();i++){
    11                 ps.setObject(i+1,parm[i]);
    12             }
    13             ps.executeUpdate();
    14             conn.commit();
  • 相关阅读:
    343. Integer Break
    338. Counting Bits
    322. Coin Change
    304. Range Sum Query 2D
    303. Range Sum Query
    221. Maximal Square
    213. House Robber II
    cf
    poj2478欧拉函数
    lightoj1138
  • 原文地址:https://www.cnblogs.com/qiaoxin11/p/12822468.html
Copyright © 2011-2022 走看看