Registering drivers that drive MySQL

Source: Internet
Author: User

1. Add the driver files to the app project

By copying the driver Mysql-connector-java-5.1.6-bin to the Web application's Web-inf\lib, the Web application can access the MySQL database through the JDBC interface.

2. Load register the specified database driver

For MySQL database, its driver loading format:

Class.forName ("Com.mysql.jdbc.Driver");

JDBC Connection Database Create connection object

First form ' Connect symbol word (URL) ', then use "connection symbol word" to implement the connection and create the Connection object

1. URL of the database connection

String URL 1= "Jdbc:mysql:";

String ur2= "? user=root&password= password";

String ur3= "&useunicode=true&characterencoding=utf-8";

String Url=url1+url2+url3;

2. Connect using the connection symbol Word to get the connection object

A static Connection getconnection (String URL)

B static Connection getconnection (String url,properties info);

C static Connection getconnection (String url,string user,string password):

3. Use JDBC to connect MySQL database, get common format of connection object

Example

Assuming that the MySQL database is "Students", the user name of the database operation is "root", the password is "123456", the code is "Utf-8"

String drivername= "Com.mysql.jdbc.driver";

String username= "Root"

String userpwd= "123456"

String dbname= "Students"

String ur1= "jdbc:mysql;//localhost:3306/" =dbname;

String ur2= "? user=" = "usernanme" = "&password=" =USERPWD;

String url3= "&useunicode=true&characterencoding=utf-8";

String Url=url1+url2+url3

Class.forName (drivername);

Connection conn=drivermanager.getconnection (URL);

Registering drivers that drive MySQL

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.