为了将字符串转换为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();