js| Beginner | Process A JSP beginner's learning process (vii)
Theunforgiven
Seventh chapter Ultra-long text operation--CLOB type data access
Back to the time I wrote the message board, when to store the text of the message board, found that VARCHAR2 () (variable length of the string) can only save 4000 bytes, that is, 2000 Chinese characters, this is too little ah, check the database type of data, found that there are so many types: LONG, 2G (if I remember correctly, it is for forward compatibility, deprecated); clob,4g, character; blob,4g, binary. It seems that the super long text should use CLOB, the picture is naturally with a blob, ask someone else, know that these two types is not as VARCHAR2 () as direct deposit, had to give up, first with VARCHAR2 () top a burst.
Then I finally got free, determined to finish the task, look up some information on the Internet, see other people's example, finally is self-taught to see: Save time need to use Empty_clob () (This is not Java function) to save an empty logo (with my understanding is to initialize first), and then through The stream writes the data. Here's the code, where a try is a CLOB type of save operation:
-----------------------------------save_new.jsp------------------------------------------
<%@ include file= "Include.inc"%>
<%@ page contenttype= "text/html;charset=gb2312" errorpage= "request_error.htm"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Untitled Document </title>
<body>
<%
String title = Request.getparameter ("title");
String kind=request.getparameter ("kind");
String Newtitle=title.replaceall ("'", "" "),////ReplaceAll () to change all single quotes in a text string to two consecutive single quotes
String Text = request.getparameter ("text");
String text1=text.replaceall ("'", "'"), no need to change single quotes to two consecutive single quotes when saving CLOB
String Text2=text.replaceall ("<", "<")///ReplaceAll () to convert all < in the string to <
String Newtext=text2.replaceall (">", ">")///ReplaceAll () to convert all > in the string to >
Replace can only handle single characters!!
Change ' is to not affect the database query statement
<> is to prevent web pages from generating labels for them, such as:<table>,<form>
String Author=session.getattribute ("name"). ToString ();
Out.println (author);
Long Id=system.currenttimemillis ()//Gets a time, from 1970-1-1 0:00:00 to the current time of the number of milliseconds, with this number as the ID of the article identification
Java.text.SimpleDateFormat formatter = new Java.text.SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Format Date
Java.util.Date currenttime_1 = new Java.util.Date ()//Get current system time
String strdate = Formatter.format (currenttime_1); Convert date time to string form
Connection con = null;
PreparedStatement stmt = null;//can not use statement, I do not know why, check the API, said this preparedstatement can be used for
Efficient execution of multiple statements, not found statement this class
ResultSet rs = null;
Try
{
Class.forName (classforname);//Load driver category
Con=drivermanager.getconnection (SERVANDDB);//Establish a database connection
Con.setautocommit (false);/Set not automatically submitted
String sql= "INSERT into article (ID,AUTHOR,TITLE,TIME,KIND,TEXT_CLOB) VALUES (' +id+" ', ' "+author+" ', ' "+newtitle+ ') ', '"
Stmt=con.preparestatement (SQL);//Add a record that Clob field is blank.
Stmt.executeupdate ()//Execute
stmt=null;//the next time you use it
Sql= "Select Text_clob from article where id= '" +id+ "' for Update";/it is because of this statement that the ID must have a unique identifier!!!!
If the ID of a record in the database is the same as the current ID value, that record is found and cannot be written to the Clob field in the newly inserted record!
Stmt=con.preparestatement (SQL);//Find the record you just added
Rs=stmt.executequery ();
Oracle.sql.CLOB OSC = null;//initialization of an empty CLOB object
if (Rs.next ())
Osc= (Oracle.sql.CLOB) Rs.getclob ("Text_clob");
Writer W=osc.getcharacteroutputstream ()//use character output stream
W.write (NewText);//write String Str_text to stream
W.flush ()//output stream data, probably formally written to the CLOB
W.close ();
Con.commit ()//Execute
Response.sendredirect ("index.jsp?page=1")/Back to Main page
}
catch (Exception e)
{out.println (e);}
Finally
{
if (rs!=null)
Rs.close ();
if (stmt!=null)
Stmt.close ();
if (con!=null)
Con.close ();
}
%>
</body>
--------------------------------------------------------------------------
Take the time is relatively simple, mainly on two lines, look at the following code:
--------------------------------------------------------------------------
<%
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
Long Id=long.parselong (Request.getparameter ("ID");//The received string is converted to a long type
Try
{
Class.forName (classforname);//Load driver category
Con=drivermanager.getconnection (SERVANDDB);//Establish a database connection
Stmt=con.createstatement ();
String sql= "SELECT * from Article where id= '" +id+ "";
Rs=stmt.executequery (SQL);
if (Rs.next ())
{//The next 2 lines are used to read data from the CLOB type, and are converted to strings.
String str_text=osc.getsubstring ((Long) 1, (int) osc.length ()),//substring is an intercept string (from 1 to length), if an error occurs with osc.getstring.
Out.print (Str_text);
}//if
}//try
catch (Exception e) {}
Rs.close ();
Stmt.close ();
Con.close ();
%>
--------------------------------------------------------------------------
The access problem for the CLOB type is now resolved, but when you manipulate the text string you will find many problems, such as single quotes ('), tags (such as <table>, <br>), and the question of carriage returns and spaces, etc. All need you to find and solve in practice.
The next chapter is about blobs.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.