美文网首页
坑爹奇葩的jdbc getColumnName(index)竟然

坑爹奇葩的jdbc getColumnName(index)竟然

作者: 吉凶以情迁 | 来源:发表于2021-12-13 16:11 被阅读0次
        /**
         * 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开始的,就问奇葩不奇葩。

    相关文章

      网友评论

          本文标题:坑爹奇葩的jdbc getColumnName(index)竟然

          本文链接:https://www.haomeiwen.com/subject/fyojfrtx.html