A learning process for beginners of jsp (preferred for SUN Enterprise Applications) (7)
TheUnforgiven
Chapter 7 Operations on ultra-long text-Access to Clob data
When I wrote a message board, I wanted to store the body content of the message board. I found that VARCHAR2 () (a variable-length string) can only store 4000 bytes, that is, 2000 Chinese characters, this is too little. Check the database type and find that there are several types: LONG, 2G (if I remember correctly, it is for forward compatibility, not recommended); CLOB, 4G, character; BLOB, 4G, binary. It seems that CLOB should be used for ultra-long text. The image uses BLOB and asks someone to know that the two types cannot be directly stored as VARCHAR2, use VARCHAR2 () first.
Later, I finally got free and decided to complete this task. I checked some information on the Internet and read other people's examples. I finally understood it through my lack of knowledge: you must use empty_clob () (this is not a Java function) to save an empty identifier (in my understanding, it is to initialize it first), and then write data through the "stream. The following is the code, where try contains the CLOB type storage operation:
--------------------------------- Save_new.jsp (preferred for SUN Enterprise Applications )------------------------------------------
<% @ 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">
<Html xml (standardization is getting closer and closer) ns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> untitled document </title>
</Head>
<Body>
<%
String title = request. getParameter ("title ");
String kind = request. getParameter ("kind ");
String newtitle = title. replaceAll ("", ""); // use replaceAll () to change all single quotes in the text String to two consecutive single quotes
String text = request. getParameter ("text ");
// String text1 = text. replaceAll ("", ""); When storing clob, you do not need to change single quotes to two consecutive single quotes
String text2 = text. replaceAll ("<", "& lt"); // use replaceAll () to change <to <
String newtext = text2.replaceAll (">", "& gt"); // use replaceAll () to change all> In the String to & gt
// Replace can only process a single character !!
// The modification is to avoid affecting the query statements of the database.
// Modify <> to prevent web pages from generating tags, such as <table> and <form>.
String author = session. getAttribute ("name"). toString ();
Out. println (author );
Long ID = System. currentTimeMillis (); // obtain the number of milliseconds from 0:00:00 to the current time. Use this number as the ID of the document.
Java. text. SimpleDateFormat formatter = new java. text. SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // format the date
Java. util. Date currentTime_1 = new java. util. Date (); // obtain the current system time
String strdate = formatter. format (currentTime_1); // converts a date to a String.
Connection con = null;
PreparedStatement stmt = null; // Statement cannot be used. I do not know why. I checked the API and said that this PreparedStatement can be used
// The Statement class is not found for multiple efficient statements.
ResultSet rs = null;
Try
{
Class. forName (CLASSFORNAME); // load the driver category
Con = DriverManager. getConnection (SERVANDDB); // create a database connection
Con. setAutoCommit (false); // The setting is not automatically submitted.
String SQL = "insert into article (id, author, title, time, kind, text_clob) values (" + ID + "," + author + "," + newtitle + ", "+ strdate +", "+ kind +", empty_clob () "; // The CLOB field name of the text stored in my database is text_clob