Optimize JDBC programming-more comments

Source: Internet
Author: User

Optimize JDBC Programming
This is based on ms SQL SERVER 2000 JDBC DRIVER HELP and is compiled by referring to other materials. This Help File of ms is really disappointing. The sample code is very good... if you are interested, you can download idea. Due to my limited level, please criticize and correct any mistakes in this article.
1. Minimize the use of database metadata Methods
Similarly, a ResultSet object is generated. The DatabaseMetaData object method is slower than other JDBC methods. Therefore, the system performance is reduced when this method is used in the same way.
In the program, the generated result set information should be cached at a high speed. For example, the result set returned by getTypeInfo () should be stored in Vector or Hashtable, which can greatly improve the program efficiency.
2. method call modes to be avoided
When calling a method, try to avoid passing null as a parameter. Although the execution can be successful, it is very heavy on the DB Server. In fact, in many cases, the required parameters are known. For example:
// The capture Violation Code is omitted here (the same below ).
DatabaseMetaData md = ...;
ResultSet rs = md. getTables (null, null, "authors", null); // retrieves information about the authors table in the ms SQL SERVER pubs database.
It should be written:
ResultSet rs = md. getTables ("northwind", "dbo", "authors", new String [] {"TABLE "});
This makes the program more effective and reliable.
3. Use a dummy query statement to obtain table-related feature information.
A Dummy Query statement (Dummy Query) does not generate a result set with records. For example, select * from tableName where 1 = 0, because the condition will never be true, DB Server will not execute this statement. Therefore, without the need to generate record rows, the dumb query can greatly improve the execution efficiency of the program. For example, to understand the column information of a table, the preceding statement is much more efficient than the select * from tableName statement, the latter database server needs to retrieve all rows and return a record set, but the former does not. To solve this problem, you can use either of the following methods:
Case 1: Use the getColumns () method
// GetColumns () is a DatabaseMetaData method. For more information, see JDK1.3.

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.