When writing applications, I need a lot of insertion into a big text, but Oracle's Clob is more cumbersome, so I didn't choose to use Clob, but used the long type in Oracle in previous versions [but the long type has some restrictions, There can only be a long field in a table]. Inserting the database directly into the database directly, but Oracle has some statements that cannot exceed 4,000 characters and report the errors of ORA-01704. Finding the Oracle's document finds a solution, that is to insert String into the database using the setcharacterStream () method for the LONG field.
Code: SQL = "INSERT INTO MSG_INFO VALUES (?,?,?, [Long type field], 'c', sysdate, sysdate " msgterm ",?)"; pstat1 = conn.preparestatement (SQL); PSTAT1. Setlong (1, msg_id); Pstat1.setint (2, MSG_GP_ID); Pstat1.setString (3, MSG_TITLE); Pstat1.SetCharacterstream (4, New StringReader ()), MSG_INFO.LENGTH ()); conn. Commit (); pstat1.setlong (5, this.upid); long type Oracle is not recommended, or using Clob is a forward road, after all, it is now the 21st century. But CLOB does use it very much, the landlord's post collection. **************************************************************** Data *************************************** In Oracle, you can use the long type to save large Text, but the data of the LONG type is different, and the general data type is different.
Save data into the field of the long type, below will use a table to test, the creation statement of the table is as follows:
Create Table T_longtest (ID Integer Not Null, FileName Varchar2 (100 Byte), Content Long Logging NocachenoparalLel;
CREATE UNIQUE INDEX PK_T_LONGTEST ON T_LONGTEST (ID) LoggingnoparalLel;
ALTER TABLE T_LONGTEST ADD (Constraint PK_T_LONGTEST Primary Key (ID));
Insert the data of the long type
BufferedReader bufReader = new BufferedReader (new FileReader (file)); Integer id = Integer.valueOf (PubFun1.CreateMaxNo (TEST_LONG_ID, 1)); PreparedStatement pstmt = con.prepareStatement (INSERT_LONG_SQL); pstmt.setObject (1, id); pstmt .SETOBJECT (2, FileName); PSTMT.SetCharacterstream (3, Bufreader, (int) length); int RetValue = pstmt.executeUpdate (); if (RetValue! = 1) {Logger.Error ("Error On Insert Value") BUFREADER.CLOSE (); pstmt.close (); INSERT_LONG_SQL value is: INSERT INTO T_LONGTEST (ID, filename, content) value (?,?,?) Note You need to use the setcharacTream method to set the value of the field of the LONG type.
Read the data of the long type
Reading also needs to be read using the stream. The following code snippet illustrates the method of reading the field of the LONG type. PrepaRedStatement pstmt = con?preparestatement (query_long_col_sql); pstmt.setObject (1, ID); ResultSet RS = PSTMT .executeQuery (); if (rs.next ()) {Reader reader = rs.getCharacterStream (1); BufferedReader bufReader = new BufferedReader (reader); StringBuffer strBuf = new StringBuffer (); String line; while ((line = bufReader .readline ())! = null) {strbuf.append (line); strbuf.append ("/ r / n");} bufreader.close (); System.Out.println ("THE Content IS:" strbuff .tostring ());} Queyr_long_col_sql's value is: SELECT Content from T_LongTest WHERE ID =?
Update data for long type
Updating the way the long type is the same, just the SQL statement. The following code breaks how to update the LONG type data. (This example does not use the T_longTest table) StringReader Reader = New StringReader (xmlstring); PSTMT = con .prepareStatement (REPORT_MODEL_CONTENT_UPDATE_SQLSTRING); pstmt.setCharacterStream (1, reader, xmlString.length ()); pstmt.setInt (2, reportModelId); if (pstmt.executeUpdate () == 0) {logger .error ( "Error on Update ");} Reader.close (); Report_Model_Content_Update_sqlstring takes the value: UPDATE REPORT_MODEL SET =? Where report_model_id =? Summary:
As can be seen from the above example, the field of operation LONG type is primarily via CHARACTERSTREAM, and if it is updated or inserted into the database into the database using preparedState's setcharactersTream, and incoming the length of the Reader type and the length of the string. If it is retrieved The data of the LONG type in the database, then use the GetCharacterStream method to get an object type object, and then you can get the LONG type data.