Some problems that should be paid attention to when using Oracle Database and JSP

xiaoxiao2021-03-06  40

I used Oracle9i, Oracle8, have a long time, I wrote it here for the questions you need to pay in JSP, only for your reference.

First, how to deal with Clob, BLOB's large type

CLOB can be used to store large text data, up to 4GB data, more common. The SQL.Clob class provided by the application development. It provides two ways to read the data of the Clob:

GetCharactersTream () Method Returns the input stream encoded by Unicode (Java.io.Reader object)

getasciistream () Method Returns the input stream encoded by ASCII (Java.io.InputStream Object)

So if you have the possibility to store Chinese characters in your database, you will use the previous method.

Now give an actual example, let me learn how to use Clob step by step.

First, create a table with a clob field:

Create Table Test (ID Integer, Content Clob);

Next, we insert a record into this table through JSP, and then get it.

Insert operation:

<%

Connection con = drivermanager.getConnection (...); // This line is slightly

String content = Request.getParameter ("content"); // big text data

Con.SetAutocommit (false); // *

String SQL = "INSERT INTO TEST VALUES (1, EMPTY_CLOB ())";

Statement Stmt = con.createstatement ();

Stmt.executeUpdate (SQL);

CON.commit (); // *

SQL = "SELECT Content from Test where id = 1 for update";

ResultSet RS = Stmt.executeQuery (SQL);

IF (rs.next ()) {

Oracle.Sql.Clob Clob = (Oracle.Sql.Clob) rs.getClob (1);

Clob.putstring (1, content);

SQL = "Update test set content =? where id =" seq;

PreparedStatement PSTMT = Con.PrepareStatement (SQL);

PSTMT.SETCLOB (1, Clob);

PSTMT.ExecuteUpdate ();

}

%>

What is the point you need to pay attention to:

1) CLOB type data cannot be directly INSERT, you must assign a Locator to it by the EMPTY_CLOB () method (Simpi, blob with the EMPTY_BLOB () function allocated Locator). Then take it out (this time it is certainly no data, However, the result set is not empty), get a Clob object, modify the content of the object, let it meet our needs, update the row record through the Update method.

2) You must lock the row (implementation via the for Update key) when modifying the LOB type record (implementation via the for Update key), otherwise Oracle will report an error.

3) Just inserted records, Select FOR Update, "Violation Reading Order" error, the solution is to set the auto submission function to false, ie the auto-submit, then commit it, then Select, it is possible. It is the role of the above code // *.

Below, we will read the recorded records from the database and display:

<%

String SQL = "SELECT Content from Test where Doc_ID = 1";

ResultSet RS = Stmt.executeQuery (SQL);

String content = ""; if (rs.next ()) {

Oracle.Sql.Clob Clob = (Oracle.Sql.Clob) Rs.getClob ("Content");

IF (clob! = null) {

Reader is = clob.getcharacterstream ();

BufferedReader Br = New BufferedReader (IS);

String s = br.readline ();

While (s! = null) {

Content = s "

"

S = Br.Readline ();

}

}

Out.println (Content);

%>

Second, the encoding problem

Because Java developers are foreigners, they are not very good for Chinese support, this is a lot of people who have a lot of headache, that is, the Chinese character code issues, about some Chinese character encoding, I will Not much to say, I mainly talk about some small problems when connecting to the Oracle database, but these small problems are very headache.

1. The Chinese problem inserted into the database is to be converted to the encoding

2, read from the database to the Chinese to convert into encoding

Let's take a look at an encoded Java code:

//Ecov.java

Import java.io.unsupportedEncodingexception;

Public Class EcoV

{

PUBLIC STATIC STRING ASC2GB (String ASC) {

String ret;

IF (ASC == Null) Return ASC;

Try {

RET = New String (asc.getbytes ("ISO8859_1"), "GB2312");

}

Catch (unsupportedencodingexception e) {

RET = ASC;

}

Return Ret;

}

Public Static String GB2ASC (String GB) {

String ret;

IF (GB == NULL) RETURN GB;

Try {

RET = New String (GB.GetBytes ("GB2312"), "ISO8859_1");

}

Catch (unsupportedencodingexception e) {

RET = GB;

}

Return Ret;

}

Public static int Byte2int (byte b) {

Return ((-1) >>> 24) & b;

}

}

In fact, this code means that two methods are combined.

Use ecov.gb2asc (arg) when performing a database insertion, to use ecov.asc2gb (arg). One of the most important points is that Oracle seems to know only the format of ISO8859_1 (I am just my idea).

Third, some small details

1, it is setAutoCommit (True or false), which is usually used in SQLUS. If you use true, don't use commit (), or use a commit () method.

2. Treatment to date types, in fact, he is not imaginary, getdate () is then simple, there is a large vulnerability in the middle. Everyone will feel a lot of fun.

3. It is best to use the connection pool technology in the database, using a standard J2EE environment, using simple JNDI technology, is a good method.

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

New Post(0)