MySQL is used as an example to explain the JDBC database connection steps.

Source: Internet
Author: User

MySQL is used as an example to explain the JDBC database connection steps.

1. What is JDBC? What is the function?

Java Data Base Connectivity Java database connection protocol

It is a Java API used to execute SQL statements and provides unified access to multiple relational databases.

He provides a benchmark to build more advanced tools and interfaces so that developers can write database applications.

With JDBC, it is easy to send SQL statements to various relational data.

In other words, with the jdbc api, you do not need to write a program to access the Oracle database.

Or write a program for accessing the MySQL database.

2. How does the Java APP use JDBC to connect to the database and process data?

Add the path of the driver package to the CLASSPATH class PATH variable.

(For example, copy the mysql-connector-java-5.1.5-bin.jar to D: \ Documents oft and add D:/Documents oft/mysql-connector-java-5.1.5-bin.jar to the classpath variable value)

Steps:

1) load Driver

2) create a database Connection object Connection

3) create a transfer object Statement that can send SQL commands to the database and return results

4) execute the SQL command and process the returned results

5) after processing, if a returned result set exists, disable the ResultSet object.

6) Close the corresponding Statement object

7) Close the Connection object

3. Case study 1

// 1. Load the driver com. mysql. jdbc. driver is the path of the Driver Class. forName ("com. mysql. jdbc. driver "); // 2. Create a database Connection object Connection conn = null; Statement stmt = null; ResultSet rs = null; String url =" jdbc: mysql: // localhost: 3306/test "; // localhost indicates that the default port 3306 on the local machine is test, and the database name String username =" root "; // define the username and password for database connection String password = "root"; conn = DriverManager. getConnection (url, username, password );

Copy codeThe Code is as follows: // 3. Create a transfer object Statementstmt = conn. createStatement () that can send SQL commands to the database and return results ();

Copy codeThe Code is as follows: // 4. the SQL command transmits the object Statement through SQL to the database for execution, and returns the result String SQL = "select * from user "; rs = stmt.exe cuteQuery (SQL );

ExecuteQuery ()Used to execute a query statement. The returned result set is actually a table that meets the query conditions.

The rs result set uses a pointer to specify the current data.

Call the next method, and the Pointer Points to the next data. If there is data, true is returned.

// 5. processing result set while (rs. next () {println (rs. getString ("username"); pringln (rs. getInt ("password "));}

The exception is not declared here. You need to put it in try catch or throw it. If you put it in try, you need to put catchSQLException...

Finally close conn stmt rs because these are JVM external resources and must be manually disabled like IO, which is not within the JVM management scope.

4. Case study 2 (add, delete, and modify)

Adding, modifying, and deleting data operations are different from data query operations:

No query results, no need to use ResultSet

The execution method uses executeUpdate () instead of executeQuery ()

The executeUpdate () method also has a return value, but it is not a ResultSet, but an int, which indicates how many data records have been updated, which is generally not processed.

5. What is a transaction?

It is a group of atomic database operations. It means that all operations in a group of databases are successful or fail.

A classic example: Transfer

Transfer RMB 100001 from a 100.00 account to a 100002 account:
Update account1 set money = money-100.00 where code = '000000 ';
Update account1 set money = money + 100.00 where code = '000000 ';

How to manage transactions?

By default, it is automatically submitted. To manage a transaction, you must set it to manual submission.

When the program runs normally, the Connection object commit method is called to commit transactions.

When a program encounters an exception, call the rollback method of the Connection object to roll back the transaction.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.