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