数据库元数据获取列详细信息

发布于 2024-10-01 09:05:16 字数 690 浏览 3 评论 0原文

我使用数据库元数据来查找列大小。但 getColumns(null,null,"table_name",null) 返回一个空结果集。我通过查询来检查该表,并且该表存在。错误在哪里?提前致谢!

更新:

Connection connection = getConnection(); //getting the connection -   
Statement st = connection.createStatement();  
ResultSet rs = st.executeQuery("select * from table_name");  
while (rs.next()) { 
   System.out.println(rs.getString("column_name")); //The values get printed 
}
ResultSet rsColumns = null;
DatabaseMetaData meta = connection.getMetaData();
rsColumns = meta.getColumns(null, null, "table_name",null);     
System.out.println(rsColumns.next()); // I get false here which means resultset is empty. But I dunno why.

I use databasemetadata to find the column size. But getColumns(null,null,"table_name",null) returns an empty resultset. I checked for the table by querying it and the table is present. Where is the error? Thanks in advance!

Update:

Connection connection = getConnection(); //getting the connection -   
Statement st = connection.createStatement();  
ResultSet rs = st.executeQuery("select * from table_name");  
while (rs.next()) { 
   System.out.println(rs.getString("column_name")); //The values get printed 
}
ResultSet rsColumns = null;
DatabaseMetaData meta = connection.getMetaData();
rsColumns = meta.getColumns(null, null, "table_name",null);     
System.out.println(rsColumns.next()); // I get false here which means resultset is empty. But I dunno why.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话已封尘 2024-10-08 09:05:16

您使用哪个数据库?这可能与它如何处理不带引号的标识符有关,遵循标准的数据库会自动将它们存储为大写。您可以通过调用 storesUpperCaseIdentifiers 方法以编程方式检查它是否执行此操作。

解决方案是在创建表时使用带引号的标识符,或者如果 storesUpperCaseIdentifiers 返回 true,则对表名称字符串调用 toUpperCase 方法。

Which database are you using? It's probably related to how it handle unquoted identifiers, databases that follow the standard automatically store them in upper case. You can programmatically check if it does that by calling the storesUpperCaseIdentifiers method.

The solution would be to either use quoted identifiers while creating the table or calling the toUpperCase method on the table name string if storesUpperCaseIdentifiers returns true.

网白 2024-10-08 09:05:16

您可以尝试以下代码片段吗:

ResultSet rsColumns = null;
DatabaseMetaData meta = connection.getMetaData();
rsColumns = meta.getColumns(null, null, "table_name",null);     
While (rsColumns,next()) {
System.out.println(rsColumns.getInt("COLUMN_SIZE"));
}

我认为您缺少“COLUMN_SIZE”。

Can you try the below snippet:

ResultSet rsColumns = null;
DatabaseMetaData meta = connection.getMetaData();
rsColumns = meta.getColumns(null, null, "table_name",null);     
While (rsColumns,next()) {
System.out.println(rsColumns.getInt("COLUMN_SIZE"));
}

I think you are missing the "COLUMN_SIZE".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文