Several problems on application and 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. Ordinarily, the binary should be stored as a BLOB type, but JBDC2 is not able to deposit 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, you need to use the long row type with standard JDBC:

CREATE TABLE Tb_file (name varchar, detail long row);

And thenFile 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(20),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 the inside:

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();
}

Also read the data you can't be like long row.

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

But to

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. Just they don't really query a lot of lines, if really query a lot of line words 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.