- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 PostgreSQL 表
create table test (
id int,
name varchar(2),
active boolean,
long_id bigint,
created timestamptz);
)
我想获取此表中每列的名称、类型和大小。这是我的代码:
public static void main(final String[] args) throws SQLException {
final Connection connection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/test", "postgres", "123");
final DatabaseMetaData metaData = connection.getMetaData();
try (final ResultSet resultSet = metaData.getColumns("public", null, "test", null)) {
while (resultSet.next()) {
System.out.println(resultSet.getString("COLUMN_NAME") + " " + resultSet.getInt("DATA_TYPE") + " " + resultSet.getInt("COLUMN_SIZE"));
}
}
}
这是输出
id 4 10
name 12 2
active -7 1
long_id -5 19
created 93 35
第三列显示列大小,例如对于 boolean 值active,它是 1 个字节。但是为什么 id 值(在 PostgreSQL 中是 int)显示 10 个字节而不是 4 个字节?
最佳答案
如 DatabaseMetaData.getColumns
中所述(强调我的):
The COLUMN_SIZE column specifies the column size for the given column. For numeric data, this is the maximum precision. For character data, this is the length in characters. For datetime datatypes, this is the length in characters of the String representation (assuming the maximum allowed precision of the fractional seconds component). For binary data, this is the length in bytes. For the ROWID datatype, this is the length in bytes. Null is returned for data types where the column size is not applicable.
32 位整数的最大精度为 10,64 位整数(长整型)的最大精度为 19。
换句话说,您假设 COLUMN_SIZE
包含以字节为单位的大小是错误的。 getColumns
结果不提供该信息,CHAR_OCTET_LENGTH
列中的字符类型除外。
COLUMN_SIZE 的定义源自 X/Open SQL CLI,其修改后的形式存在于调用级接口(interface) (SQL/CLI) 标准 (ISO-9075-3) 中,具体来自 DescribeCol
函数(引用自 ISO-9075-3:2003 草案):
b) Case:
i) If the data type of C is character string, then ColumnSize is set to the maximum length in octets of C.
ii) If the data type of C is exact numeric or approximate numeric, then ColumnSize is set to the maximum length of C in decimal digits.
iii) If the data type of C is datetime or interval, then ColumnSize is set to the length in positions of C.
iv) If the data type of C is a reference type, then ColumnSize is set to the length in octets of that reference type.
v) Otherwise, ColumnSize is set to an implementation-dependent value.
关于java - DatabaseMetaData.getColumns 对于 int 值有奇怪的列大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60131568/
我用java对表中的数据进行了排序。使用 getColumnClass 和不使用 getColumnClass 排序都已完成。他们之间有什么区别?谢谢大家 最佳答案 对于 TableModel 的所有
我在 Oracle 数据库中有一些用户,假设 UserOne、UserTwo 和 UserTree 具有相同且非空的表,名称为“tableExample”。 就我而言,我需要通过 getColumns
元数据上的方法 getColumns() 为同义词返回一个空的结果集(对于表和 View ,它正确地返回列列表)。 这发生在 Oracle 11g Express 上并使用最新的 Oracle JDB
我使用的是 Slickgrid 2.0a1 版本,我需要获取一些列宽度,以便在其顶部显示一些框(框和列需要具有相同的宽度)。 问题是,当我使用 grid.getColumn(id).width 时,它
我需要从下面的代码返回一个可滚动的结果集: ResultSet columnMetaData = connection.getMetaData().getColumns(null, null, "my
我遇到了 getColumns 错误的问题,我自己根本无法弄清楚。我在网上搜索过但没有运气。我希望你们中的一些人对此有一个很好的答案!因为我被困住了... 我创建了一个创建成员场景,我想在创建后将信息
我打算使用 TableModelListener 对 JTable 的用户条目使用react。我想知道编辑单元格的列。不幸的是,方法 getColumn() 返回 -1 而不是编辑后的列号。知道为什么
我使用 AbstractTableModel 创建了一个自定义 TableModel。我能够填充我的 JTable。但是我的 JTable 有一个按钮列说“Button1”。所以我使用 CellRen
我使用 oracle 11g,并创建了一个这样的表: create table test1( id int, name varchar(10), inserttime date ) 然后我用jdbc的
我有一个数据库类,它封装了 java.sql.* 类的功能。基本上,您将其子类化以创建与特定数据库的连接,然后您可以使用其方法执行常见操作。 我想实现的一个操作是 getColumn() 方法。如果预
我正忙于编写一段代码以从 Oracle 数据库中获取表的所有列名。我想出的代码如下所示: DriverManager.registerDriver (new oracle.jdbc.driver.Or
我有一个 PostgreSQL 表 create table test ( id int, name varchar(2), active boolean, long_id bigint, creat
我有一个 JTextArea 我想根据列数和行数显示特定的文本行数组;但是 getColumns 和 getRows 返回零。以下内容应该让我了解我要完成的工作: public class Frame
“getColumns”到底应该返回什么?如果我使用抽象接口(interface)检索表,那么获取列名的唯一方法似乎是使用我的 IDE 所说的已弃用的元数据类。然而,尽管进行了几个小时的搜索,我似乎找
本文整理了Java中com.ctc.wstx.sw.XmlWriter.getColumn()方法的一些代码示例,展示了XmlWriter.getColumn()的具体用法。这些代码示例主要来源于Gi
我想要一个函数,我在其中传递一个引用的单元格,而不是一个范围,它返回该单元格的列号。 function lastRealDate(v) { var thisColumn = v.getColumn
我正在尝试提取 JDBC 数据库驱动程序的数据库元数据。 现在我面临着文档的一部分,我无法向自己解释: 方法 DatabaseMetaData.getColumns(...); ( JavaDoc )
本文整理了Java中org.apache.poi.xssf.usermodel.XSSFComment.getColumn()方法的一些代码示例,展示了XSSFComment.getColumn()的
我已经安装了MariaDB,并且在尝试创建这个简单的表时发现了一些困难: CREATE TABLE testdb.ACCOMODATION ( Id BIGINT NO
我是一名优秀的程序员,十分优秀!