mysql 5.0数据库mydb
CREATE DATABASE mydb
CHARACTER SET 'utf8'
COLLATE 'utf8_general_ci';
表employee:
create table employee (
id integer not null auto_increment,
firstname varchar(15),
........
resume text,
primary key (id)
)ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
employee.hbm.xml
<class name="Employee" table="employee">
<id name="id" type="java.lang.Integer" column="id">
<generator class="identity" />
</id>
<property name="firstname" type="java.lang.String" column="firstname" length="15"/>
....
<property name="resume" type="java.sql.Clob" column="resume" />
</class>
操作类Dao.java:
public void importLargeObjects() {
String filename = null;
File file;
String rootPath ;
Session session = Dao.sessionFactory.openSession();
Query query = session.createQuery("from Employee");
List<?> list = query.list();
Iterator<?> it = list.iterator();
while (it.hasNext()) {
Employee employee = (Employee) it.next();
session.beginTransaction();
rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath().toString();
//取build/texts/目录下雇员名.txt为当前文件名,文本文件均以utf8编码。
filename = rootPath+"texts/" + employee.getFirstname() + ".txt";
file = new File(filename);
try {
FileReader reader = new FileReader(filename);
// 写入的文件不能过大,即要小于int最大值。
Clob resume = Hibernate.createClob(reader, (int) file.length());
employee.setResume(resume);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//file.delete();
session.save(employee);
session.getTransaction().commit();
}
session.close();
}
用MySQL Query Browser查看resume为乱码,怀疑可能是MySQL Query Browser问题,使用SQL Manager for MySQL 2007(4.1)查看resume,仍为乱码,估计应该是乱码。如何正确编码?
分享到:
相关推荐
介绍了利用Hibernate将一个长字符串转换为流存储至Clob字段中,然后再从Clob字段中读出来,转换为字符串输出的方式
JDBC读写Oracle的CLOB字段
是我自己验证过的 用的是oracle10数据库 文件里面有具体的配置。
向Oracle数据库插入Clob大段文本解决方法
hibernate Blob、Clob字段的映射的例子.数据库mysql,数据库放在文件夹里面,例子的说明文章在我的csdn blog: http://blog.csdn.net/zhengcandan
CLOB字段处理 对CLOB进行存取的例子
ORACLE中CLOB字段转String类型
把oracle数据库中字段类型为clob的字段值以字符串的形式读取出来
CLOB字段类型报错 ORA-01704:文字字符串过长的解决
dblink_clob字段异常解决方案,解决dblink连接远程数据库时无法使用clob字段的问题
J2EE开发中,经常会遇到存储大文本的文字信息,而oracle数据库的vachar字段最大能存储4000个字节,存储更大的信息必须使用clob或blob字段,本文档就是针对j2ee开发中对colb字段操作的实现。
关于在oracle数据库中读取clob大字段问题
用Hiberate处理CLOB字段 ;通过Hiberate处理CLOB类型的数据;
kettle通过java代码将数据库blob 字段抽取到本地文件
hibernate存取oracle的clob
使用Hibernate映射Blob,Clob字段,并对这些字段完成存,取功能
数据库中clob类型转换的问题 数据库中clob类型转换的问题
JDBC方式操作CLOB字段实例代码 。
Oracle如何插入CLOB字段值,附件为简单的示例代码。Oracle如何插入CLOB字段值,附件为简单的示例代码。
主要是整合了jdbc处理clob类型的增删改查方法,有需要的同学可以看下