Introduction to JDBC Basic operations

Source: Internet
Author: User

One. Introduction to JDBC's main API

JDBC provides developers with a standard set of APIs that are classes and interfaces written by the Java language. Used to connect to the database and execute SQL statements. JDBC is also part of the Java Core Class library, located under the java.sql package.
The core APIs that JDBC contains are mainly the following:
DriverManager: Managing a set of JDBC drivers
Connection:java connection of a program to a specific database
Statement: Executes a static SQL statement and returns the object to which it produces results
PreparedStatement: The object of a precompiled SQL statement
CallableStatement: An interface for executing SQL stored procedures
Resultset: Represents the result set of a database query

Two. Procedures for using the JDBC API

(1) To register the API driver, there are two ways:

Class.forName (String drivername);

Example: New Com.mysql.jdbc.Driver () creates a driver class object

(2) Establishing a connection to the SQL database

Use the static method of DriverManager: Getconnection () method to obtain the connection connection. The Getconnection () method is defined as follows:

Connection getconnection (string url, string user, string pwd).

(3) Get the SQL executor
Using Connection's Createstatement () method to get the statement object, statement can execute the SQL statement and get the result of SQL query. The Createstatement () method is defined as follows:

Statement createstatement ().

(4) Execute SQL statement to get result set

Execute SQL query statement

ResultSet executeQuery (String sql)

Execute UPDATE statements, such as Insert,delete,update

int executeupdate (String sql)

Execute Create and DROP statements

boolean execute (String sql)

(5) Get query results
ResultSet can be used to obtain query results, the commonly used methods are:
Boolean next () returns False when there are no rows
String getString () returns the value corresponding to the column name

Introduction to JDBC Basic operations

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.