字符串如何存到数据库blob字段
字符串如何存到数据库blob字段
字符串如何存到数据库blob字段?
我将字符串存放到blob字段出现乱码,请问如何解决?是否需要将字符串转换为二进制再存储,如何转换呢?
字符串如何存到数据库blob字段?
String.getBytes() 可以吗?
我们原来是做成fileinputstream然后set进去。
字符串如何存到数据库blob字段?
用String.getByte()写进去的也是乱码!
这个是mysql下存取blob字段的一个很简单的类,跟据自己的需要改改就行了
//存取二进制文件到BLOB字段的函数import oracle.sql.BLOB;import oracle.jdbc.driver.OracleResultSet;public void updateBlob(Connection conn,String strFile,long newId,String table_name) { try { conn.setAutoCommit(false); Statement stmt = conn.createStatement(); //stmt.execute("insert into A(A,B) values ('"+strId+"',empty_blob())"); ResultSet rset = stmt.executeQuery("select t.content from "+table_name +" t where id=" + newId + " FOR UPDATE"); BLOB blob = null; while (rset.next()) { blob = ( (OracleResultSet) rset).getBLOB(1); } File binaryFile = new File(strFile); FileInputStream instream = new FileInputStream(binaryFile); //读入二进制文件 OutputStream outstream = blob.getBinaryOutputStream(); byte[] buffer = new byte[32]; int length = -1; while ( (length = instream.read(buffer)) != -1) outstream.write(buffer, 0, length); instream.close(); outstream.close(); conn.commit(); conn.setAutoCommit(true); stmt.close(); conn.close(); } catch(Exception e){ e.printStackTrace(); }}