Answers to several common problems in application development of Java+oracle

Source: Internet
Author: User
Tags stmt

Problem one: such as Paugaca JDBC driver:

Normally we have three ways to load drivers:

1) class.forname (string) this would like to be a class specified by a String that initializes the static content of the driver at load time, ClassLoader In fact, the driver class called the Drivermanager.registerdriver (driver) method

2 Use System Properties: System.getproperty (). Load (new FileInputStream ("Property File"));

The advantage of specifying Jdbc.driver=drivername in a property file is that you can load multiple JDBC at the same time, instead of accessing the Java source code while changing the database, just modifying the property file

3) Direct Registerdriver (Driver) This method is most reliable and can be used in any environment.

1) method is simple, but the MS JVM is not properly initialized. For example, the use of IE in the applet can not be used, should use the method of 3. However, the 3 method is less flexible than 2, which can be considered in terms of environment.

Problem two: Large object storage

In general, large object storage is to save files to the database, of course, can also be in memory of the very large strings. For files like pictures, of course, binary storage, there are many misunderstandings, the network tutorial 99% is not workable, even Sun's own documents have been wrong, although the error is very small.   The supposedly binary file should be stored as a BLOB type, but JBDC2 does not store the BLOB directly into the binary file, and if you do, you get an IO instead of an SQL exception, which took me nearly two hours to figure it out.   If you want to deposit a binary file with Oracle, use the standard JDBC you will use the Long row type: Create TABLE tb_file (name varchar, 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.executeupdate (); If you must use BLOB storage, you must use Oracle's own method: Create table Tb_file (name varchar (), detail BLOB);

Con.setautocommit (FALSE);

Stmt.executeupdate ("INSERT into tb_file values (' Aaa.gif ', Empty_blob ())"); The following must select the object that gets the blob and write to it again: rs = stmt.executequery ("Select Detail from Tb_file where name= ' 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 ();

}

Similarly read data you cannot inputstream in = Rs.getbinaryinputstream ("detail") as Long row;

Rather blob blob = Rs.getblob ("detail");

in = Blob.getbinarystream ();

Question three: scrollable result sets

ORACLE explicitly indicates that the result set scrolling is not supported, then we use JDBC to get a scrollable result set that is supported by JDBC itself, that is, the result set is cached in memory, and many developers mistakenly think it is supported by the database. But they do not really query a large number of lines, if the real query a large number of lines must be dead!!!!!! For a large number of rows of data, it is preferred to return to its stupid method also do not use a scrollable result set.

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.