javaString与Blob类型数据转换
需要把String类型数据转换成Reader,然后再使⽤setCharacterStream插⼊数据库中。
例如下例中,要插⼊String longStr,则先转换成Byte[],再ByteArrayInputStream,最后InputStreamReader。
添加或更新clob型数据,如下所⽰(以更新为例):
PreparedStatement pstmt=conn.prepareStatement(“update tablename set column1=? “+条件语句);
byte[] bytes_zyjs = Bytes();
ByteArrayInputStream baisss = new ByteArrayInputStrea(bytes_zyjs);
InputStreamReader bais = new InputStreamReader(baisss);
pstmt.setCharacterStream(1,bais,bytes_zyjs.length);
但是如上⽅式写⼊汉字就会产⽣乱码,于是查看资料得知,上述⽅法多⽤于oracle下,⽽mysql下使⽤的是setBinaryStream⽅法,只要传⼊位置,inputstream,和长度即可。⽰例如下:
byte[] cert_dataBytes = Bytes();
ByteArrayInputStream bais1 = new ByteArrayInputStream(cert_dataBytes);
byte[] prikey_dataBytes = Bytes();
ByteArrayInputStream bais2 = new ByteArrayInputStream(prikey_dataBytes);
String sql = "insert into cert_data values(?,?,?)";
PreparedStatement pstm = null;
try {
conn.setAutoCommit(false);
pstm = conn.prepareCall(sql);
pstm.setInt(1,cert_sn);
pstm.setBinaryStream(2, bais1,cert_dataBytes.length);//使⽤⼆进制读取,可以直接写⼊汉字,否则容易产⽣乱码
pstm.setBinaryStream(3, bais2, prikey_dataBytes.length);
connmit();
conn.setAutoCommit(true);
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(pstm != null)
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//从数据库中读取Blob类型数据后,要转换成String类型,即转换成InputStream,再从InputStream转成byte[],再到String即可。如下:
//把数据库中blob类型转换成String类型
public String convertBlobToString(Blob blob){
String result = "";
try {
ByteArrayInputStream msgContent =(ByteArrayInputStream) BinaryStream();
byte[] byte_data = new byte[msgContent.available()];
result = new String(byte_data);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
2批量插⼊ 带blob类型的实体类
public void insertBatchList(ArrayList list) {
Connection conn = null;
PreparedStatement stmt = null;
MetadataMapVo vo = null;
try {
conn = DBConnection(JNDINames.CRM_DATASOURCE, this);
stmt = conn.FilterSQL(SQL_INSERT));
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
vo = (MetadataMapVo) (i);
int index = 1;
stmt.setString( index++, ((MetadataMapVo)vo).getId() );
stmt.setString( index++, ((MetadataMapVo)vo).getInterc_type());
stmt.setString( index++, ((MetadataMapVo)vo).getInterc_metadata_id() );
stmt.setString( index++, ((MetadataMapVo)vo).getSource_type() );
stmt.setString( index++, ((MetadataMapVo)vo).getSource_id() );
stmt.setString( index++, ((MetadataMapVo)vo).getTarget_type() );
stmt.setString( index++, ((MetadataMapVo)vo).getTarget_id() );
String desc =((MetadataMapVo)vo).getDescription();
if(!StringUtil.isEmptyOrNull(desc)){
ByteArrayInputStream is = new Bytes());
stmt.setBinaryStream( index++, Bytes().length);
}else{
stmt.setString( index++, null);
}
stmt.setString( index++, ((MetadataMapVo)vo).getRequeir_file_path() );
stmt.setString( index++, ((MetadataMapVo)vo).getStatus());
stmt.addBatch();
}
java stream
}
} catch (SQLException se) {
Debug.print(SQL_INSERT, this);
throw new DAOSystemException("SQLException while insert sql:\n" + SQL_INSERT, se);  } finally {
DAOUtils.closeStatement(stmt, this);
DAOUtils.closeConnection(conn, this);
}
}
查询带blob类型的实体类
[code]
private String convertBLOBtoString(Object blobContent) {
try {
Class clazz = Class();
Method method = Method("getBinaryStream",new Class[] {}); InputStream is = (InputStream) method.invoke(blobContent,new Object[] {}); ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i=-1;
while((ad())!=-1){
baos.write(i);
}
String();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
private Map rowToMap(ResultSet rs) throws Exception {
Map result = new HashMap();
ResultSetMetaData rsmd = rs.getMetaData();
int cols = ColumnCount();
String type="";
for (int i = 1; i <= cols; i++) {
ColumnName(i).toUpperCase().equals("SOURCE_TYPE")){
type = rs.getString(i);
}
ColumnName(i).toUpperCase().equals("DESCRIPTION")){
result.ColumnName(i).toLowerCase(), Blob(i)));;
}else{
result.ColumnName(i).toLowerCase(), rs.getString(i));
}
}
if(!type.equals("")){
if("4".im())||"6".im())){
String xml=(String) ("description");
Document doc =DocumentHelper.parseText(xml);
Element root = RootElement();
String cal_function = root.attributeValue("cal_function");
String require_des = root.attributeValue("require_des");
result.put("cal_function", cal_function);
result.put("require_des", require_des);
}
}
return result;
}
@Override
public List getBooldData(Map map) throws Exception {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
List<Map<String,String>> list = null;
String whereCond ="";
try {
whereCond =" where a.interc_type='"+("interc_type")+"' and
a.interc_metadata_id='"+("interc_metadata_id")+"'";
conn = DBConnection(JNDINames.CRM_DATASOURCE, this);
stmt = conn.FilterSQL(SELECT_SQL + whereCond));
rs = uteQuery();
Map<String, String> m = null;
list = new ArrayList<Map<String,String>>();
()){
list.wToMap(rs));
}
} catch (SQLException se) {
Debug.print(SELECT_SQL + whereCond, this);
throw new DAOSystemException("SQLException while getting sql:\n" + SELECT_SQL + whereCond, se); } finally {
DAOUtils.closeResultSet(rs, this);
DAOUtils.closeStatement(stmt, this);
DAOUtils.closeConnection(conn, this);
}
System.out.println(); System.out.println(list.size()); return list;
}
[/code]

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。