"Analysis of the Java--JDBC"

Source: Internet
Author: User



the Java EE platform consists of a complete set of services ( Services), application interfaces (APIs), and protocol components, which provide functional support for the development of Web-based multi-tier applications, with the core content being the learning and use of 13 technical specifications (APIs and components). Here's a look at the Java database links.


I. Introduction of JDBC

JDBC(Java Data baseconnectivity,java database connection) is a JAVAAPI for executing SQL statements. You can provide unified access to a variety of relational databases, consisting of a set of classes and interfaces written in the Java language. JDBC provides a standard API for tool/database developers to build more advanced tools and interfaces that enable database developers to write database applications with pure Java APIs. The JDBC specification designs the Java Database Programming framework with the idea of interface and implementation separation. The interface is contained in the java.sql and javax.sql packages. The implementation classes for these interfaces are called database drivers.

      Second,theJDBC driver

JDBC drivers are the individual database manufacturers based on JDBC specifications produced by the JDBC implementation classes, consisting mainly of four types:


1 , the first type : implementation is through JDBC are all delegated to their programming interfaces, such as ODBC . This type of driver requires the installation of a local code base, which relies on local programs, so portability is poor. such as the jdbc-odbc Bridge driver.


2. The second type : implementation is partly based on the Java language. That is, part of the driver is written in the Java language, the other part of the local database to delegate the client code to implement. Like the driver of type 1, this type of driver also relies on local programs, so portability is poor


3. The third type : implementation is all based on the Java language. This type of driver is usually provided by a middleware server so that the client program can communicate using a database-independent protocol and a middleware server, and the middleware server forwards the client's JDBC call to the database for processing


4, the fourth kind : realization is all based on the Java language. This type of driver contains the access protocol for a particular database, allowing the client to communicate directly with the database.


iii.jdbc Programming steps

for JDBC The programming steps are roughly as follows:


1. Load Database driver

Class.forName (Driverclass), the above Dirverclass is the database driver class corresponding to the classpath string, according to different database vendors to provide different drivers.


2. get a link to a database via DriverManager

drivermanager.getconnection (stringurl, stirng user, String Pass), when using DriverManager to get links, You need to pass in three parameters: the URL of the data volume, the user name, and the password.

3. Through The Connection object Creates a statement object, and connection creates the statement in the following three ways:

createstatement () creates a basic statement object. preparestatement (String sql): Creates a precompiled statement object based on the incoming SQL statement. preparecall (String sql): Create CallableStatement object based on incoming SQL statement

4, Statement Execute SQL statement, statement has three methods to execute SQL statement:

Execute: Can execute any SQL statement, single more troublesome

executeupdate: You can execute DML, DDL statements. The execution DML returns the number of rows affected by the SQL statement, and the execution DDL returns 0;

executeQuery: Only query statements can be executed, and after execution returns a representation of the query results The ResultSet object.

5, the operation result set, for ResultSet

The main move pointer and get the value of Next,Previous, first, last, beforefrist, Afterlast, Absolute and other methods of moving pointers. getxxx Gets the value of the move pointer to the row, specific column, index. Using the column name as the parameter to get the value is good readability, using the index as the parameter to get good performance.

Iv. JDBC &odbc

speaking of JDBC , we will first think ODBC . The database links we contacted were all configured in ODBC , andMicrosoft's ODBC API might be the most widely used programming interface for accessing the relational database. It can connect almost all of the databases on almost any platform.


However, why Java to use JDBC it? Because ODBC uses the C language interface, it is not intended to be used directly in Java. Calling local C code from Java has many drawbacks in terms of security, implementation, robustness, and automatic consistency of programs. Literal translation from the ODBC C API to the Java API is undesirable. And if you are using ODBC, you must manually install the ODBC Driver Manager and drivers on each client computer. If you write the JDBC driver completely in Java, the JDBC code can be automatically installed, ported, and secured on all Java platforms, from network computers to mainframes.


"Analysis of the Java--JDBC"

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.