mysql 字符串存为blob

来源:互联网 发布:unity3d ui动画 编辑:程序博客网 时间:2024/06/03 13:59
string存储为blob
引用:http://stackoverflow.com/questions/17400497/how-to-convert-blob-to-string-and-string-to-blob-in-java
PreparedStatement ps1 = conn.prepareStatement("update t1 set a2=? where id=1");Blob blob = conn.createBlob();blob.setBytes(1, str.getBytes());ps1.setBlob(1, blob);ps1.executeUpdate();



blob读取转码

String sqlString = "select CAST(" +  clobFieldName + " AS CHAR(10000) CHARACTER SET utf8) from " + tableName+" where " + pkName +" ='" + pkValue + "'";ps = conn.prepareStatement(sqlString);rs = ps.executeQuery();


blob流读取

String sqlString = "select " +  clobFieldName + " from " + tableName+" where " + pkName +" ='" + pkValue + "'";ps = conn.prepareStatement(sqlString);rs = ps.executeQuery();
Blob b = (Blob)rs.getBlob(1);<span style="white-space:pre"></span>InputStream is = b.getBinaryStream();ByteArrayInputStream bais = (ByteArrayInputStream)is;byte[] byte_data = new byte[bais.available()]; //bais.available()返回此输入流的字节数bais.read(byte_data, 0,byte_data.length);//将输入流中的内容读到指定的数组String s;try {s = new String(byte_data,"utf-8");} catch (UnsupportedEncodingException e) {<span style="white-space:pre"></span>// TODO Auto-generated catch block<span style="white-space:pre"></span>e.printStackTrace();} //再转为String,并使用指定的编码方式


0 0