importjava.sql.SQLException;importjava.util.Date;importjava.util.Iterator;importjava.util.List;importorg.hibernate.Hibernate;importorg.hibernate.HibernateException;importorg.hibernate.Query;importorg.hibernate.Session;importorg.hibernate.Transaction;importHib_DB.SessionManager;public classTest_Work ...{/** *//***@paramargs
*@throwsSQLException
*@throwsSQLException
*
/*==============================================================*/
/**//* Table: "cmp_bszn" 相关SQL*/
/**//*==============================================================*/
/**//*
create table "cmp_bszn" (
"id" INTEGER not null,
"kind" CHAR(2) not null,
"bszn_title1" VARCHAR2(200) not null,
"bszn_title2" VARCHAR2(200),
"bszn_code" VARCHAR2(50),
"bszn_bumen" VARCHAR2(50),
"bszn_date" VARCHAR2(50),
"bszn_content" CLOB not null,
"sys_date" DATE default SYSDATE not null,
constraint PK_CMP_BSZN primary key ("id")
);*/
public static void main(String[] args) throwsSQLException ...{
Test_Work exam= newTest_Work();/**//*Insert*/exam.InsertRecords();
exam.QueryRecords();/**//*Update*/exam.Demo_Update(new Long(5));/**//*Delete*/exam.DeleteRecords(new Long(3));
}/**//*
* 查询*/
private void QueryRecords() throwsSQLException ...{try...{
Session session=SessionManager.getSession();
Query records= session.createQuery("from cmpBszn order by id");
List record=records.list();
Iterator iterator=record.iterator();while(iterator.hasNext()) ...{
cmpBszn obj=(cmpBszn) iterator.next();
System.out.println(obj.getId()+ " " +obj.getKind());
java.sql.Clob clob=obj.getBsznContent();if (clob != null) ...{
String b1= clob.getSubString(1, (int) clob.length());
obj.setBsznContentString(b1);
}
System.out.println(obj.getBsznContentString());
System.out.println("----------------------------------");
}
System.out.println(record.size());
}catch(HibernateException e) ...{
System.out.println("error");
}finally...{try...{//Step 5 - close the session
SessionManager.closeSession();
}catch(HibernateException e1) ...{//do nothing
}
}
}private void QueryRecords(Long id) throwsSQLException ...{try...{
Session session=SessionManager.getSession();
Query records=session
.createQuery("from cmpBszn order by id where id = " +id);
List record=records.list();
Iterator iterator=record.iterator();while(iterator.hasNext()) ...{
cmpBszn obj=(cmpBszn) iterator.next();
java.sql.Clob clob=obj.getBsznContent();if (clob != null) ...{
String b1= clob.getSubString(1, (int) clob.length());
obj.setBsznContentString(b1);
}
System.out.println(obj.getId()+ " " + obj.getKind() + " "
+obj.getBsznTitle1());
System.out.println("内容:" +obj.getBsznContentString());
System.out.println("----------------------------------");
}
}catch(HibernateException e) ...{
System.out.println("error");
}finally...{try...{//Step 5 - close the session
SessionManager.closeSession();
}catch(HibernateException e1) ...{//do nothing
}
}
}/**//*
* 插入*/
private voidInsertRecords() ...{
String kind= "03";
String bsznTitle1= "ttttttttt";
String bsznContentString= "哈哈成功了";
Date sysDate= newDate();try...{
Session session=SessionManager.getSession();
Transaction tx=session.beginTransaction();
cmpBszn obj= newcmpBszn();
obj.setKind(kind);
obj.setBsznTitle1(bsznTitle1);
obj.setSysDate(sysDate);
obj.setBsznContent(Hibernate.createClob(bsznContentString));//关键createClob方法
session.save(obj);
tx.commit();
System.out.println("Save Success.");
}catch(Exception e) ...{
System.out.println("Save failed.");
}finally...{try...{
SessionManager.closeSession();
}catch(HibernateException e1) ...{
}
}
}private voidUpdateRecords(Long id) ...{
String kind= "03";
String bsznTitle1= "update_test";
String bsznContentString= "修改更新";
Date sysDate= newDate();try...{
Session session=SessionManager.getSession();
Transaction tx=session.beginTransaction();
cmpBszn obj= (cmpBszn) session.load(cmpBszn.class, id);
obj.setKind(kind);
obj.setBsznTitle1(bsznTitle1);
obj.setSysDate(sysDate);
obj.setBsznContent(Hibernate.createClob(bsznContentString));//关键
session.update(obj);
tx.commit();
System.out.println("Save Success.");
}catch(Exception e) ...{
System.out.println("Save failed.");
}finally...{try...{
SessionManager.closeSession();
}catch(HibernateException e1) ...{
}
}
}/**//*
* 删除记录*/
private voidDeleteRecords(Long id) ...{try...{
Session session=SessionManager.getSession();
cmpBszn message= (cmpBszn) session.load(cmpBszn.class, id);
Transaction tx=session.beginTransaction();
session.delete(message);
tx.commit();
System.out.println("Delete successful.");
}catch(HibernateException e) ...{//TODO 自动生成 catch 块
e.printStackTrace();
System.out.println("删除失败!");
}finally...{try...{//Step 5 - close the session
SessionManager.closeSession();
}catch(HibernateException e1) ...{//do nothing
}
}
}private void Demo_Update(Long id) throwsSQLException ...{
System.out.println("修改前:");this.QueryRecords(id);this.UpdateRecords(id);
System.out.println("修改后:");this.QueryRecords(id);
}
}