Java connection MySQL database three ways

Source: Internet
Author: User
Tags driver manager

Haven't updated the blog for a long time! Today, using day, I learned a bit about MySQL database. Introduction to the database of three ways to connect!

Development tools: MyEclipse

MySQL5.6

MySQL Connection driver: Mysql-connector-java-5.1.27.jar

Load driver:

1. Create the Lib folder in the project catalog and place the downloaded JDBC in the folder as shown in:

2. Right-click the project name, select Add JARs in the Libraries page in Java build path ..., select the JDBC you just added, such as:

You can also "right-click" in the Project and select "Build Path".

Packet Preparation:

In the database DAY17 create the following data table EMP:

CREATE TABLE EMP (   empno            INT(4)          PRIMARYKEY,   ename             varchar($),   job              varchar(9),   HireDate         DATE,   sal              FLOAT(7,2));

Add Data:

Connect to the database and read the data:

Database name: DAY17

Packet Name: EMP

Port number: 3306

User name: Root

Password: root

1  PackageGz.itcast.a_jdbc;2 3 Importjava.sql.Connection;4 ImportJava.sql.Driver;5 ImportJava.sql.DriverManager;6 Importjava.util.Properties;7 8 Importorg.junit.Test;9 /**Ten * JDBC Connection database One  * @authorAPPle A  * -  */ -  Public classDemo1 { the     //URL of the connection database -     PrivateString url = "Jdbc:mysql://localhost:3306/day17"; -                         //JDBC Protocol: Database sub-protocol: Host: Port/Connected Database// -  +     PrivateString user = "root";//User name -     PrivateString password = "root";//Password +      A     /** at * First Method -      * @throwsException -      */ - @Test -      Public voidTest1 ()throwsexception{ -         //1. Creating a Driver class object inDriver Driver =NewCom.mysql.jdbc.Driver ();//New version -         //Driver Driver = new Org.gjt.mm.mysql.Driver ();//older versions to          +         //set user name and password -Properties props =NewProperties (); theProps.setproperty ("User", user); *Props.setproperty ("Password", password); $         Panax Notoginseng         //2. Connect to the database and return to the Connection object -Connection conn =driver.connect (URL, props); the          + SYSTEM.OUT.PRINTLN (conn); A     } the      +     /** - * Use the Driver Manager class to connect to the database (registered two times, not necessary) $      * @throwsException $      */ - @Test -      Public voidTest2 ()throwsexception{ theDriver Driver =Newcom.mysql.jdbc.Driver (); -         //Driver driver2 = new Com.oracle.jdbc.Driver ();Wuyi         //1. Registering a driver (multiple drivers can be registered) the drivermanager.registerdriver (driver); -         //Drivermanager.registerdriver (driver2); Wu          -         //2. Connect to a specific database AboutConnection conn =drivermanager.getconnection (URL, user, password); $ SYSTEM.OUT.PRINTLN (conn); -          -     } -      A     /** + * (This is the recommended way to connect to the database) the * It is recommended to use the load driver class to register the driver -      * @throwsException $      */ the @Test the      Public voidTest3 ()throwsexception{ the         //Driver Driver = new Com.mysql.jdbc.Driver (); the          -         //registers a driver by loading a static block of code by getting a byte-code object inClass.forName ("Com.mysql.jdbc.Driver"); the          the         //Driver driver2 = new Com.oracle.jdbc.Driver (); About         //1. Registering a driver (multiple drivers can be registered) the         //drivermanager.registerdriver (driver); the         //Drivermanager.registerdriver (driver2); the          +         //2. Connect to a specific database -Connection conn =drivermanager.getconnection (URL, user, password); the SYSTEM.OUT.PRINTLN (conn);Bayi          the     } the  -}

The result of the operation is:

Java connection MySQL database three ways

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.