Oracle database and JSP connections attention detail

Source: Internet
Author: User
Tags commit

How to deal with the large types of CLOB and blobs

CLOB can be used to store large text data, storing up to 4GB of data, and is more common in application development. Java provides a corresponding Sql.clob class. It provides two methods to read the CLOB data:

Getcharacterstream () method returns the input stream encoded by Unicode (Java.io.Reader object)

Getasciistream () method returns an ASCII-encoded input stream (Java.io.InputStream object)

So if you have the possibility of storing Chinese characters in your database, you should use the previous method.

Now for a practical example, let me learn how to use CLOB step-by-step.

First, create a table with CLOB fields:

CREATE TABLE Test (ID INTEGER, content clob);

Next, we use JSP to insert a record into this table and then get it displayed.

Insert Action:

The above points to note are:

1 CLOB type of data can not be directly insert, you must first through the Empty_clob () method to assign a locator (in the same way, the Blob in the Empty_blob () function to assign locator). Then select it (of course it has no data at this time). , but the result set is not empty), get a Clob object, modify the contents of the object so that it meets our needs, and then update the row record through the Update method.

2 Be sure to lock the row (implemented by the FOR UPDATE keyword) When you modify a record with LOB type by SELECT, or Oracle will complain.

3 The record just inserted on select for update, there will be a "violation of Read order" error, the solution is to set the Autocommit function to False, that is, do not allow autocommit, and then commit it, then select, you can. This is the function of//* two lines in the above code.

Next, we read the record just inserted from the database and display it:

II. Coding Issues

Because Java developers are foreigners, so their support for Chinese is not too good, which makes a lot of us feel very headache, that is, we say through the encoding problem, about some of the encoding norms I do not say, I mainly talk about the Oracle database connection when some of the small problems, But these little problems are a lot of headaches.

1, in the insert into the database in the Chinese problem to convert to code

2. Read Chinese from the database and convert it into code.

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 put the two methods into the same.

To use ECOV.GB2ASC (ARG) when making a database insert, use ECOV.ASC2GB (ARG) when reading. One of the key points is that Oracle only knows the code for iso8859_1 this format (just my idea).

Three or one minor details

1, is Setautocommit (true or false), is our common commit () in the Sqlplus function, if it is true, do not use commit (), or use the commit () method.

2, the date type of processing, in fact he is not imagined setdate (), getDate () So simple, the middle of a big loophole. You will feel a lot of fun when you have more debugging.

3, in and the database is best to use the connection pool technology, with the standard Java EE environment, with simple Jndi technology, is a good way.

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.