The Java environment is:
jdk1.8 java Version "1.8.0_171"
1. Installing the MySQL Jar
: http://dev.mysql.com/downloads/connector/j/
When I downloaded it, I couldn't open it, and the blue Light opened quickly. Blue lights are sometimes not available.
Supported versions of JAR:
After the download is complete, Eclipse is required to add the jar package: You can put the jar package into the project
2. Create a database:
CREATE TABLE' student ' (' ID ')int( One) not NULL, ' name 'varchar(255)DEFAULT NULL, ' age 'int( One)DEFAULT NULL, PRIMARY KEY(' id ')) ENGINE=MyISAMDEFAULTCHARSET=UTF8MB4;
3. Writing Java code
Packagetest;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement; Public classMysqldemo {Final StaticString jdbc_driver = "Com.mysql.cj.jdbc.Driver"; Final StaticString Db_url = "JDBC:MYSQL://LOCALHOST:3306/MV?SERVERTIMEZONE=UTC"; Final StaticString USER = "root"; Final StaticString PASS = "root"; Public Static voidMain (string[] args)throwsClassNotFoundException, SQLException {Connection conn=NULL; Statement stmt=NULL; Class.forName (Jdbc_driver); System.out.println ("Connect Database ..."); Conn=drivermanager.getconnection (Db_url, USER, PASS); //Execute Querystmt =conn.createstatement (); String SQL= "SELECT * FROM Student"; ResultSet RS=stmt.executequery (SQL); //Expand result Set while(Rs.next ()) {intid = rs.getint ("id"); String name= rs.getstring ("name"); intAge = Rs.getint ("Age"); System.out.println ("ID:" +ID); System.out.println ("Name:" +name); System.out.println ("Age:" +Age ); } }}
Execution Result: Success
4. Problems encountered:
4.1 The server time zone value '? й??????? ' is unrecognized or represents more than one time zone
Solve:
Jdbc:mysql://LOCALHOST:3306/MV?SERVERTIMEZONE=UTC
4.2 Loading class ' Com.mysql.jdbc.Driver '. This is deprecated. The new driver class is ' com.mysql.cj.jdbc.Driver '. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary
Resolution: The original driver has been modified
Final Static String jdbc_driver = "Com.mysql.cj.jdbc.Driver";
Java Connection Database