如何将 Java 中的字节存储到 PostgreSQL 中的字节中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21285165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:54:03  来源:igfitidea点击:

How to store byte from Java into a bytea in PostgreSQL?

javapostgresqlbytesqlexceptionbytea

提问by renatogbp

I am getting a problem when I try to insert a variable of type "byte" in Java into a variable of type "bytea" in PostgreSQL.

当我尝试将 Java 中“byte”类型的变量插入 PostgreSQL 中“bytea”类型的变量时遇到问题。

This is my table in postgresql:

这是我在 postgresql 中的表:

CREATE TABLE mesa (
   id_mesa              serial PRIMARY KEY,
   tag                  varchar(50),
   modelo               varchar(50),
   menor_complemento    smallint NOT NULL,
   peso_min             smallint NOT NULL,
   peso_max             smallint NOT NULL,
   som                  bytea NOT NULL,
   rotina               bytea NOT NULL,
   address64bits        bytea NOT NULL UNIQUE
);

my code in Java is the following:

我的 Java 代码如下:

private Mesa mesa;
//construtor
public MesaDAO (Mesa mesa) {
    this.mesa = mesa;
}
(...)
String stm = "INSERT INTO mesa(tag, modelo, menor_complemento, peso_min, "
            + "peso_max, som, rotina, address64bits) "
            + "VALUES(?,?,?,?,?,?,?,?)";
    try {
        pst = con.prepareStatement(stm);
        pst.setString(1, mesa.getTag());
        pst.setString(2, mesa.getModelo());
        pst.setInt(3, mesa.getMenorComplemento());
        pst.setInt(4, mesa.getPesoMin());
        pst.setInt(5, mesa.getPesoMax());
        pst.setByte(6, mesa.getSom());
        pst.setByte(7, mesa.getRotina());
        pst.setBytes(8, mesa.getAddress64Bits());
        pst.executeUpdate();
        (...)

The mesa types are:

台面类型是:

public class Mesa{
  private Integer idMesa;
  private String tag;
  private String modelo;
  private Integer menorComplemento;
  private Integer pesoMin;
  private Integer pesoMax;
  private byte som;
  private byte rotina;
  private byte[] address64Bits;
  (...)
}

Then, when I try to insert something it throws a PSQLException stating that the column "som" is a "bytea" but the expression is a "smallint". I also think it gonna have the same SQLException on the following two lines after that one.

然后,当我尝试插入一些东西时,它会抛出一个 PSQLException ,指出列 "som" 是一个 "bytea" 但表达式是一个 "smallint"。我还认为在那一行之后的以下两行上会有相同的 SQLException 。

So I don't know how to fix that, if someone could help me I will be glad!

所以我不知道如何解决这个问题,如果有人能帮助我,我会很高兴!

Thank you in advance,

先感谢您,

EDIT:

编辑:

Thanks guys for helping me, I will provide the solution here: change the variable "som" and "rotina" to "byte[]" type and then use pst.getBytes(...) instead of pst.getByte(...).

感谢大家帮助我,我将在这里提供解决方案:将变量“som”和“rotina”更改为“byte[]”类型,然后使用 pst.getBytes(...) 而不是 pst.getByte(... )。

Clarifying:

澄清:

I am using XBee (API mode), so I need to send the byte value through the port serial. So I was just verifying if what I am getting from the database is the same value of that one I inserted there. In conclusion, I want to know how to recover the bytes from database to send them through the serial port.

我正在使用 XBee(API 模式),所以我需要通过端口串行发送字节值。所以我只是验证我从数据库中得到的值是否与我在那里插入的值相同。总之,我想知道如何从数据库中恢复字节以通过串口发送它们。

回答by Gilberto

Code Java by PostgreSQL

用 PostgreSQL 编写 Java

pst.setBinaryStream(6, mesa.getSom());
pst.setBinaryStream(7, mesa.getRotina());
pst.setBinaryStream(8, mesa.getAddress64Bits());`