mysql 字符串存为blob

    xiaoxiao2022-06-24  34

    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,并使用指定的编码方式

    转载请注明原文地址: https://ju.6miu.com/read-1123691.html

    最新回复(0)