JDBC Connection database, jdbc database

Source: Internet
Author: User
Tags driver manager

JDBC Connection database, jdbc database

JDBC connection to the database is generally divided into three steps:

(1) register the database driver

(2) create a database connection URL

(3) obtain the Connection object

The Code is as follows:

1 try {2 // load the database driver and register it with the driver Manager 3 Class. forName ("com. mysql. jdbc. driver "); 4 // database connection String 5 String url =" jdbc: mysql: // localhost: 3306/mysqltest "; 6 // database userName 7 String userName = "root"; 8 // Database passWord 9 String passWord = "123456"; 10 // create Connection 11 Connection conn = (Connection) driverManager. getConnection (url, userName, passWord); 12 // determine whether the database connection is null 13 if (conn! = Null) {14 // output connection information 15 out. print ("database connection successful! "); 16 // close database connection 17 conn. close (); 18} else {19 // output connection error message 20 out. println (" database connection failed! "); 21} 22} catch (ClassNotFoundException e) {23 e. printStackTrace (); 24}
View Code

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.