Access to the Clob field

xiaoxiao2021-03-06  22

It is an Oracle9i database, JBoss or WebLogic. JDBC uses Class12.jar with Oracle9i ------------- Database Structure:

Java code:

Create Table Sncparameters (ID Number (19) Not Null, SNCID Number (19), Name Varchar2 (255), Value Clob)

-------------- Bo is built with xdoclet:

Java code:

Public class sncparameters extends baseObject {

/ ** * returns the ID. * * @Return long * @ hibernate.id * column = "id" * type = "long" * generator-class = "native" * unsaved-value = "null" * / public long GetId () {return id;}

/ ** * sets the id attribute of the sncparameters object * * @Param id the new id value * / public void setid (long id) {this.id = id;}

/ ** * returns the name. * * @Return string * * @ hibernate.property * Column = "name" * type = "string" * not-null = "true" * unique = "false" * / public string getname () {RETURN NAME;

/ ** * sets the name attribute of the sncparameters object * * @Param name the new name value * / public void setName (String name) {this.name = name;}

/ ** * returns the sncid. * * @Return long * * @ hibernate.property * column = "sncid" * type = "long" * not-null = "true" * unique = "false" * / public long getsncid () {RETURN SNCID;} / ** * sets the sncid attribute of the sncparameters Object * * @Param sncid the new sncid value * / public void setsncid (long sncid) {this.sncid = sncid;}

/ ** * @Return Clob * * @ hibernate.property * column = "value" * type = "clob" * not-null = "true" * unique = "false" * / public clob getValue () {Return Value;

/ ** * Sets the Values ​​attribute of the SNCParameters object * * @param values ​​The new Values ​​value * / public void setValue (Clob value) {this.value = value;} private Long id; private Long sncId; private String name; Private clob value; private string value; public string getValueString () {return valueString;} public void setvaluestring (string valueString) {this.valueString = valueString;}}

Note: ValueString is not mapped to the database's clob field, just convenient to use this BO person with get, set to process this giant clob field ---------- xDoclet generated XML file:

Java code:

------------------ Insert code: Java code:

public List batchAddSncParameters (List sncParametersList, Long sncId) throws DbAccessException {logger.enterMethod (); List ret = new ArrayList (); try {sess = getSession (); if (! sncParametersList = null && sncParametersList.size ()> 0) {For (int i = 0; i

} Else {// jboss oracle.sql.clob clob = (oracle.sql.clob) cpnew.getValue (); java.io.writer pw = clob.getcharacteroutputstream (); pw.write (content); pw.flush ); Pw.close ();} ret.add (new long);}}}} catch (Exception E) {logger.error (e); ErrorReason errorreason = new errorreason (ErrorReason.insert_Object_failed_reason); throw new DBACCESSEXCEPTION (DBACCESSEXCEPTION, ERRORREASON);} finally {closesession (sess); logger.exitmethod ();} return ret;} ---------------- Note: WebLogic must use WebLogic.jdbc.vendor.Orcle.OracleThinClob --- ------------------ Read the clob field:

Java code:

public List selectSncParametersBySncId (long sncId) throws DbAccessException {logger.enterMethod (); List ret = new ArrayList (); try {sess = getSession (); String query = "select cp from cp in class com.idncn.mc.bo. SncParameters where cp.sncid =? "; Logger.debug (" SQL = " query); list it = sess.find (Query, New Long), Hibernate.long); for (int i = 0; i < iTer.size (); i ) {sncparameters newcp = new sncparameters (); sncparameters cp = (sncparameters) (iTer.get (I)); Logger.debug ("After Fetch: CP); newcp.setID (CP) .GETID ()); newcp.setsNCID (cp.getsncid ()); newcp.setname (cp.getname ()); java.sql.clob clob = cp.getValue (); if (clob! = null) {Logger .debug ("b ===" clob.length ()); string b = clob.get Substring (1, (int) Clob.Length ()); //logger.debug ("b===" b); newcp.setValueString (b);} ret.add (newcp);}} catch (Exception e) {logger.error (e); errorReason errorReason = new errorReason (ErrorReason.SELECT_FAILED_REASON); throw new DbAccessException (DbAccessException.DBA_OPERATE_EXCEPTION, errorReason);} finally {closeSession (sess); logger.exitMethod ();} return ret; }

-------------- Update this field: Java code:

public void updateSncParameters (SNCParameters newParam) throws DbAccessException {logger.enterMethod (); try {sess = getSession (); Long id = newParam.getId (); SNCParameters pp = (SNCParameters) sess.load (SNCParameters.class, id, net .sf.hibernate.lockmode.upgrade; pp.setsncid (newparam.getsncid ()); pp.setname (newparam.getName ()); pp.setid (newparam.getid ()); string newValue = newparam.getValueString ); Logger.debug ("update length =" newvalue.Length ()); string appserver = system.getProperty ("Appserver", "JBoss"); Logger.debug ("Appserver:" AppServer); if (! Appserver.equalsignoreCase ("jboss")) {// Weblogic OracleThinClob Clob = (ORACLETHINCLOB) PP.GetValue (); if (pp! = null) {java.io.writer PW = Clob.getCharacterOutputStream (); PW.WRITE (); pw.flush (); pw.close ();}} else {// jboss oracle.sql.clob clob = (oracle.sql.clob) pp.getValue (); if (pp! = NULL) {java.io.writer PW = clob.getCharacterOutputStream (); pw.write (newvalue); pw.flush (); pw.close ();}}} catch (exception e) {logger.error (e) ErrorReason ErrorReason =

转载请注明原文地址:https://www.9cbs.com/read-66516.html

New Post(0)