JDBC common class and method (pick)

xiaoxiao2021-03-06  61

First, four drivers concept A, JDBC-ODBC Bridge bridge type driver, the features of this type of driver must be installed in advance on the user's computer, and then pass the JDBC-ODBC call method, In turn access the database through ODBC. As part of JDK1.1, it is part of Sun.jdbc.odbc package Application ---> JDBC-ODBC Bridge ----> JDBC-ODBC Library ---> ODBC Driver -> Database Applicable to fast prototype System, no JDBC-driven database such as Access B, JDBC-Native API Bridge is also one of the bridge drivers, which must first install a specific driver (similar to ODBC) first on the user's computer, then Through the conversion of the JDBC-Native API bridge, convert the Java API call to a modulation method of a particular driver, and then access the database. Use the developers to communicate directly with the database. Application ---> JDBC Driver ----> Native Database Library ----> Database is slightly better than Class A. C. JDBC-MIDDLEWARE This type of driver is the biggest advantage of provoking any troubles installed on the user's computer, just installing Middleware on the server side, and MiddleWare will be responsible for all necessary conversions necessary. Application ---> JDBC Driver ----> Java MiddleWare ---> JDBC Driver ----> Database has the greatest flexibility, usually provided by those non-numerous manufacturers, the smallest in four types. D, Pure JDBC Driver This type of driver is the most mature JDBC driver, which not only does not need to install any additional drivers on the user's computer, and do not need to install any intermediary program (MiddleWare) on the server side, all access databases. The operation is done directly by the driver. Application ---> JDBC Driver -----> Database Engine ---> Database's highest performance, communicate directly with the database engine through your local protocol, with the ability to assemble in Internet. Second, the commonly used JDBC class and method 1, DriverManager class: responsible for managing JDBC drivers. Before using the JDBC driver, you must first load the driver and register to the DriverManager before you can use, and provide a method to establish a connection to the database. Method: A, class.Forname (String driver); // Load registration driver B, Static Connection getConnection (String Url, String User, String Password "THROWS SQLEXCEPTION; / / Getting Connection C, Static Driver GetDriver (STRING) URL) THROWS SQLEXCET; / / Search in the driver that has been registered with the DriverManager, a driver 2 that opens the database specified by the URL 2, the Connection class is responsible for maintaining the JSP / Java database program and the database online. Three very useful class objects can be established.

Method: A, Statement createStatement () throws SQLException; // build Statement class object Statement createStatement (int resultSetType, int resultSetConcurrency) throws SQLException; // build Statement class object resultSetType value TYPE_FORWARD_ONLY result set is not scrollable rolling TYPE_SCROLL_INSENSITIVE result set, does not reflect the The database change type_scroll_sensitive result set can be scrolled, reflecting the change of the database ResultSetConcurrency value concur_read_only Cannot Update the result set Update Data JDBC2.0 supports the rolling result set, and you can update B, DatabaseMetadata GetMetata () throws SQLException; // build DatabaseMetaData class object C, PreparedStatement prepareStatement (String sql) throws SQLException; // build PreparedStatement class object D, boolean getAutoCommit () throws SQLException // returns the class object AutoCommit Connection state E, void setAutoCommit ( Boolean AutoCommit) THROWS SQLEXCEPTION / / Set the AutoCommit status f of the Connection class object, Void Commit () throwxception // determines how to perform new, delete, or modify the record of the database G, void rollback () throws sqlexception // cancel execution Add, delete, or modify the records of the record H, Void Close () THROWS SQLEXCEPTION / / End Connection object online i, Boolean isclosed () THROWS SQLEXCEPTION / / Test Whether the Connection class object has been turned off on the database 3, the Statement class provides the method provided by the Statement class, using the standard SQL command, add, delete, or modify the database directly, delete or modify the operation method: A, RESULTSET EXECUTEQUERY String SQL) THROWS SQLEXCEPTION // Use the select command to query B, int executeUpdate (String SQL) throws SQLEXCEPTION // use INSERT / Delete / Update to add, delete, and modify the database. C, void close () THROWS SQLEXCEPTION / / End the Statement class object Online 4, the PREPAREDSTATEMENT PREPAREDSTATEMENT class, and the Statement class is different in the preparedState product class, and the incoming SQL command is in advance, when there is When a single SQL instruction is performed multiple times, use the PreparedStatement class more efficient than the Statement class: a, resultSet executeQuery () THROWS SQLEXCEEQUERY () THROWS SQLEXCEPTION // Search B, int ExecuteUpdate () THROWS SQLEXCEUPDATE () THROWS SQLEXCEPTION // Using INSERT / Delete / Update new, delete, and modify the database.

C, ResultSetMetaData getMetaData () throws SQLException // ResultSet class object acquired information about the fields D, void setInt (int parameterIndex, int x) throws SQLException // set the integer value type parameter to the IN-E PreparedStatement class objects, void setFloat (int parameterIndex, float x) throws SQLException // set values ​​to floating point numbers PreparedStatement iN parameter of the object class F, void setNull (int parameterIndex, int sqlType) throws SQLException // set NULL value type to an object class PreparedStatement iN Parameter g, void setstring (int parameterIndex, string x) THROWS SQLEXCEPTION / / Set string type value to the IN parameter h of the PreparedStatement class object, void setdate (int parameterIndex, date x) throws sqlexception // set the date type value to PREPAREDSTATEMENT INPORT I, VOID SETTIME (INT ParameterIndex, Time X) THROWS SQLEXCEPTION / / Setting Time Type Value Give the PREPAREDSTATEMENT class object's IN parameter 5, the DatabaseMetadata class DatabaseMetadata class saves all the characteristics of the database, and provides many ways To achieve this. Methods: A, String getDatabaseProductName () throws SQLException // get the database name B, String getDatabaseProductVersion () throws SQLException // get the database version, code-named C, String getDriverName () throws SQLException // get the name of the JDBC driver D, String getDriverVersion ( "THROWS SQLEXCEPTION / / The version code E, String geturl () THROWS SQLEXCEPTION / / STRING GETUSERNAME () THROWS SQLEXCEPTION / / THROWS SQLEXCEPTION / / THROWEXCEPTION / / THROWS SEM 6, ResultSet class is responsible Store the result of the query database. And provide a range of methods to add, delete, and modify the database. It is also responsible for maintaining a record pointer (CURSOR), and the record pointer points to a record in the data table, and can enhance the efficiency of the program with the appropriate mobile record pointer.

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

New Post(0)