在java中打印“Select *”查询中的所有列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23291346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing all columns from "Select *" query in java
提问by Mohsenuss91
I want to know how to print "Select *" query in java ? I tried this function but it give me this message "invalid column name "after execution.
我想知道如何在 java 中打印“Select *”查询?我试过这个函数,但它在执行后给我这条消息“无效的列名”。
public static void ask() throws Exception {
try {
java.sql.Statement s= conn.createStatement();
ResultSet rs = s.executeQuery("SELECT * FROM DB1.T1");
System.out.println("**************Resultats**************");
while ( rs.next() ) {
String all = rs.getString("*");
System.out.println(all);
}
conn.close();
} catch (Exception e) {
System.err.println("exception! ");
System.err.println(e.getMessage());
}
}
采纳答案by Rahul Tripathi
This line is the problem:
这一行是问题:
String all = rs.getString("*");
you have to provide the column name in the getString()method. Presently it assumes *
as a column name which is not correct and hence shows an error.
您必须在getString()方法中提供列名。目前它假定*
为不正确的列名,因此显示错误。
You have to provide the column name in that like this:
您必须像这样提供列名:
String all = rs.getString("Column_Name");
EDIT:-
编辑:-
You may try this if you dont want to write the names of the columns
如果你不想写列的名称,你可以试试这个
ResultSetMetaData md = rs.getMetaData();
int colCount = md.getColumnCount();
for (int i = 1; i <= colCount ; i++){
String col_name = md.getColumnName(i);
System.out.println(col_name);
}
回答by Olesya Razuvayevskaya
You cannot specify *
to get the value. For every column your query returns and you wish to print out, specify either:
您不能指定*
获取值。对于您的查询返回并希望打印的每一列,请指定:
String all = rs.getString("your_column_name");
or:
或者:
String all = rs.getString(i);
where "i" is the order of the printed column in the output of your query
其中“i”是查询输出中打印列的顺序
回答by fluminis
The *
in your query means select all the columns of the table.
在*
您的查询手段选择表的所有列。
But after executing the query, your result will have all the name of the columns. So to access a result, you have to do:
但是在执行查询后,您的结果将包含所有列的名称。因此,要访问结果,您必须执行以下操作:
String a_column_value = rs.getString("a_column_name");
回答by A4L
You could use the following generic approach:
您可以使用以下通用方法:
c = getConnection();
st = c.createStatement();
rs = st.executeQuery("select * from MY_TABLE");
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
while(rs.next()) {
for(int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
Object object = rs.getObject(columnIndex);
System.out.printf("%s, ", object == null ? "NULL" : object.toString());
}
System.out.printf("%n");
}
It gets the Metadatafrom the the returned Resultset to determine the number of columns the result set contains and does a simple iteration. This works even if your SQL is select col1, col2 from MY_TABLE
. In such a case columnCount
would be 2
.
它从返回的结果集中获取元数据以确定结果集包含的列数并进行简单的迭代。即使您的 SQL 是select col1, col2 from MY_TABLE
. 在这种情况下columnCount
会2
。