获取列的名称 (PostgreSQL)
如何从 PostgreSQL 的表中获取第一列的名称?
我知道如何获得所有这些,但如何将第一个与其他分开?
public void getColumns(String username, String password, String database, String table){
try {
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost:5432/"+database;
connection = DriverManager.getConnection(url, username, password);
// Gets the metadata of the database
DatabaseMetaData dbmd = connection.getMetaData();
ResultSet rs = dbmd.getColumns(null, null, table, null);
while (rs.next()) {
colummName = rs.getString("COLUMN_NAME");
System.out.println(colummName);
}
} catch (SQLException e) {
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if(connection!=null){
} else {
window.showNotification("Cannot connect to a Database",Notification.TYPE_WARNING_MESSAGE);
}
}
How can I get the name of the first column out of a table in PostgreSQL?
I know how to get them all but how do I separate the first one from the others ?
public void getColumns(String username, String password, String database, String table){
try {
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost:5432/"+database;
connection = DriverManager.getConnection(url, username, password);
// Gets the metadata of the database
DatabaseMetaData dbmd = connection.getMetaData();
ResultSet rs = dbmd.getColumns(null, null, table, null);
while (rs.next()) {
colummName = rs.getString("COLUMN_NAME");
System.out.println(colummName);
}
} catch (SQLException e) {
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if(connection!=null){
} else {
window.showNotification("Cannot connect to a Database",Notification.TYPE_WARNING_MESSAGE);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有点难以理解您要问的是什么,但我认为您需要给定索引中的列名称(在本例中,您需要第一列的名称)。
使用 ResultSetMetaData:
注意列索引为一从零开始。所以第一列是 1,第二列是 2 等等
Little bit hard to understand what it is you're asking, but I think you want the column name from a given index (in this case, you want the first column's name).
Use ResultSetMetaData:
Note the column indexes are one-based, not zero-based. So first column is 1, second is 2 etc