Java connects MySQL database with JDBC

Source: Internet
Author: User
Tags driver manager

Get to know the common interface of JDBC in the first

1. Driver Interface Driver

The driver for each database should provide a class that implements the Java.sql.Driver interface, referred to as the driver class. Typically, the driver class to connect to the database is loaded through the static method forname (String className) of the Java.lang.Class class, and the parameter is the full package name of the driver class to be loaded.

For example: Connect MySQL database: Class.forName ("Com.mysql.jdbc.Driver");

2. Driver Manager DriverManager

After successfully loading a driver class successfully registered in the DriverManager class, the DriverManager class can use the Getconnection () method to establish a database connection.

Common methods (The following are static methods, that is, directly using the class name can be directly used):

Getconnection (String url,string user,string password): Get database connection

setLoginTimeout (int seconds): Sets the maximum waiting time for each waiting database to be connected

Setlogwriter (Java.io.PrintWrite out): Used to set the log output object

println (String message): Used to output the specified message to the current JDBC log stream

3. Database Connection Interface Connection

Responsible for connecting to a particular database, you can execute SQL statements and return results in a connected context, and you can obtain information about the database, such as databases, stored procedures, and connectivity features, through the GetMetaData () method.

Common methods are as follows:

Createstatement (): Creates and returns an statement instance, typically executing a parameterless SQL statement that creates the instance.

Preparestatement (): Creates and returns an instance of Preparestatement, often executing an SQL statement with parameters that is created.

Preparecall (): Creates and returns an Preparecall instance that can call a stored procedure.

Setautocommit (): Sets the auto-commit mode for the current connection instance, with the default bit true, which automatically synchronizes changes to the database, or FALSE to manually synchronize the changes to the database by executing a commit () or rollback () method.

Getautocommit (): To see if the current connection instance is in autocommit mode.

4. Execute the static SQL statement Interface statement

Used to execute a static SQL statement

Common methods:

ExecuteQuery (String SQL): Executes the specified static SELECT statement and returns a ResultSet instance that can never be null as the result set.

Executeupdate (String SQL): Executes a static INSERT,UPDATE,DELETE statement and returns a numeric value of type int, which feeds back the number of records synchronized.

Clearbatch (): Clears all SQL statements that are in batch

5. Execute the dynamic SQL statement interface Preparestatement

6. Execute the stored procedure interface CallableStatement

7. Accessing the result set interface resultset

Java connects MySQL database with JDBC

Related Article

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.