Mysql as an example to explain the JDBC database connection steps _mysql

Source: Internet
Author: User
Tags rollback stmt try catch

1. What is JDBC and what is the effect?

Java Data Base Connectivity Java Database Connection protocol

is a Java API for executing SQL statements that provides uniform access to a variety of relational databases.

He provides a benchmark by which more advanced tools and interfaces can be built to enable developers to write database applications

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

In other words, with the JDBC API you don't have to write a program specifically to access the Oracle database.

or write a program specifically for accessing the MySQL database.

2. What steps does Java app use to connect to a database and process data with JDBC?

Add the path where the driver package is located to the Classpath class path variable

(such as: Copy Mysql-connector-java-5.1.5-bin.jar to D:\javasoft, and then "D:/javasoft/mysql-connector-java-5.1.5-bin.jar" Add to classpath variable value)

Steps:

1) Load Driver Drive

2 Create a database connection object connection

3 Create a Transfer object that can send SQL commands to the database and return the results statement

4 Execute SQL command and process return result

5 after processing, if there is a return result set, close the result set object resultset

6) Close the corresponding Statement object

7) Close the Connection object connection

3, Case analysis 1

1. Loading drive Com.mysql.jdbc.Driver is the path
class.forname ("Com.mysql.jdbc.Driver") of driver class;  


2, create the database connection object
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String url = "Jdbc:mysql://localhost:3306/test"; LocalHost indicates that this machine 3306 is the default port test for the database name
String username = "root";  Defines the username and password of the connection database
String password = "root";
conn = Drivermanager.getconnection (Url,username,password);

Copy Code code as follows:
3. Create a Transfer object that can send SQL commands to the database and return the result statementstmt = Conn.createstatement ();

Copy Code code as follows:
4. Transfer the SQL command through the SQL Transfer object statement to the database execution, and return the result string sql = "SELECT * from user"; rs = stmt.executequery (SQL);

executequery () is used to execute a query statement that returns a result set, which is actually a table that satisfies the query criteria

The RS result set uses pointers to specify which data is currently

Calls the next method, the pointer points to the next data, and returns True if there is data

5, processing result set while
(Rs.next ()) {
  println (rs.getstring ("username"));
  Pringln (Rs.getint ("password"));
}

There is no declaration to throw an exception, you need to put it in a try catch, or you can throw it, if you put it in a try, catchsqlexception ...

Finally, to close the Conn stmt RS, because these are external resources of the JVM, are closed manually, just like Io, which is not within the scope of the JVM's management

4, Case analysis 2 (add to the deletion)

Add, modify, delete all belong to the data operation, and the data query is different:

No query results, no need to use resultset

The execution method is executeupdate () not executequery ()

The Executeupdate () method also has a return value, but not a resultset, but an int, which indicates how many data have been updated, and generally does not handle

5. What is a business?

is a set of atomic database operations, referring to a set of database operations, either successful or unsuccessful.

A classic example: transfer

Transfer 100.00 to 100002 accounts from 100001 accounts:
Update account1 set money=money-100.00 where code= ' 100001 ';
Update account1 set money=money+100.00 where code= ' 100002 ';

How do I manage the transaction?

Automatically committed by default, first to be manually submitted for transaction management

When the program is working, the last call to the connection object's commit method for the transaction commits

Call the Connection object's Rollback method for transaction rollback when an exception occurs

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.