java插入postgresql问题(时间格式问题)

java插入postgresql问题:
ERROR: column is of type timestamp without time zone but expression is of type character varying
建议:You will need to rewrite or cast the expression.

解决:
jdbc:postgresql://127.0.0.1:5432/testdb?stringtype=unspecified
后面添加stringtype=unspecified,即自动格式化数据
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用JDBC驱动程序来存取PostgreSQL中的二进制类型bytea。下面是一个简单的示例代码: ```java import java.sql.*; public class BinaryDataExample { public static void main(String[] args) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { // Load the PostgreSQL JDBC driver Class.forName("org.postgresql.Driver"); // Connect to the database conn = DriverManager.getConnection("jdbc:postgresql://localhost/mydatabase", "myusername", "mypassword"); // Insert binary data into the database pstmt = conn.prepareStatement("INSERT INTO mytable (mybinarydata) VALUES (?)"); byte[] binaryData = {1, 2, 3, 4, 5}; ByteArrayInputStream bais = new ByteArrayInputStream(binaryData); pstmt.setBinaryStream(1, bais, binaryData.length); pstmt.executeUpdate(); // Retrieve binary data from the database pstmt = conn.prepareStatement("SELECT mybinarydata FROM mytable WHERE id = ?"); pstmt.setInt(1, 1); rs = pstmt.executeQuery(); if (rs.next()) { // Read binary data from the result set InputStream is = rs.getBinaryStream("mybinarydata"); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = is.read(buffer)) != -1) { // Process binary data } } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } if (pstmt != null) { pstmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } ``` 上面的代码演示了如何将一个字节数组插入PostgreSQL数据库中的bytea字段中,并从数据库中检索出它。在插入时,我们使用了PreparedStatement的setBinaryStream方法,该方法允许我们将一个字节数组作为InputStream插入到数据库中。在检索时,我们使用了ResultSet的getBinaryStream方法,该方法返回一个InputStream对象,我们可以使用它来读取存储在数据库中的二进制数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值