The trick is to use JDBC often to handle a lot of repetitive code and capture endless SqlException. It is worth trying to extract these complex, repetitive processes and put them in a reusable JDBC tool class.
The Util class contains a large number of static methods that can assist in the development of a particular domain. So
StringutilClass will contain a
CapitalizeMethod
StreamutilClass may contain
Pushstreammethod, which is used to read from an input stream and write to the output stream.
There are two decisions to make when creating such a set of standard tool classes. First, you have to determine what functions are needed, and second, you must determine what kind of log you need to do.
The second decision is more difficult to surround like
UtilClass is required to use the
System.err, whether to throw a new exception, hide the error, or use a log tool such as log4j. With the release of JDK1.4, we can all use the Java.log package, so it's much easier to answer this question.
For the following example, we output the error to the
System.err。
A useful feature of the JDBC util is
ensureloadedMethod. When writing JDBC code, it is a common requirement to ensure that a specific drive is loaded into memory before the SQL call. This can be done by calling
jdbcutil.ensureloaded (String drivername)The perfect solution.
Note JDBC
UtilClass is named
Jdbcutilis based on Sun's Java code standards, in the class name, the acronym for the first letter is only capitalized.
jdbcutil.ensureloadedThe code is similar:
For Oracle,
jdbcutil.ensureloadedWill Call:
jdbcutil.ensureloaded ("Oracle.jdbc.driver.OracleDriver");
Other common features include the
ResultSetTo an array of objects, from
ResultSetGets the column name, gets the type of a particular column, or gets the primary key name of a table.
A set of generic
UtilClasses can greatly enhance the development process; Remember, however, that the most important thing is not to put all the actions into
Util.javaIn