Uncover the mysteries of JDBC so that JDBC is no longer mysterious

Source: Internet
Author: User

What is 1.JDBC?

JDBC (Java database Connectivity) Java DB connection

What can 2.JDBC do?

In short, JDBC can do three things: establish a connection to the database, send a statement that operates the database, and process the results.

What is the advantage of 3.JDBC?

Java is a strong, secure, easy-to-use, easy-to-understand and automatically downloadable feature on the Web, and is an excellent language for writing database applications. All that is needed is a way for a conversation between a Java application and a variety of different databases.

4.JDBC implementation?

JDBC defines interfaces
Database Vendor Implementation Interface
The programmer invokes the interface, and the actual invocation is the implementation part of the underlying data vendor.


Implementations generally have several steps:


(1) Driver interface and driver class loading

Different types of databases will have different driver classes.

For example, we can load the Oracle driver class via Class.forName ("Oracle.jdbc.driver.OracleDriver");


(2) Connection interface

The real implementation of the connection interface is done by the driver package provided by the database vendor.

String=url;

Connection conn=drivermanager.getconnection ("URL", "DatabaseName", "password")

Different types of databases will have different URLs.


(3) Statement interface
Statement: Created by Method Createstatement. The statement object is used to send a simple SQL statement.
PreparedStatement: Created by Method Preparestatement. The PreparedStatement object is used to send an SQL statement with one or more input parameters (in parameters). PreparedStatement has a set of methods for setting the value of the in parameter. These in parameters are sent to the database when the statement is executed. The PreparedStatement instances extend the statement, so they all include the statement method. The PreparedStatement object may be more efficient than the statement object because it has been precompiled and stored there for future use.

The Statement interface provides three ways to execute SQL statements: ExecuteQuery, Executeupdate, and execute.

(4) ResultSet interface

Used to process the results of SQL statement execution.

(5) Closing the object

The statement object will be automatically shut down by the Java garbage collection program. As a good programming style, you should explicitly close them when you don't need statement objects. This frees the DBMS resources immediately, helping to avoid potential memory problems.

Uncover the mysteries of JDBC so that JDBC is no longer mysterious

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.