Typical questions about Java + Oracle Application Development

Source: Internet
Author: User

Problem 1: to load the JDBC driverProgram:

Normally, there are three ways to load the driver:

1) class. forname (string) is used to initialize the static content of the driver when loading a class specified by a string in classloader. In fact, the driver class calls drivermanager. registerdriver (driver) method.

2) Use System attributes: system. getproperty (). Load (New fileinputstream ("Property file "));
The advantage of specifying JDBC. Driver = drivername in the property file is that multiple JDBC files can be loaded at the same time, and you do not need to access Java when changing the database.Source code, Only modifying the attribute file.

3) Direct registerdriver (driver) is the most reliable and can be used in any environment.
1) The method is simple, but the JVM of Ms cannot be correctly initialized. For example, you cannot use IE in the applet. You should use the 3) method. But 3) the method is not as flexible as 2), which can be considered based on the overall environment.

Question 2: Large Object Storage

In general, Large Object Storage stores files in the database. Of course, large strings in the memory can also be used. Of course, files like images are stored in binary format. There are many misunderstandings here. Tutorial 99% on the network is not feasible. Even Sun's own documents are always incorrect, although the errors are very small. Binary files should be stored as blob, but jbdc2 cannot directly store blob into binary files. If you do this, you will get an IO instead of an SQL exception, it took me nearly two hours to figure it out.

If you want to store a binary file in Oracle, you need to use the long row type to use standard JDBC:

Create Table tb_file (name varchar (20), detail long row );

Then

File file = new file ("aaa.gif ");
Int filelength = (INT) file. Length ();
Inputstream fin = new fileinputstream (File );
Preparedstatement pstmt = con. preparestatement ("insert into tb_file values('aaa.gif ',?) ");
Pstmt. setbinarystream (1, FIN, filelength );
Pstmt.exe cuteupdate ();

If you must use blob storage, you must use the Oracle method:

Create Table tb_file (name varchar (20), detail BLOB );
Con. setautocommit (false );
Stmt.exe cuteupdate ("insert into tb_file values('aaa.gif ', empty_blob ())");

In the following example, You must select to get the BLOB Object and then write it:

Rs = stmt.exe cutequery ("Select Detail from tb_file where name1_'aaa.gif 'For upfdate ");
If (Rs. Next ())
{
Blob blob = Rs. getblob (1 );
Binaryoutputstream out = (Oracle. SQL. Blob) BLOB). getbinaryoutputstream ();
Byte [] B = new byte [(Oracle. SQL. Blob) BLOB). getbuffersize];
Inputstream fin = new fileinputstream (File );
Int Len = 0;
While (LEN = fin. Read (B ))! =-1)
Out. Write (B, 0, Len );
Fin. Close ();
Out. Close ();
Con. Commit ();
}

You cannot read data like long row.

Inputstream in = Rs. getbinaryinputstream ("detail ");

To

Blob blob = Rs. getblob ("detail ");
In = blob. getbinarystream ();

Question 3: scrollable result set

Oracle explicitly states that results set scrolling is not supported. Therefore, we can use JDBC to obtain a scrollable result set that is supported by JDBC itself, that is, the result set must be highly cached in the memory, many developers mistakenly think it is supported by databases. But they do not actually query a large number of rows. If they really query a large number of rows, they are definitely dead !!!!!! For data with a large amount of rows, you are willing to return to it in a stupid way and do not use a scrollable result set.

 

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.