JDBC Connection MySQL Database

Source: Internet
Author: User

Before using JDBC to connect to a MySQL database, download the JDBC MySQL driver first: Http://pan.baidu.com/s/1pL0XqsV

After downloading the extract, create a new Lib folder in the Java project created by Eclipse, and then copy the extracted Mysql-connection-java-5.1.7-bin.jar files to the Lib folder.

Then right-click on the mysql-connection-java-5.1.7-bin.jar---> select build path----> Click Add to Build Path. The referenced libraries file will then appear in the project directory to indicate that the driver was added successfully.

To start connecting to a database, there are generally 2 steps to connecting a database:

(1) Load the database driver and register it in the drive management (DriverManager) class

(2) connecting to a database

1  Packagecom.db;2 3 ImportJava.sql.DriverManager;4 Importjava.sql.SQLException;5 6 Importcom.mysql.jdbc.Connection;7 8  Public classdbhelper{9         //database, port number, databaseTen     PrivateString Dburl = "Jdbc:mysql://localhost:3306/dormitorymanager"; One         //Database user name A     PrivateString dbUser = "root"; -         //Database Login Password -     PrivateString Dbpassword = "123456"; the         //database-driven pinning -     PrivateString jdbcname = "Com.mysql.jdbc.Driver"; -      -     //connecting to a database +      PublicConnection Getconn () { -          +Connection conn =NULL;//Create a Connection connection object A         Try { atClass.forName (Jdbcname);//load the database driver and register it in the Drive management list -}Catch(ClassNotFoundException e) { - e.printstacktrace (); -         } -         Try { -                         //connecting to a database inconn =(Connection) drivermanager.getconnection (Dburl,dbuser,dbpassword); -}Catch(SQLException e) { to e.printstacktrace (); +         } -         returnConn;  the     } *}

JDBC Connection MySQL Database

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.