Implement MySql database connection in java

Source: Internet
Author: User

1. Achieve MySql database connection.

The Code is as follows:

Packagetest;

Importjava. SQL. DriverManager;

Importjava. SQL. ResultSet;

Importjava. SQL. SQLException;

Importjava. SQL. Statement;

Publicclassdb {

/**

* @ Paramargs

*/

Publicstaticvoidmain (String [] args ){

// TODOAuto-generatedmethodstub

Stringdriver = "com. mysql. jdbc. Driver"; // method of definition

Stringpath = "jdbc: mysql: // localhost: 3306/buybook"; // define the path (address)

Stringuser = "root"; // define the user name

Stringpwd = "123"; // define the password

ResultSetrt;

Connectioncnn = null;

Statementstatement;

Try {

Class. forName (driver); // obtain the database driver

Cnn = DriverManager. getConnection (path, user, pwd); // obtain the database connection

Statement = cnn. createStatement (); // create an executor

Stringsql = "select * frombooks"; // Add an SQL statement;

Rt1_statement.exe cuteQuery (SQL); // execute the SQL statement and generate the result set

While (rt. next () {// cyclically output the result set

System. out. print (rt. getString ("bno "));

System. out. print ("");

System. out. println (rt. getString ("bname "));

}

} Catch (ClassNotFoundExceptione ){

// TODOAuto-generatedcatchblock

E. printStackTrace ();

} Catch (SQLExceptione ){

// TODOAuto-generatedcatchblock

E. printStackTrace ();

}

Finally

{

Try {

Cnn. close ();

} Catch (SQLExceptione ){

// TODOAuto-generatedcatchblock

E. printStackTrace ();

}

}

}

}

Required knowledge points:

1. next () method: move the cursor backward

2. The DriverManager object (or class) is required for obtaining the Connection object. The getConnection () method of DriverManager creates a connection object through the database Connection url, database username, and password.

3. The Connection interface is located in the java. SQL package.

4. The ResultSet interface is located in the java. SQL package.

5. The Statement interface is located in the java. SQL package.

6. By default, only one ResultSet object can be opened for each Statement object at a time.

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.