简单建表
CREATE DATABASE jdbc;
CREATE TABLE users (
id INT PRIMARY KEY ,
`NAME` VARCHAR(40),
`password`VARCHAR (40),
email VARCHAR (60),
birthday DATE
);
INSERT INTO users (id,`NAME`,`password`,`email`,`birthday`)
VALUES (1, '张三','123456','za@qq.com','2001-01-01');
INSERT INTO users (id,`NAME`,`password`,`email`,`birthday`)
VALUES (2, '李四','123456','za@qq.com','2001-01-01');
INSERT INTO users (id,`NAME`,`password`,`email`,`birthday`)
VALUES (3, '王五','123456','za@qq.com','2001-01-01');
INSERT INTO users (id,`NAME`,`password`,`email`,`birthday`)
VALUES (4, '赵六','123456','za@qq.com','2001-01-01');
INSERT INTO users (id,`NAME`,`password`,`email`,`birthday`)
VALUES (5, '钱七','123456','za@qq.com','2001-01-01');
SELECT * FROM users;
利用maven加载Jar包
Java连接
点击Database
选择Data source
选择 MySQL
如果没有右侧边栏,点击左下角
设置User和password
选定要使用的表
编写Java
运行结果
使用预编译
```sql
```sql
```sql
```java
package com.kuang;
import java.sql.*;
public class test02 {
public static void main(String[] args) throws Exception {
String url="jdbc:mysql://localhost:3306/JDBC?useSSL=false&serverTimezone=UTC";
String name = "root";
String password = "245960";
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//连接数据库,代表数据库
Connection connection = DriverManager.getConnection(url,name,password );
//编写sql
String sql ="insert into users(id ,NAME,password,email,birthday) values (?,?,?,?,?)";
//预编译
PreparedStatement preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1,10);
//给第一个占位符?的赋值为1
preparedStatement.setString(2, "jie");
//给第二个占位符?赋值为jie
preparedStatement.setString(3,"123456");
//给第三个占位符?赋值为123456
preparedStatement.setString(4,"2459604063@qq.com");
//给第四个占位符?赋值为2459604063@qq.com
preparedStatement.setDate(5,new Date(new java.util.Date().getTime()));
//给第五个占位符?赋值为
//执行sql
int i = preparedStatement.executeUpdate();
if (i>0){
System.out.println("插入数据成功");
}
//关闭连接,释放资源
preparedStatement.close();
connection.close();
}
}