Some questions about developing Java under Oracle

Source: Internet
Author: User
Tags copy insert query stmt
oracle| problem I didn't want to write a tutorial like this, because since it's called a tutorial, it's going to be a history test, and for me, a rookie programmer, I just like to share some of my own experience. Can not be a method, irrelevant to the idea of where to speak. But some of the problems I can not help but to understand, because the current online many "tutorials" are in the beginners to the wrong, some are the author's personal understanding of the error, some authors have never done development but can write a software development article. They will only do translate,copy,cut these operations, the simplest example is the sun's JDK development document so far the method of introducing large object (file) storage is wrong, but said that after N (n >100) of the rewrite is no one to correct, Because later people are just copy a bit, there is no real to do, but the original author to their own name.
(Respect for the statement: Where to Axman, Super Rookie, poetry Sword Scholar signed the article in addition to the CNJSP website posted declined any site)

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 calls Drivermanager.registerdriver (driver);
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 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 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 JDBC2 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 rows and if really query a lot of lines it's definitely 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.

-------------------------------------
Note: In response to the author's request, this article declined any website to paste!

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.