This article mainly describes several tips for connecting Oracle databases through JDBC, and connecting JDBC with a Java Database) APIs are a series of practical interfaces that allow Java programmers to access the database correctly, different developers have different interfaces. After using JDBC from Oracle for many years, I have accumulated many skills that allow us to better utilize the system performance and implement more functions.
1. Use the Thin driver in client software development
In terms of Java software development, Oracle database provides four types of drivers, two types of client software for application software, applets, servlets, and so on, the other two types are used for server software such as Java stored procedures in databases. In the development of client software, we can choose the OCI driver or Thin driver. The OCI driver uses the Java localized interface JNI) to communicate with the database through the Oracle client software.
The Thin driver is a pure Java driver that communicates directly with the database. To achieve the highest performance, Oracle recommends using the OCI driver in client software development, which seems to be correct. However, I recommend using the Thin driver because multiple tests show that the performance of the Thin driver is usually higher than that of the OCI driver.
2. Disable the automatic submission function to improve system performance.
The connection to the database is established for the first time. By default, the connection is in the automatic submission mode. To achieve better performance, you can disable the automatic submission function by calling the setAutoCommit () method of the Connection class with the Boolean value false parameter, as shown below:
- conn.setAutoCommit(false);
It is worth noting that once the automatic commit function is disabled, we need to manually manage transactions by calling the commit () and rollback () Methods of the Connection class.
3. Use the Statement object in dynamic SQL statements or commands with time restrictions
When executing SQL commands, we have two options: You can use the PreparedStatement object or the Statement object. No matter how many times you use the same SQL command, PreparedStatement only parses and compiles it once. When a Statement object is used, it is parsed and compiled every time an SQL command is executed.
This may make you think that using a PreparedStatement object is faster than using a Statement object. However, my tests show that this is not the case in client software. Therefore, in SQL operations with time restrictions, unless SQL commands are processed in batches, we should consider using the Statement object.
In addition, using the Statement object makes it easier to write dynamic SQL commands, because we can connect strings together to create a valid SQL command. Therefore, I think the Statement object can simplify the creation and execution of dynamic SQL commands.
4. Use the helper function to format dynamic SQL commands
When creating a dynamic SQL command executed using the Statement object, we need to solve some formatting problems. For example, if we want to create an SQL command to insert the name 'Reilly into the table, we must replace the "'" In 'Reilly with the two connected. The best way to do this is to create a helper method to complete the replacement operation, and then use the created helper method when the connection string heart uses a public table to execute an SQL command.
Like this, we can let the helper method accept a value of the Date type, and then let it output a string expression based on the Oracle to_date () function.
5. Use the PreparedStatement object to improve the overall efficiency of the database
When you use the PreparedStatement object to execute an SQL command, the command is parsed and compiled by the database, and then placed in the command buffer zone. Then, every time you execute the same PreparedStatement object, it will be parsed again, but will not be compiled again. Pre-compiled commands can be found in the buffer and can be reused.
In enterprise-level applications of a large number of users, the same SQL command is often executed repeatedly. Reducing the number of compilations caused by PreparedStatement objects can improve the overall performance of the database. If the time required for creating, preparing, and executing a PreparedStatement task on the client is longer than that required by the Statement task, we recommend that you use the PreparedStatement object in all situations except the dynamic SQL command.
6. Use the PreparedStatement object in batch processing repeated insert or update operations
If insert and update operations are processed in batches, the required time can be significantly reduced. The Statement and CallableStatement provided by Oracle do not really support batch processing. Only the PreparedStatement object actually supports batch processing.
We can use the addBatch () and executeBatch () Methods to select a standard JDBC batch, or use the setExecuteBatch () method of the PreparedStatement object and the standard executeUpdate () method () method: select an Oracle proprietary method that is faster. To use the dedicated Batch Processing Mechanism of Oracle, you can call setExecuteBatch () as follows ():
- PreparedStatement pstmt3D null;
- try {
- ((OraclePreparedStatement)
- pstmt).setExecuteBatch(30);
- ...
- pstmt.executeUpdate();
- }
When setExecuteBatch () is called, the specified value is an upper limit. When this value is reached, SQL command execution is automatically triggered. Standard executeUpdate () the method will be sent to the database as a batch. You can call the sendBatch () method of the PreparedStatement class to transfer a batch task at any time.
7. Use the Oracle locator method to insert and update the LOB object)
The PreparedStatement class of Oracle does not fully support processing large objects such as BLOB and CLOB. In particular, the Thin driver does not support setting BLOB values using the setObject () and setBinaryStream () Methods of the PreparedStatement object, you cannot use setCharacterStream () to set the CLOB value.
Only the locator method can obtain the value of the LOB type from the database. You can use the PreparedStatement object to insert or update LOB, but you need to use locator to obtain the value of LOB. Because of these two problems, we recommend that you use the locator method to insert, update, or obtain the value of LOB.
8. Use SQL92 syntax to call the Stored Procedure
When calling a stored procedure, we can use SQL92 or Oracle PL/SQL. Since Oracle PL/SQL does not have any practical advantages, in addition, it will cause trouble for developers who will maintain your application in the future. Therefore, I suggest using SQL92 when calling the stored procedure.
9. Use Object SQL to transfer the Object mode to the database
Since the Oracle database can be used as an object-oriented database, you can consider converting the object-oriented mode in the application to the database. The current method is to create Java Beans as disguised database objects, map their attributes to the relational table, and then add methods to these beans. Although this is not a problem in Java, operations are performed outside the database, so other database access applications cannot use the object mode.
If you use Oracle's object-oriented technology, you can create a new database object type to simulate its data and operations in the database, and then use tools such as JPublisher to generate your own Java bean class. If you use this method, not only can Java applications use the object mode of application software, but other applications that need to share data and operations in your application can also use the object mode of application software.
10. use SQL to perform database operations
The most important experience I would like to introduce to you is to make full use of SQL's collection-oriented method to address database processing needs, rather than using Java and other procedural programming languages.
If a programmer needs to search for many rows in a table, each row in the result searches for data in other tables. Finally, the programmer creates an independent UPDATE command to UPDATE the data in the first table in batches. Similar tasks can be completed in an UPDATE command by using multi-column subqueries in the set clause. When a task can be completed in a single SQL command, why stream data online? I suggest that you carefully learn how to maximize the SQL function.
Article by: http://database.csdn.net/page/3886fb36-00d1-4d44-8dca-bf029782cb93