Common JDBC classes and Methods

Source: Internet
Author: User
1. Four driver concepts

JDBC-ODBC Bridge
Bridge driver, this type of driver is characteristic of the user's computer must be installed in advance ODBC driver, and then through the call method of the JDBC-ODBC, and then through ODBC to access the database.
Later than JDK, it is part of the sun. JDBC. ODBC package.
Application ---> JDBC-ODBC bridge ----> JDBC-ODBC library ---> ODBC driver --> Database
It is suitable for fast prototype systems, and does not provide JDBC-driven databases such as Access

B. JDBC-native API Bridge
This type of driver must first install a specific driver (similar to ODBC) on the user's computer, and then use the JDBC-native API bridge to convert, converts a Java API call to a calling method of a specific driver to access the database.
Use the local database provided by the developer to directly communicate with the database.
Application ---> JDBC driver ----> native database library ----> Database
This is slightly better than Class.

C. JDBC-Middleware
The biggest benefit of this type of driver is that it saves the trouble of installing any driver on the user's computer. You only need to install Middleware on the server, middleware is responsible for all necessary conversions to access the database.
Application ---> JDBC driver -----> Java middleware ---> JDBC driver ----> Database
It has the maximum flexibility, which is usually provided by non-database vendors and is the smallest of the four types.

D. Pure JDBC driver
This type of driver is the most mature JDBC driver, not only does not need to install any additional driver on the user's computer, but also does not need to install any mediation Program (middleware) on the server ), all database access operations are directly completed by the driver.
Application ---> JDBC driver -----> Database Engine ---> Database
With the highest performance, you can use your local protocol to directly communicate with the database engine and be able to assemble on the Internet.

Ii. Common JDBC classes and Methods

1. drivermanager class:
Manages JDBC drivers. Before using the JDBC driver, you must load the driver and register it with drivermanager before using it. You can also provide a method to establish a connection to the database.

Method:
A. Class. forname (string driver); // load the registration driver
B. Static connection getconnection (string URL, string user, string password) throws sqlexception;
// Obtain the connection to the database
C. Static driver getdriver (string URL) throws sqlexcetion;
// Find a driver that can open the database specified by the URL in the driver that has been registered with drivermanager

2. Connection class
Maintains the connection between JSP/Java database programs and databases. You can create three very useful class objects.

Method:
A. Statement createstatement () throws sqlexception; // create a statement Class Object
Statement createstatement (INT resultsettype, int resultsetconcurrency) throws sqlexception;
// Create a statement Class Object

Resultsettype Value
The type_forward_only result set cannot be rolled.
The type_scroll_insensitive result set can be rolled and does not reflect database changes.
Type_scroll_sensitive result set can be rolled to reflect database changes

Resultsetconcurrency Value
Concur_read_only cannot update data with the result set
Concur_updatable can use the result set to update data.

In jdbc2.0, the rolling result set is supported, and data can be updated.

B. databasemetadata getmetadata () throws sqlexception; // create a databasemetadata Class Object
C. preparedstatement preparestatement (string SQL) throws sqlexception;
// Create a preparedstatement Class Object
D. boolean getautocommit () throws sqlexception // return the autocommit status of the connection Class Object
E. Void setautocommit (Boolean autocommit) throws sqlexception
// Set the autocommit status of the connection object
F. Void commit () throws sqlexception // confirm the operation to add, delete, or modify records to the database
G, void rollback () throws sqlexception // cancel the operation of adding, deleting, or modifying records to the database
H. Void close () throws sqlexception // end connection object online to the database
I, Boolean isclosed () throws sqlexception // test whether the connection class object has been closed online to the database

3. Statement class

Using the methods provided by the statement class, you can use standard SQL commands to directly add, delete, or modify databases.

Method:

A. resultset executequery (string SQL) throws sqlexception // use the SELECT command to query the database
B. Int executeupdate (string SQL) throws sqlexception
// USE insert/delete/update to add, delete, and modify a database.
C. Void close () throws sqlexception // end the online connection of the statement Class Object to the database

4. preparedstatement class

The difference between the preparedstatement class and the statement class is that the preparedstatement class object will compile the passed SQL commands for use in advance. When a single SQL command is executed more than once, the preparedstatement class is more efficient than the statement class.

Method:

A. resultset executequery () throws sqlexception // use the SELECT command to query the database
B. Int executeupdate () throws sqlexception
// USE insert/delete/update to add, delete, and modify a database.
C. resultsetmetadata getmetadata () throws sqlexception
// Obtain information about fields of the resultset Class Object
D. Void setint (INT parameterindex, int X) throws sqlexception
// Set the in parameter of the preparedstatement class object to an integer.
E. Void setfloat (INT parameterindex, float X) throws sqlexception
// Set the value of the floating point type to the in parameter of the preparedstatement class object.
F, void setnull (INT parameterindex, int sqltype) throws sqlexception
// Set the null value to the in parameter of the preparedstatement Class Object
G, void setstring (INT parameterindex, string X) throws sqlexception
// Set the value of the string type to the in parameter of the preparedstatement class object.
H, void setdate (INT parameterindex, date X) throws sqlexception
// Set the date type value to the in parameter of the preparedstatement Class Object
I, void settime (INT parameterindex, time x) throws sqlexception
// Set the time type value to the in parameter of the preparedstatement Class Object

5. databasemetadata class

The databasemetadata class saves all the features of the database and provides many methods to obtain this information.

Method:

A. String getdatabaseproductname () throws sqlexception // obtain the Database Name
B. String getdatabaseproductversion () throws sqlexception // obtain the database version code
C, string getdrivername () throws sqlexception // get the JDBC driver name
D. String getdriverversion () throws sqlexception // get the JDBC driver version code
E, string geturl () throws sqlexception // gets the jdbc url of the connected database
F, string GetUserName () throws sqlexception // obtain the user account used to log on to the database

6. resultset class

Stores the query results of the database. It also provides a series of methods to add, delete, and modify databases. It is also responsible for maintaining a record pointer, recording pointer pointing to a record in the data table. by moving the record pointer appropriately, you can access the database as you like and improve program efficiency.

Method:

A, Boolean absolute (INT row) throws sqlexception // move the record pointer to the specified record
B. Void beforefirst () throws sqlexception // move the record pointer to the first record
C. Void afterlast () throws sqlexception // move the record pointer after the last record
D, Boolean first () throws sqlexception // move the record pointer to the first record
E, Boolean last () throws sqlexception // move the record pointer to the last record
F, Boolean next () throws sqlexception // move the record pointer to the next record
G, Boolean previous () throws sqlexception // move the record pointer to the previous record
H. Void deleterow () throws sqlexception // Delete the record pointed to by the record pointer
I, void movetoinsertrow () throws sqlexception // move the record pointer to add a record
J. Void movetocurrentrow () throws sqlexception // move the record pointer to the recorded record
K, void insertrow () throws sqlexception // Add a record to the database
L. Void updaterow () throws sqlexception // modify a record in the database
M, void update type (INT columnindex, Type X) throws sqlexception // modify the value of a specified field
N, int get type (INT columnindex) throws sqlexception // gets the value of the specified field
O, resultsetmetadata getmetadata () throws sqlexception // gets the resultsetmetadata Class Object

7. resultsetmetadata class

The resultsetmetadata class object stores information about fields in all the resultset class objects. It provides many methods to obtain this information.

Method:

A, int getcolumncount () throws sqlexception // gets the number of fields in the resultset Class Object
B. Int getcolumndisplaysize () throws sqlexception // obtain the field length of the resultset Class Object
C, string getcolumnname (INT column) throws sqlexception // obtain the field name of the resultset Class Object
D. String getcolumntypename (INT column) throws sqlexception // gets the field type name of the resultset class object.
E, string gettablename (INT column) throws sqlexception // get the name of the data table to which the field of the resultset Class Object belongs.
F, Boolean iscasesensitive (INT column) throws sqlexception // test whether the fields of the resultset class object are case sensitive.
G, Boolean isreadonly (INT column) throws sqlexception // test whether the field of the resultset class object is read-only

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.