English 中文(简体)
插入并恢复 BLOB 值 SubType = Text Firebird Java 文本
原标题:Insert and restore BLOB values SubType = Text Field Firebird Java

I m building a project, and I came across a problem with a Blob field I created in the database Firebird. This field would be referring to a field observations, which I do not want to limit the amount of text which the user will enter.

但是,我有一个问题,不知道我如何保存和阅读这个领域。

I m using JDBC to use the insert prepareStatement stmt stmt.set ... - For the blob do not know how to do and also do not know how to convert the String value for the field

问题回答

您可以使用 prepared statement. set string () 和 ResultSet.get String () , 如果您使用 BLOBSub_TYPE 1 (aka BLOBSub_TYPE EXT ), 则Firebird驱动程序会将它转换为/ 换成 blob 字符集。 您需要确保您的连接字符集与 blob 字符集相同, 否则您可以得到错误的字符集转换 。

其他选项是明确创建 Clob (使用 connection.capeClob () ), 并在语句上设置此功能, 或者使用 setCharacterStream 方法 。

为了将字符串转换为blob,我找到了"http://studyjava.org/client-side-scripts/convert-string-to-blob-and-blob-to-string" rel=“nofollow” >这个例子 :

//Convert String to Blob
String data = “hello world”;
java.sql.Blob blob = org.hibernate.Hibernate.createBlob(data.getBytes());

//Convert Blob to String
byte[] bdata = blob.getBytes(1, (int)blob.length());
String data1 = new String(bdata);

查看 < a href=> " "http://www.java2s.com/Code/Java/Database-SQL-JDBC/DemoPrepared StatementSetBlob.htm" rel=“nofolpt” > 这个例子 setBlob 准备的语句。这里是一块。从他们的示例来看,您可以在 setBlob 上调用 prepared Statement

java.sql.Blob blob = null;
try {
  conn = getConnection();
  // prepare blob object from an existing binary column
  pstmt = conn.prepareStatement("select photo from my_pictures where id = ?");
  pstmt.setString(1, "0001");
  rs = pstmt.executeQuery();
  rs.next();
  blob = rs.getBlob(1);

  // prepare SQL query for inserting a new row using setBlob()
  String query = "insert into blob_table(id, blob_column) values(?, ?)";
  // begin transaction
  conn.setAutoCommit(false);

  pstmt = conn.prepareStatement(query);
  pstmt.setString(1, "0002");
  pstmt.setBlob(2, blob);

  int rowCount = pstmt.executeUpdate();
  System.out.println("rowCount=" + rowCount);
  // end transaction
  conn.commit();




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...