首页 > 解决方案 > 如果有多行可用,如何按列标题区分行名

问题描述

如果有多行可用,任何人都可以建议如何按列标题区分行名。:

** - 桌子 **

STD    ABC   SSS   AAA
-----------------------
123    222   333   123
-----------------------
222    132   234   543

我有下面的代码打印列名及其值,如果有多行,它会再次打印相同的列名及其值。

STD-123
ABC-222
SSS-333
AAA-123
STD-222
ABC-132
SSS-234
AAA-543

但是我在这里看是否有多个我们可以打印如下:

STD-123
ABC-222
SSS-333
AAA-123
STD1-222
ABC1-132
SSS1-234
AAA1-543 if there are more row it should print like

STD2-123
ABC2-222
SSS2-333
AAA2-123

代码

import java.io.*;  
import java.sql.*;  

public class RetrieveFile {  
    public static void main(String args[]) throws Exception {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@123.32.23.12:8080/orcl", "test", "******");

            PreparedStatement ps = con.prepareStatement("select ABS from MSG where MID='1123'");
            ResultSet rs = ps.executeQuery();

            try {
                printResultColumns(rs);
            } catch (SQLException e) {
                System.err.println(e.getMessage());
            }

            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void printResultColumns(ResultSet resultSet) throws SQLException, IOException {

        ResultSetMetaData rsmd = resultSet.getMetaData();
        int columnCount = rsmd.getColumnCount();
        int rowCount = 0;
        while (resultSet.next()) {
            // you get a single result row in here, not the entire ResultSet
            for (int i = 1; i <= columnCount; i++) {
                int type = rsmd.getColumnType(i);
                String typeName = rsmd.getColumnTypeName(i);
                String name = rsmd.getColumnName(i);
                String value;
                switch (type) {
                case Types.VARCHAR:
                    value = resultSet.getString(i) == null ? "null" : resultSet.getString(i);
                    //System.out.println(name + ": " + value);
                    System.out.println(name + (rowCount == 0 ? "" : rowCount) + ": " + value);
                    break;
                case Types.DOUBLE:
                    value = resultSet.getString(i) == null ? "null" : String.valueOf(resultSet.getDouble(i));
                    //System.out.println(name + " [" + typeName + "]: " + value);
                    System.out.println(name + (rowCount == 0 ? "" : rowCount) + ": " + value);
                    break;
                case Types.INTEGER:
                    value = resultSet.getString(i) == null ? "null" : String.valueOf(resultSet.getInt(i));
                    //System.out.println(name + " [" + typeName + "]: " + value);
                    System.out.println(name + (rowCount == 0 ? "" : rowCount) + ": " + value);
                    break;
                case Types.DATE:
                    value = resultSet.getString(i) == null ? "null" : String.valueOf(resultSet.getDate(i).toString());
                   // System.out.println(name + " [" + typeName + "]: " + value);
                    System.out.println(name + (rowCount == 0 ? "" : rowCount) + ": " + value);
                    break;
                case Types.TIMESTAMP:
                    value = resultSet.getString(i) == null ? "null" : resultSet.getTimestamp(i).toString();
                   // System.out.println(name + " [" + typeName + "]: " + value);
                    System.out.println(name + (rowCount == 0 ? "" : rowCount) + ": " + value);
                    break;
                case Types.BOOLEAN:
                    value = resultSet.getString(i) == null ? "null" : (resultSet.getBoolean(i) ? "true" : "false");
                    //System.out.println(name + " [" + typeName + "]: " + value);
                    System.out.println(name + (rowCount == 0 ? "" : rowCount) + ": " + value);
                    break;
                case Types.DECIMAL:
                case Types.NUMERIC:
                    value = resultSet.getString(i) == null ? "null" : resultSet.getBigDecimal(i).toString();
                    //System.out.println(name + " [" + typeName + "]: " + value);
                    System.out.println(name + (rowCount == 0 ? "" : rowCount) + ": " + value);
                    break;
                case Types.LONGVARCHAR:
                     break;
                case Types.CHAR:                    
                    value = resultSet.getString(i) == null ? "null" : resultSet.getString(i);
                    //System.out.println(name + ": " + value);
                    System.out.println(name + (rowCount == 0 ? "" : rowCount) + ": " + value);
                    break;
                default:
                     rowCount++;
                    System.out.println(rsmd.getColumnTypeName(i) + rsmd.getColumnName(i) +rsmd.getColumnLabel(i));
                    System.out.println(rowCount == 0 ? "" : rowCount);

                }

            }
        }
    }
    }  

标签: javajdbc

解决方案


您可以定义一个变量,在循环resultSet之前计算 (0 based)的行数:while

int rowCount = 0;

然后在 switch 内部,对于每种情况,您必须在 .rowCount之后打印name
例如,而不是:

System.out.println(name + ": " + value);

做这个:

System.out.println(name + (rowCount == 0 ? "" : rowCount) + ": " + value);

这将rowCount在您的输出中插入数字值(仅当它大于 0 时)。
所以你需要打印的是:

rowCount == 0 ? "" : rowCount

作为while循环增量的最后一行rowCount

rowCount++;

像这样:

int rowCount = 0;
while (resultSet.next()) {
    // you get a single result row in here, not the entire ResultSet
    for (int i = 1; i <= columnCount; i++) {
        ...........................
        switch (type) {
            ..................
        }

    }
    rowCount++;
}

推荐阅读