java - 如果有多行可用,如何按列标题区分行名
问题描述
如果有多行可用,任何人都可以建议如何按列标题区分行名。:
** - 桌子 **
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);
}
}
}
}
}
解决方案
您可以定义一个变量,在循环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++;
}
推荐阅读
- java - 检查 JPanel 或 JFrame 的类/对象扩展是否使用 Reflect 或 instanceOf
- html - 放一个
- python - 将上下文管理器与 X() 一起用作 foo 然后在使用后检查来自 foo 的信息的模式是什么?
- javascript - HTML5 画布设置顶部/左侧
- azure - Linux 容器 Azure 应用服务部署的 Azure 发布失败
- python - 在 python3 中导入 keras 时出现 numpy.core.umath 错误
- ruby - 如何使用 RSpec 检测带有块的方法调用
- rxjs - Apollo Server:从 Observable 创建订阅解析器
- python - 如何在回调函数中发送 next()
- payment-gateway - OMNIPAY 中的 NMI 网关