postgresql java 类型_哪些PostgreSQL列类型应用于存储Java BigDecimal?

请参阅

PostgreSQL datatypes – 也许

Numeric,它可以作为任意精度类型(这是一个Postgresql扩展).

@H_404_5@

@H_404_5@

…without any precision or scale creates a column in which numeric values of any precision and scale can be stored,up to the implementation limit on precision.

我不完全确定“精确度的实施限制”是什么.从来没有尝试过大量的数字.如果达到极限,那么可以回到文本.但是,我怀疑在此之前会有重大的其他问题;-)如果您希望存储少一些,则将精度和/或比例指定为数字.

编辑为sjr指出,限制是精确的1000位十进制数(来自同一链接):

@H_404_5@

@H_404_5@

The type numeric can store numbers with up to 1000 digits of precision [in currentl implementations] and perform calculations exactly. It is especially recommended for storing monetary amounts and other quantities where exactness is required…

如果需要更多的精度 – 尽管手头有更大的问题 – 然后数字列将不适合(本身).但这真的是一个非常极端的“如果”,可能不会发挥限制作用.

Java中,可以使用JDBC驱动程序来连接和操作PostgreSQL数据库。要存PostgreSQL二进制类型bytea,可以使用PreparedStatement对象的setBytes()方法来设置二进制数据,并使用ResultSet对象的getBytes()方法来获二进制数据。 以下是一个示例代码,演示了如何存bytea类型的数据: ```java import java.sql.*; public class PostgresBinaryExample { public static void main(String[] args) throws SQLException { //连接PostgreSQL数据库 String url = "jdbc:postgresql://localhost/test"; String user = "postgres"; String password = "password"; Connection conn = DriverManager.getConnection(url, user, password); //创建表 Statement stmt = conn.createStatement(); String createTable = "CREATE TABLE binary_data (id SERIAL PRIMARY KEY, data BYTEA)"; stmt.executeUpdate(createTable); //插入二进制数据 byte[] binaryData = {0x01, 0x02, 0x03}; PreparedStatement pstmt = conn.prepareStatement("INSERT INTO binary_data (data) VALUES (?)"); pstmt.setBytes(1, binaryData); pstmt.executeUpdate(); //查询二进制数据 ResultSet rs = stmt.executeQuery("SELECT data FROM binary_data"); if (rs.next()) { byte[] retrievedData = rs.getBytes("data"); System.out.println("Retrieved data: " + Arrays.toString(retrievedData)); } //清理资源 rs.close(); pstmt.close(); stmt.executeUpdate("DROP TABLE binary_data"); stmt.close(); conn.close(); } } ``` 在上面的代码中,我们首先连接PostgreSQL数据库,然后创建一个名为binary_data的表,其中包含一个名为data的bytea。接着,我们插入一个包含三个字节的二进制数据。最后,我们查询该数据并输出到控制台,最后清理资源。 注意,上述代码只是一个示例,实际用中需要更多的错误处理和异常处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值