在线等:Hibernate如何向oracl插入大段文本,急!!

vvfish119 2005-04-06 01:04:45
使用vchar(2)只能插入4000个字符,如果想插入更多的文字,比如一篇小说,应该如何解决?希望给出完整解决方法,谢谢大家了。
...全文
147 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
pw0819_wang 2005-04-06
  • 打赏
  • 举报
回复
有什么不懂的可以加我QQ:33186506
我也是刚刚研究HIBERNATE大家可以一起讨论一下:)
pw0819_wang 2005-04-06
  • 打赏
  • 举报
回复
晕哦,不能连续回复,还好爪哥帮忙,3q.

package blob;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;

/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class ConToDB {
public static final ThreadLocal localsession = new ThreadLocal();

private static Configuration sf = null;

private static SchemaExport dbExport = null;

private static SessionFactory session_factory = null;

static {
try {
sf = new Configuration().addClass(ProductTest.class);//绑定类

//如用了下面三句话第一次运行时会自动创建表,如原来就存在这些用到的表就会删除重建
//dbExport = new SchemaExport(sf);

//dbExport.setOutputFile("myschema.sql");

//dbExport.create(true, true);

session_factory = sf.buildSessionFactory();//创建一个SessionFactory实例

} catch (Exception e) {
System.out.println("Exception " + e.getMessage());

}

}

public static Session currentSession() throws HibernateException {//返回当前工作线程中的SESSION
Session session = (Session) localsession.get();
//如果线程还不存在的话就创建一个新的SESSION
if (session == null) {
session = session_factory.openSession();//创建与数据库的连接同等于JDBC里的CONN
localsession.set(session);
}
return session;
}

public static void ClossSession() throws HibernateException {
Session session = (Session) localsession.get();
localsession.set(null);
if (session != null)
session.close();
}

}

这个类是连接数据库的.我把它分开写了.

我还做了个测试类,就是从java文件直接保存这个图片文件(你也可以保存别的)到数据库:

/*
* Created on 2005-3-9
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package blob;

import net.sf.hibernate.HibernateException;

/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
*
* @hibernate.class
* table="Test.java"
* dynamic-update="true"
* dynamic-insert="true"
* optimistic-lock="version"
*/
public class Test {

public static void main(String args[]){
BlobTest bt = new BlobTest();
ProductTest p = new ProductTest();
try {
bt.insert(p, "18.jpg");
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
对了,刚才的ProductTest对应的XML文件如下:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping
>
<class
name="blob.ProductTest"
table="ProductTest"
dynamic-update="true"
dynamic-insert="true"
select-before-update="false"
optimistic-lock="version"
>

<id
name="ID"
column="ID"
type="java.lang.Integer"
unsaved-value="0"
>
<generator class="sequence">
<param name="sequence">seq_id</param>
</generator>
</id>

<property
name="pic"
type="java.sql.Blob"
column="pic"
/>
</class>

</hibernate-mapping>
fdabobi 2005-04-06
  • 打赏
  • 举报
回复
帮up~~
pw0819_wang 2005-04-06
  • 打赏
  • 举报
回复
BlobTest:

import oracle.sql.BLOB;
/**
*
* @hibernate.class table="BlobTest" dynamic-update="true" dynamic-insert="true"
* optimistic-lock="version"
*/
public class BlobTest {

Session session = null;

Transaction tx = null;

Query query = null;

public void insert(ProductTest product, String fileName)
throws HibernateException {
session = ConToDB.currentSession();
byte[] buffer = new byte[1];
buffer[0] = 1;
product.setPic(Hibernate.createBlob(buffer));
tx = null;
try {
//session.connection().setAutoCommit(false);
tx = session.beginTransaction();
session.saveOrUpdate(product);
session.flush();
session.refresh(product, LockMode.UPGRADE);
BLOB blob = (BLOB) product.getPic();
OutputStream out = blob.getBinaryOutputStream();
String file = fileName;
File f = new File(file);
FileInputStream fin = new FileInputStream(f);
int count = -1, total = 0;
byte[] data = new byte[(int) fin.available()];
fin.read(data);
out.write(data);
fin.close();
out.close();
session.flush();
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
ConToDB.ClossSession();
}
}

public ProductTest queryProduct() {
try {
session = ConToDB.currentSession();
String hql = "from blob.ProductTest";
query = session.createQuery(hql);
List userList = query.list();
ProductTest product = (ProductTest) userList.get(0);
return product;
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}

}
}
pw0819_wang 2005-04-06
  • 打赏
  • 举报
回复
blob.PorductTest:

package blob;

import java.sql.Blob;




/**
*
* @hibernate.class
* table="ProductTest"
* dynamic-update="true"
* dynamic-insert="true"
* optimistic-lock="version"
*/
public class ProductTest {

private Integer ID = null;

/**
* @hibernate.id
* column="ID"
* type="java.lang.Integer"
* unsaved-value="0"
* generator-class="native"
*/
public Integer getID() {
return ID;
}
/**
* @param id The iD to set.
*/
public void setID(Integer id) {
ID = id;
}
private Blob pic = null;

/**
* @hibernate.property
* column="pic"
*
* @return Blob
*/

public Blob getPic() {
return pic;
}
/**
* @param pic The pic to set.
*/
public void setPic(Blob pic) {
this.pic = pic;
}
}
pw0819_wang 2005-04-06
  • 打赏
  • 举报
回复
唉~不能加附件,我只能贴代码了,晕哦.
index.jsp
<%@ page language="java" pageEncoding="GB2312" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page import="java.io.*"%>
<%@page import="java.sql.Blob"%>
<%@page import="blob.*"%>
<%
response.reset();
//这个设置很重要,否则客户端浏览器不能识别输出内容,导致弹出下载的对话框。
response.setContentType("image/jpeg");
ServletOutputStream sos = response.getOutputStream();
//这里是用hibernate来根据id装载对象,你可以用别的方式如jdbc来组装对象,附文中有
//BaseInfo这个javabean的示意性代码.
BlobTest bt = new BlobTest();
ProductTest bif = bt.queryProduct();
//输出图片
if(bif!=null&&bif.getPic()!=null){
Blob blob=(Blob)bif.getPic();
InputStream pi = blob.getBinaryStream();
int blobsize = (int)blob.length();
byte[] blobbytes = new byte[blobsize];
int bytesRead = 0;
while ((bytesRead = pi.read(blobbytes)) != -1) {
sos.write(blobbytes, 0, bytesRead);
}
pi.close();
sos.flush();
} else{
out.print("no record");
}
%>
loveknife 2005-04-06
  • 打赏
  • 举报
回复
oracle的字段换大点的, 比如blob,以流的形式读写,
至于用Hibernate怎么插入,俺也不清楚,正在努力学习ing
kk2486 2005-04-06
  • 打赏
  • 举报
回复
不清楚,帮顶吧

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧