Java 如何从 JDBC ResultSet 中获取列数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2614416/
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
How to get the number of columns from a JDBC ResultSet?
提问by Jonas
I am using CsvJdbc(it is a JDBC-driver for csv-files) to access a csv-file. I don't know how many columns the csv-file contains. How can I get the number of columns? Is there any JDBC-function for this? I can not find any methods for this in java.sql.ResultSet.
我正在使用CsvJdbc(它是 csv 文件的 JDBC 驱动程序)来访问 csv 文件。我不知道 csv 文件包含多少列。我怎样才能得到列数?是否有任何 JDBC 功能?我在 java.sql.ResultSet 中找不到任何方法。
For accessing the file, I use code similar to the exampleon the CsvJdbc website.
为了访问该文件,我使用了类似于CsvJdbc 网站上的示例的代码。
采纳答案by Roman
You can get columns number from ResultSetMetaData:
您可以从ResultSetMetaData获取列号:
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
回答by lokesh
PreparedStatement ps=con.prepareStatement("select * from stud");
ResultSet rs=ps.executeQuery();
ResultSetMetaData rsmd=rs.getMetaData();
System.out.println("columns: "+rsmd.getColumnCount());
System.out.println("Column Name of 1st column: "+rsmd.getColumnName(1));
System.out.println("Column Type Name of 1st column: "+rsmd.getColumnTypeName(1));
回答by 1ac0
Number of a columns in the result setyou can get with code (as DB is used PostgreSQL):
您可以使用代码获得的结果集中的列数(因为 DB 使用的是 PostgreSQL):
//load the driver for PostgreSQL Class.forName("org.postgresql.Driver"); String url = "jdbc:postgresql://localhost/test"; Properties props = new Properties(); props.setProperty("user","mydbuser"); props.setProperty("password","mydbpass"); Connection conn = DriverManager.getConnection(url, props); //create statement Statement stat = conn.createStatement(); //obtain a result set ResultSet rs = stat.executeQuery("SELECT c1, c2, c3, c4, c5 FROM MY_TABLE"); //from result set give metadata ResultSetMetaData rsmd = rs.getMetaData(); //columns count from metadata object int numOfCols = rsmd.getColumnCount();
But you can get more meta-informations about columns:
但是您可以获得有关列的更多元信息:
for(int i = 1; i <= numOfCols; i++)
{
System.out.println(rsmd.getColumnName(i));
}
And at least but not least, you can get some info not just about table but about DB too, how to do it you can find hereand here.
回答by Prabodh Hend
After establising the connection and executing the query try this:
在建立连接并执行查询后,试试这个:
ResultSet resultSet;
int columnCount = resultSet.getMetaData().getColumnCount();
System.out.println("column count : "+columnCount);
回答by nitish shah
This will print the data in columns and comes to new line once last column is reached.
这将在列中打印数据,并在到达最后一列后换行。
ResultSetMetaData resultSetMetaData = res.getMetaData();
int columnCount = resultSetMetaData.getColumnCount();
for(int i =1; i<=columnCount; i++){
if(!(i==columnCount)){
System.out.print(res.getString(i)+"\t");
}
else{
System.out.println(res.getString(i));
}
}