简书链接:坑爹奇葩的jdbcgetColumnNameindex竟然从1开始
文章字数:205,阅读全文大约需要1分钟
我做了一个jdbc底层项目,jdbc底层实际上是http请求。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* 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;

实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

/**
* 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模式的时候出错了,坑爹,坑爹,必须记录下来,搞得我改过来改过去

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

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开始的,就问奇葩不奇葩。