Java 如何查询blob?

Java 如何查询blob?,java,sql,jdbc,blob,Java,Sql,Jdbc,Blob,我想创建返回一些值的SQL查询,其中一个值是blob 当我将blob列放入select时,它返回BigDecimal:| 它的普通JDBC查询带有参数插入 stmt = con.prepareStatement("INSERT INTO TABLE(fileName, " + "blobData) VALUES(?, ?)"); stmt.setString(1, "somefilename"); stmt.setObject(2, dat

我想创建返回一些值的SQL查询,其中一个值是blob

当我将blob列放入select时,它返回BigDecimal:|

它的普通JDBC查询带有参数插入

stmt = con.prepareStatement("INSERT INTO TABLE(fileName, "
            + "blobData) VALUES(?, ?)");
        stmt.setString(1, "somefilename");
        stmt.setObject(2, data);//data is byte[]
        stmt.executeUpdate();
选择

ResultSet rs;

    stmt = con.prepareStatement("SELECT blobData "
        + "FROM BlobTest " + "WHERE fileName = ?");

    stmt.setString(1, "somevalue");
    rs = stmt.executeQuery();
    if (!rs.next()) {
      System.out.println("No such file stored.");
    } else {
      Blob b = rs.getBlob(1);
      BufferedOutputStream os;
    • 插入

      stmt = con.prepareStatement("INSERT INTO TABLE(fileName, "
                  + "blobData) VALUES(?, ?)");
              stmt.setString(1, "somefilename");
              stmt.setObject(2, data);//data is byte[]
              stmt.executeUpdate();
      
      选择

      ResultSet rs;
      
          stmt = con.prepareStatement("SELECT blobData "
              + "FROM BlobTest " + "WHERE fileName = ?");
      
          stmt.setString(1, "somevalue");
          rs = stmt.executeQuery();
          if (!rs.next()) {
            System.out.println("No such file stored.");
          } else {
            Blob b = rs.getBlob(1);
            BufferedOutputStream os;
      

      尝试从结果集调用getBytes()方法:

      stmt = dbConn.prepareStatement("SELECT ...");
      ResultSet rs = stmt.executeQuery();
      byte[] blob = rs.getBytes(1);
      

      这将以字节数组的形式返回blob对象。

      尝试从结果集调用getBytes()方法:

      stmt = dbConn.prepareStatement("SELECT ...");
      ResultSet rs = stmt.executeQuery();
      byte[] blob = rs.getBytes(1);
      

      这将以字节数组的形式返回blob对象。

      请查看上的javadocs部分

      您需要在blob列上调用
      getBlob
      ,然后读取二进制流

          Blob blob = rs.getBlob("BLOB_COLUMN_NAME");        
          InputStream blobStream = blob.getBinaryStream();
      
          //e.g. save blob to a file
          FileOutputStream out = new FileOutputStream("file.txt");
          byte[] buffer = new byte[1024];
          int n = 0;  
          while( (n = blobStream.read(buffer)) != -1 ) {
              out.write(buffer, 0, n);
          }
          out.flush();
          out.close();
          blobStream.close();
      

      请看上的javadocs部分

      您需要在blob列上调用
      getBlob
      ,然后读取二进制流

          Blob blob = rs.getBlob("BLOB_COLUMN_NAME");        
          InputStream blobStream = blob.getBinaryStream();
      
          //e.g. save blob to a file
          FileOutputStream out = new FileOutputStream("file.txt");
          byte[] buffer = new byte[1024];
          int n = 0;  
          while( (n = blobStream.read(buffer)) != -1 ) {
              out.write(buffer, 0, n);
          }
          out.flush();
          out.close();
          blobStream.close();
      
      实际上,setBytes()(而不是setObject())可能更便于移植。但这在很大程度上取决于JDBC驱动器,因此setBytes()(而不是setObject())可能更具可移植性。但这在很大程度上取决于JDBC驱动程序