1.我用的是sql server数据库。因为要存入大量的字符串数据 大约几十万个字符(中文),
所以我在数据库中把这个字段设置成了image字符集。这样存入把中文转换成二进制存入了数据库。
方法:
/**
*
* @param str
* @return <字符串转换流的形式>
*/
public InputStream convertToInputStream(String str)
{
ByteArrayInputStream is = new ByteArrayInputStream(str.getBytes());
return is;
}
//保存数据库中
byte[] b = new byte[is.available()];
infoRelease.setNewsContent(b);
数据形式如图:
2.问题:现在我如何从数据库中取出数据还原成中文的字符串?
所以我在数据库中把这个字段设置成了image字符集。这样存入把中文转换成二进制存入了数据库。
方法:
/**
*
* @param str
* @return <字符串转换流的形式>
*/
public InputStream convertToInputStream(String str)
{
ByteArrayInputStream is = new ByteArrayInputStream(str.getBytes());
return is;
}
//保存数据库中
byte[] b = new byte[is.available()];
infoRelease.setNewsContent(b);
数据形式如图:
2.问题:现在我如何从数据库中取出数据还原成中文的字符串?
//从数据库中取出该表单条记录
InfoRelease infoRelease = (InfoRelease) infoObjectMap.get("return_Data");
//取出字段NewsContent 所对应的字节流
byte[] bt = infoRelease.getNewsContent();
{
//这里如何转化~~~}
Person person; // 需要插入的Person对象InputStream input; // Person应该插入的Photo,通过网页上传获得,将插入image类型字段Connection conn = new Connection(); // 实际的Connection对象PreparedStatement stmt = null;ResultSet rs = null;String sql= null; conn.setAutoCommit(false); // 设置 auto commit = false;sql = "insert into person (id, name) values (?, ?)"; // 插入Person的其它字段stmt = conn.prepareStatement(sql);stmt.setInt(1, person.id);stmt.setString(2, person.name);stmt.executeUpdate(); sql = "update person set photo = null where id = ?"; // 初始化image数据类型的指针// 即使在insert时插入image类型的值为null,数据库依然不会创建指针。可以使用update创建指针。stmt = conn.prepareStatement(sql);stmt.setInt(1, person.id);stmt.executeUpdate(); sql = "select textptr(photo) from person where id = ?"; // 获得image类型的指针stmt = conn.prepareStatement(sql);stmt.setInt(1, person.id);rs = stmt.executeQuery(sql);if (rs.next()){byte[] ptr = rs.getBytes(1); // ptr:image类型的指针sql = "updatetext person.photo ? ? 0 ?"; // 更新image类型字段,0表示不删除内容stmt = conn.prepareStatement(sql);int offset = 0;int len = 0;byte[] buffer = new byte[10*1024]; // 读写缓冲区while ((len = input.read(buffer)) != -1) // 读取input流,-1表示已经读完{stmt.setBytes(1, ptr); // image类型的指针stmt.setInt(2, offset); // 偏移量stmt.setBytes(3, buffer); // 读入的内容stmt.executeUpdate(); // 将读取内容写入数据库stmt.clearParameters();offset += len; // 移动偏移量}sql = "updatetext person.photo ? ? NULL"; // 删除多余内容,0表示偏移量后所有内容stmt = conn.prepareStatement(sql);stmt.setBytes(1, ptr);stmt.setInt(2, offset); // 终点的偏移量stmt.executeUpdate(); input.close(); // 关闭输入流}conn.commit(); // commit connection
if(rs.next()){
System.out.println(rs.getString("picname"));
int tmpi=0;
InputStream ins= rs.getBinaryStream("pic");
while((tmpi=ins.read())!=-1){
out.write(tmpi);
}
ins.close();
out.flush();
out.close();
System.out.println("Reading from database to file "+newName+" success");
}
你存储的时候String text ="原文";
byte[] strContent = text.getBytes();那么就byte[] bt = infoRelease.getNewsContent();
new String(bt);
如果存储的时候text.getBytes(“XXX”);
那就new String(bt,"XXX")
不行 我试了 取出的字符 是方块的形式。重新说一下我怎么存入数据库的。
--------------------------------------------------------------//存入数据库的时候
String s = myform.getString("newsContent");//从form中取出字符串(即中文文字)
InputStream is = new ByteArrayInputStream(s.getBytes()); //转换成流的形式
byte[] b;
try {
b = new byte[is.available()]; //转换成二进制流
infoRelease.setNewsContent(b); //存入数据库了
} catch (IOException e) {
// TODO Auto-generated catch block
}
//--------------------------------------------
DAO中Entity 中的定义
private byte[] newsContent;
public byte[] getNewsContent() {
return this.newsContent;
} public void setNewsContent(byte[] newsContent) {
this.newsContent = newsContent;
}
//-----------------------------------------------------
Action:
//从数据库中取出的时候 需要处理的
InfoRelease infoRelease = (InfoRelease) infoObjectMap.get("return_Data"); //取出这条信息对象
//从对象中取出发布的信息:目前数据库中存的就是二进制字节流的形式 需要处理转换成 中文
tring text="";
try {
text = new String(infoRelease.getNewsContent(),"GBK"); //这里无论是UTF-8编码还是GBK都是乱码
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
infoRelease.setContent(text);
相同的话用String()去构造,应该不会有问题吧,哈哈
没遇到过失灵的时候,建议楼主先缩小出错的范围。
为什么我用hibernate存完之后 汉字只有 几万个。难道有2G个字节吗?还是我 hibernate配置的不正确 我用的是binary字符
try {
b = new byte[is.available()]; //转换成二进制流
infoRelease.setNewsContent(b); //存入数据库了
} catch (IOException e) {
// TODO Auto-generated catch block
}
b不是没有数据吗?怎么存储的??
我看也是 valuesof
1 String s = myform.getString("newsContent");//从form中取出字符串(即中文文字)
此处的s是否已经乱码
2 你是否存入了数据,请检查一下