我试图从DB2表中提取数据。我想知道是否有一种方法可以从ResultSet打印整个记录,而无需指定单个列名。
我的代码:
import java.sql.*;
public class Ftp {
public static void main(String[] args) {
String url = "jdbc:db2://mvshost:4456/SQA_SYSTEM";
String user = "xxxxx";
String password = "yyyyyy";
Connection con;
PreparedStatement stmt;
ResultSet rs;
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
con = DriverManager.getConnection(url, user, password);
con.setAutoCommit(false);
stmt = con.prepareStatement("SELECT EMP FROM IDATDMK.GREEN");
rs = stmt.executeQuery();
while (rs.next()) {
String EMP = rs.getString("EMP");
System.out.println("EMP from DB2 = " + EMP);
}
rs.close();
stmt.close();
con.commit();
con.close();
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
}
如果我将查询编写为Select * from IDATDMK.GREEN
,如何在while循环中打印整个记录而不指定单个列名。我在表格中有80多列。
答案 0 :(得分:0)
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
while(rs.next(){
for (int i = 1; i <= columnsNumber; i++) {
if (i > 1) System.out.print("\n");
String columnValue = rs.getString(i);
System.out.print(columnValue) }}