Use of the environment
- jdk 1.8
- mysql 8.0
- idea 2019
Specific process
1, custom installation mysql, you need to check Connector / J.
You can then install directory C:\Program Files (x86)\MySQL\Connector J 8.0
to find the next
mysql-connector-java-8.0.15.jar
jar package, that is connected to the driver java mysql.
2, will be copied to the jre lib directory driver.
That is, mysql-connector-java-8.0.15.jar
copied to
C:\Program Files\Java\jdk1.8.0_211\jre\lib
in
3, using the idea to create a new empty ordinary java project
Then File -> Project Structure, or Ctrl+Alt+Shift+S
open the window as shown below:
4, a new data table in the database
/*
Navicat MySQL Data Transfer
Source Server : @localmysql
Source Server Version : 80015
Source Host : localhost:3306
Source Database : javademo
Target Server Type : MYSQL
Target Server Version : 80015
File Encoding : 65001
Date: 2019-05-28 16:25:05
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`age` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', '张三', '165');
INSERT INTO `student` VALUES ('2', '历史', '45');
INSERT INTO `student` VALUES ('3', '浮点', '32');
INSERT INTO `student` VALUES ('4', '法法', '54');
5, the preparation of the database connection java code
package com.tuweiwei;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class demo01 {
public static void main(String[] args) {
// Connection 是java.sql包下的类,表示数据库连接
Connection conn;
// 驱动程序名
String driver = "com.mysql.cj.jdbc.Driver";
// 指定要连接的数据库,这里我的数据库名为 javademo
String jdbcUrl = "jdbc:mysql://localhost:3306/javademo?serverTimezone=UTC";
// 数据库用户名
String userName = "root";
// 数据库密码
String userPwd = "111111";
try {
// 加载驱动程序
Class.forName(driver);
// 连接数据库
conn = DriverManager.getConnection(jdbcUrl, userName, userPwd);
if (!conn.isClosed()) {
System.out.println("Succeeded connecting to the Database!");
}
// java.sql包的Statement类,表示数据库的语句
Statement statement = conn.createStatement();
// 真实的SQL代码
String sql = "select * from t_test_user";
// java.sql包的ResultSet类,表示数据库的查询结果集
ResultSet rs = statement.executeQuery(sql);
String name = null;
String age = null;
while (rs.next()) {
name = rs.getString("name");
age = rs.getString("age");
System.out.println(name+"\t"+age);
}
rs.close();
conn.close();
}
catch (Exception e){
System.out.println(e);
}
}
}
Problems encountered
1, the driver package is installed mysql com.mysql.jdbc.Driver be changed after 8.0 com.mysql.cj.jdbc.Driver
2、java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time
Area Error prompt system can execute commands in mysql:
SET Global TIME_ZONE = '+ 8:00'
or add serverTimezone = UTC parameter in the database-driven url