首页 > 其他分享 >坑爹奇葩的jdbc getColumnName(index)竟然从1开始

坑爹奇葩的jdbc getColumnName(index)竟然从1开始

时间:2022-11-24 13:08:02浏览次数:38  
标签:index jdbc String columnName 坑爹 object resultSet column SQLException


/**
* Get the designated column's name.
*
* @param column the first column is 1, the second is 2, ...
* @return column name
* @exception SQLException if a database access error occurs
*/
String getColumnName(int column) throws SQLException;

/**
* Get the designated column's table's schema.
*
* @param column the first column is 1, the second is 2, ...
* @return schema name or "" if not applicable
* @exception SQLException if a database access error occurs
*/
String getSchemaName(int column) throws SQLException;

实现

/**
* Return the column descriptor given a column index.
*
* @param column The column index (from 1 .. n).
* @return The column descriptor as a <code>ColInfo<code>.
* @throws SQLException
*/
ColInfo getColumn(int column) throws SQLException {
if (column < 1 || column > columnCount) {
throw new SQLException(
Messages.get("error.resultset.colindex",
Integer.toString(column)), "07009");
}

return columns[column - 1];
}

我本来是写的从1开始的,后面写webapi我自己都纳闷,额,我怎么给写成1了,怎么这么低级的错误,我就把哪个地方改成了0 ,把<=count 改成count,然后切换为jdbc模式的时候出错了,坑爹,坑爹,必须记录下来,搞得我改过来改过去

while (resultSet.next()) {
ResultSetMetaData rsMetaData = resultSet.getMetaData();
//检索列名列表
LinkedHashMap<String, Object> hashMap = new LinkedHashMap<>();
int count = rsMetaData.getColumnCount();
keymaploop:
for (int i = 1; i <= count; i++) {// getColumn 从1 开始
String columnName = rsMetaData.getColumnName(i);
int columnType = rsMetaData.getColumnType(i);
Object object = null;
if (columnType == Types.VARBINARY) {
object = resultSet.getBinaryStream(columnName);
} else if (columnType == Types.CLOB) {
Clob clob = resultSet.getClob(columnName);
object = ClobToString(clob);
} else if (columnType == Types.BLOB) {
Blob blob = resultSet.getBlob(columnName);
if (blob != null) {
object = blob.getBinaryStream();
}
} else {
object = resultSet.getObject(columnName);
}
if (onlyOneColumn) {
String result = String.valueOf(object == null ? "" : object);
if (!TextUtils.isEmpty(result)) {
list.add(result);
}
break keymaploop;//只查询一列作为list
} else {
if (object instanceof InputStream) {
hashMap.put(columnName, new JDBCWrapper.InputStreamWrap((InputStream) object, columnName));
} else if (object instanceof Array) {
hashMap.put(columnName, object);
} else {
hashMap.put(columnName, String.valueOf(object == null ? "" : object));
}

}
}
if (!onlyOneColumn) {
list.add(hashMap);//否则返回一个字符串list
}


}

除此之外,注册输出函数第一个index=1是返回值,也是从1开始的,就问奇葩不奇葩。

标签:index,jdbc,String,columnName,坑爹,object,resultSet,column,SQLException
From: https://blog.51cto.com/u_15458814/5883445

相关文章

  • jmeter jdbc MySQL压测
    一、添加MySQL驱动下载MySQL驱动,安装  然后再测试计划添加MySQL驱动jar包    二、添加jdbc配置       三、添加jdbc请求     ......
  • 使用JDBCTemplate时idea的字符集和数据库的字符集都是utf-8但是在数据中中文变为了?
     修改过程1:在jdbc的配置文件中连接的后面手动设置字符集结果:       ......
  • JDBC
    获取插入主键publicintregister(Stringusername,Stringpassword,Stringemail)throwsException{Connectionconnection=DbUtil.getCon();Stringsql......
  • nexus-maven-repository-index.zip手动下载与设置
    问题描述:在启动eclipse的时候,在maven控制台经常会看到更新nexus-maven-repository-index.zip,用eclipse更新速度会很慢,甚至有不能完成下载的情况;问题解决:1.在你的eclipse......
  • 用JDBC操纵BLOB和CLOB数据
    在访问Oracle数据库,对Oracle的BLOB和CLOB进行操作的时候,当通过OracleJDBCDriver来调用的时,如下所例:DrivermyDriver=(Driver)Class.forName("oracle.jdbc.driv......
  • mysql中eq_range_index_dive_limit参数学习
    ​概念官方文档如下描述:Thisvariableindicatesthenumberofequalityrangesinanequalitycomparisonconditionwhentheoptimizershouldswitchfromusingind......
  • JDBC知识
    1jdbc是什么?JavaDateBaseconnectivity,java数据库连接,java语言连接数据库JDBC本质:sun公司定义了一套操作所有关系型数据库的规则(接口),各个数据库厂商去实现这套数据库,提......
  • z-index不起作用的原因(相对网上答案有补充)
    网上答案大部分答案是:z-index标签没有使用position进行定位(除static),同时父级任意标签设为position:relative定位;z-index标签使用了浮动,去除浮动,转而使用position定位;......
  • Java_JDBC
    JDBC一、JDBC简介1、概念:​ JDBC就是使用Java语言操作关系型数据库的一套API​ 全称:(JavaDataBaseConnectivity)Java数据库连接2、本质:为了使得Java代码可以......
  • Boat Attack ArgumentOutOfRangeException: Index was out of range
    BoatAttackArgumentOutOfRangeException:Indexwasoutofrange问题是因为默认情况下,buidsetting中的两个项目没有勾选,File>BuildSetting全部勾选项目再次运行......