Java - 如何在结果集上获取列名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19094999/
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
Java - How to get Column name on Result Set
提问by JeraldPunx
Hello I'm trying to make an error when there is no matched student...
and it will display like this
No matching records found
and I want the column name still the same but still not figuring it out... can some one tell me if this is right??
你好,当没有匹配的学生时,我试图犯一个错误......它会像这样显示
No matching records found
,我希望列名仍然相同但仍然没有弄清楚......有人可以告诉我这是不是对??
Heres my function for that... and I add comment there where I put the error... but i don't know how to get the columnname
这是我的功能......我在那里添加了评论,我把错误......但我不知道如何获取列名
public void SearchTableStudent() {
String tempSearchValue = searchStudent.getText().trim();
boolean empty = true;
sql = "SELECT student_id as 'Student ID',"
+ "concat(lastname, ' , ', firstname, ' ', middlename) as 'Name'"
+ "FROM user "
+ "WHERE CAST(student_id as CHAR) LIKE '%" + tempSearchValue + "%'";
try {
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while(rs.next()) {
table.setModel(DbUtils.resultSetToTableModel(rs));
empty = false;
}
if(empty) {
String error = "";
table.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"No matching records found",null}
},
new String [] {
/** I WANT TO PUT THE SAME COLUMN NAME ON MY DATABASE SELECTED BUT DON't Know
WHAT FUNCTION TO DO*/
}
));
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
I try like this but still gave me NULL!!!
this code is below of empty = false;
我像这样尝试,但仍然给了我 NULL !!!此代码低于empty = false;
for(int i=0; i<table.getColumnCount(); i++) {
test[i] = table.getColumnName(i);
}
采纳答案by Prabhakaran Ramaswamy
ResultSetMetaData metaData = resultSet.getMetaData();
int count = metaData.getColumnCount(); //number of column
String columnName[] = new String[count];
for (int i = 1; i <= count; i++)
{
columnName[i-1] = metaData.getColumnLabel(i);
System.out.println(columnName[i-1]);
}
回答by anubhava
Get ResultSetMetaData
using ResultSet#getMetaData()
:
开始ResultSetMetaData
使用ResultSet#getMetaData()
:
ResultSetMetaData meta = rs.getMetaData();
And then to get column name of 1st column:
然后获取第一列的列名:
String col1Name = meta.getColumnLabel(1);
Similarly to get column name of 2nd column:
类似地获取第二列的列名:
String col2Name = meta.getColumnLabel(2);
回答by ppeterka
Get the metadata
获取元数据
ResultSetMetaData metaData = rs.getMetaData();
Then you can do:
然后你可以这样做:
String columnName = metaData.getColumnName(int index);
回答by Ashok kumar
Try this.
尝试这个。
ResultSetMetaData meta = resultset.getMetaData();
Integer columncount = meta.getColumnCount();
int count = 1 ; // start counting from 1 always
String[] columnNames = new String[columncount];
while(count<=columncount){
columnNames [count-1] = meta.getColumnLabel(count);
count++;
}
Since here your expecting is to get the columns alias instead of column name, so you have to use ResultSetMetaData.getColumnLabel instead of ResultSetmetaData.getColumnName.
由于这里您期望的是获取列别名而不是列名,因此您必须使用 ResultSetMetaData.getColumnLabel 而不是 ResultSetmetaData.getColumnName。
回答by krstf
rs.getMetaData().getColumnName(int i);
rs.getMetaData().getColumnName(int i);
and do not concat the query param!
并且不要连接查询参数!