Notes for connecting ORACLE databases to JSP

Source: Internet
Author: User
Notes for connecting ORACLE databases and JSP-general Linux technology-Linux programming and kernel information. For more information, see the following. I have used ORACLE9i, oracle8 for a long time. I wrote out the issues that need attention in JSP for your reference only.

I. How to deal with large types of Clob and BLOB
CLOB can be used to store enlarged text data and store up to 4 GB of data, which is common in application development. SQL provided by java. corresponding to the Clob class. it provides two methods to read Clob data:
The getCharacterStream () method returns the unicode-encoded input stream (java. io. Reader object)
The getAsciiStream () method returns the input stream (java. io. InputStream object) encoded in ASCII format)
Therefore, if your database may store Chinese characters, you must use the previous method.
Here is a practical example. Let me learn how to use CLOB step by step.
First, create a table containing the CLOB field:
Create table test (id INTEGER, content clob );

Next, we insert a record to this table through JSP, and then obtain and display it.
Insert operation:


Note the following points:
1) clob data cannot be directly inserted. You must first use the empty_clob () method to allocate a locator (similarly, blob uses the empty_blob () function to allocate locator ). then select it (at this time, it certainly does not have data, but the result set is not empty), get a Clob object, modify the content of this object to make it meet our needs, then update the row record using the update method.

2) When modifying a record containing the lob type through the select statement, the row must be locked (implemented using the for update keyword). Otherwise, oracle will report an error.

3) when a newly inserted record is selected for update, a "read order violation" error may occur. The solution is to set the automatic submission function to false, that is, automatic submission is not allowed, then commit it, and then select it. this is the role of the two lines/* in the above Code.

Next, we will read the newly inserted records from the database and display them:


Ii. coding problems
Because JAVA developers are foreigners, their support for Chinese is not very good. This makes us feel a lot of headaches, that is, the problem of Chinese character encoding that we mentioned, I will not talk much about the Chinese character encoding standards. I will mainly talk about some minor issues in connecting to the oracle database, but these minor issues are a headache.

1. Chinese characters inserted into the database must be converted to encoding.
2. Reading Chinese from the database must be converted to encoding.

Let's look at a coded 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, the meaning of this Code is to combine the two methods.
ECov. gb2asc (arg) is used for database insertion, and ECov. asc2gb (arg) is used for reading ). One of the most critical points is that Oracle seems to only know the encoding in the ISO8859_1 format (just my idea ).


Iii. Small details

1. setAutoCommit (true or false) is a common commit () function in sqlPlus. If true is used, do not use commit (). Otherwise, use commit () method.
2. The processing of date types is not as simple as setDate () and getDate (), but there are major vulnerabilities in the middle. You may have a lot of fun to debug it yourself.
3. It is best to use the connection pool technology in the database and the standard J2EE environment and the simple JNDI technology, which is a good method.
Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.