解决方案
以下示例使用 getColumnCount、getColumnName、getColumnTypeName、getColumnDisplaySize 方法获取表中的列数、列名或列类型。
import java.sql.*;
public class jdbcConn {
public static void main(String[] args) throws Exception {
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con = DriverManager.getConnection(
"jdbc:derby://localhost:1527/testDb","name","pass");
Statement stmt = con.createStatement();
String query = "select * from emp order by name";
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
System.out.println("no of columns in the table = "+ rsmd.getColumnCount());
System.out.println("Name of the first column "+ rsmd.getColumnName(1));
System.out.println("Type of the second column "+ rsmd.getColumnTypeName(2));
System.out.println("No of characters in 3rd column "+ rsmd.getColumnDisplaySize(2));
}
}