Several classic issues developed by Java + Oracle application

zhaozj2021-02-16  72

Question 1: If you load the JDBC driver:

There are three ways to load the driver normal:

1) Class.Forname (String) This is desired to initialize the static content of the driver when the ClassLoader is set, in which the driver class is called when the driver class is called.

2) Use system properties: system.getProperty (). Load (New fileInputStream); specifying JDBC.Driver = drivername in the properties file is that you can load multiple JDBC simultaneously, do not have access to databases Java source code, just modify the properties file.

3) Direct RegisterDriver (driver) is the most reliable and can be used in any environment. 1) Simple method, but the JVM of MS cannot initialize correctly. For example, when using IE, you cannot use it in the applet. But 3) The method is not as good as 2) in flexibility, and can be considered according to the environment.

Question 2: Large object storage

In general, large object storage is to save the file to the database, of course, the large string in memory. For documents such as pictures, it is of course used in binary storage. There are many misunderstandings. The tutorial on the network is not in, even Sun own documents have been wrong, although the mistakes are small. According to the binary, it should be stored as a BLOB type, but JBDC2 cannot be directly stored in binary files. If you do this, you will get an IO instead of SQL anomaly, spend this, I spent nearly two hours.

If you want to store a two-file file into Oracle, you will use the long row type with a standard JDBC:

Create Table TB_File (Name VARCHAR (20), Detail Long Raw;

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 store with blob, you must use Oracle's own way:

Create Table TB_File (Name Varchar (20), Detail Blob); Con.SetAutocummit (False); Stmt.executeUpdate ("INSERT INTO TB_FILE VALUES (" AAA.GIF ', EMPTY_BLOB ()) ")

Below you must select SELECT to get the object of the blob again to write:

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 ();} Same data you and Can't like Long Row

InputStream IN = rs.getbinaryInputStream ("Detail");

But

Blob blob = rs.getblob ("detail"); in = blob.getbinaryStream ();

Question 3: Scrollable result set

ORACLE clearly explains that the result set is not supported, then we use JDBC to get a scrollable result set is supported by JDBC, that is, the result set is highly cache in memory, and many developers are wrong to think that it is database support. of. Just don't really query a lot of lines, if you really query, you must be dead! ! ! ! ! ! For ultra-large amounts of data, you are willing to return to it.

转载请注明原文地址:https://www.9cbs.com/read-17055.html

New Post(0)