[Database Study Notes] (1) JDBC driver category and Study Notes jdbc

Source: Internet
Author: User

[Database Study Notes] (1) JDBC driver category and Study Notes jdbc











There are several methods for jdbc to load database drivers:

Type 4: Local protocol driver Type 1: jdbc-odbc bridge Jdbc-odbc bridge is provided by sun and is a standard api provided by jdk. this type of driver actually transmits all jdbc calls to odbc, and then odbc calls the local database driver code. (the local database driver code refers to the binary code library for database operations provided by the database vendor. for example, in oracle for windows, it is an oci dll file) jdbc-odbc bridge | odbc | manufacturer's DB code --------------- database Server can access almost all databases using the jdbc-odbc bridge as long as the local machine is equipped with the relevant odbc driver, the jdbc-odbc method is feasible for applications with odbc driver on the client. however, since jdbc-odbc first calls odbc and then odbc calls the local database interface to access the database. therefore, the execution efficiency is relatively low, which is not suitable for applications with large data access. in addition The method requires that the odbc driver be installed on the client. Therefore, it is not suitable for internet-based and intranet-based applications. because, you cannot ask all customers to find the odbc driver. ===== generally, ACCESS is usually used for beginners. The actual project is unnecessary. Type 2: Local Api drives local api drivers to convert jdbc calls to standard database calls and then access the database. this method requires local database driver code. local api driver | vendor's DB code ------------- Database Server (Figure 2) is much more efficient than the jdbc-odbc bridge. however, it still needs to load the library provided by the database vendor on the client. this is not suitable for internet-based applications. in addition, the execution efficiency is not high enough for jdbc drivers of the 3 or 4 type. type3: network protocol-driven drivers are actually built based on the layer-3 structure we are familiar. jdbc first transmits the access request of the local database to the middleware server on the network. the middleware server then translates requests into database-compliant calls and then sends these calls to the database server. if the middleware server uses java, you can also use the jdbc driver on the middle layer as the method for accessing the database. network Protocol Driver --------- middleware Server ---------- database server because this driver is based on the Server. therefore, it does not need to load the library provided by the database vendor on the client. in addition, it is better in terms of execution efficiency and scalability. because most of the functions are implemented on the server side, this type of driver can be designed very small and can be quickly loaded into the memory. however, this kind of driver still needs to be configured with other database drivers in the middleware layer, and the execution efficiency is not the best because there is an additional middle layer for data transmission. type4 local protocol driver this driver directly converts jdbc calls to requests that comply with the relevant database system specifications. because 4-type drive write applications can communicate directly with the database server. this type of driver is fully implemented by java, thus realizing platform independence. local protocol driver --------- the database Server does not need to pass the jdbc call to odbc, local database interface or intermediate layer Server because of this driver. the execution efficiency is very high. moreover, it does not need to load any software or driver on the client or server. these drivers can be dynamically downloaded. however, different drivers need to be downloaded for different databases. the Jdbc-odbc bridge is more suitable for developing applications because of its low execution efficiency. It is also suitable for beginners who know about jdbc programming. for applications that require large data volume operations, type 2, 3, and 4 drivers should be considered. for intranet applications, you can consider type 2 drivers, but because type 3 and 4 drivers are running ...... remaining full text>

Here is an example: Driver A, data source name B, user name C, password D, database table T, and JDBC to retrieve all data in table T.

Package dao;

Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. SQL. Statement;

Public class Test {
Private String className = "";
Private String url = "B ";
Private String user = "C ";
Private String password = "D ";
Private Connection connection;
Private Statement statement;
Private ResultSet resultSet;

Public Connection getConn (){
Try {
Class. forName (className );
Connection = DriverManager. getConnection (url, user, password );
} Catch (ClassNotFoundException e ){
// TODO automatically generates catch Blocks
E. printStackTrace ();
} Catch (SQLException e ){
// TODO automatically generates catch Blocks
E. printStackTrace ();
}
Return connection;
}

Public ResultSet executeQuery (String SQL ){
Connection = getConn ();
Try {
Statement = connection. createStatement ();
ResultSet = statement.exe cuteQuery (SQL );
} Catch (SQLException e ){
// TODO automatically generates catch Blocks
E. printStackTrace ();
}
Return resultSet;
}

Public static void main (String [] args ){
Test test = new Test ();
ResultSet set = test.exe cuteQuery ("select * from T ");
}
}

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.