第一步,创建user表,测试数据库
CREATE DATABASE `jdbcstudy` /*!40100 DEFAULT CHARACTER SET utf8mb3 */ /*!80016 DEFAULT ENCRYPTION='N' */
use jdbcstudy;
CREATE TABLE `users` (
`id` int NOT NULL,
`NAME` varchar(40) DEFAULT NULL,
`PASSWORD` varchar(40) DEFAULT NULL,
`email` varchar(60) DEFAULT NULL,
`birthday` date DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3
第二步,导入数据库依赖
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
第三步,编写测试代码
操作jdbc的执行步骤:
1.加载驱动
2.获取连接对象,连接数据库
3.获取执行sql对象
4.返回结果
5.关闭资源
package org.example;
import java.sql.*;
public class FistDemo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.用户信息和url
String url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&&useSSL=true";
String username = "root";
String password = "123456";
//3.连接成功,数据库对象 Connection代表数据库
Connection connection = DriverManager.getConnection(url, username, password);
//4.执行SQL的对象 Statement 执行SQL的对象
Statement statement = connection.createStatement();
//5.执行SQL的对象去执行SQL
String sql = "select *from users";
ResultSet resultSet = statement.executeQuery(sql);//返回的结果集,结果集中封装了我们全部查询出来的结果
while (resultSet.next()) {
System.out.println("id" + resultSet.getObject("id"));
System.out.println("NAME" + resultSet.getObject("NAME"));
System.out.println("PASSWORD" + resultSet.getObject("PASSWORD"));
System.out.println("email" + resultSet.getObject("email"));
System.out.println("birthday" + resultSet.getObject("birthday"));
System.out.println("=====================================================");
}
resultSet.close();
statement.close();
connection.close();
//6.释放连接
}
}
PS.
DriverManager
我们操作Driver和获取connection对象都是交给DriverManager统一管理,DriverManager可以注册和删除加载的驱动
static {
try {
DriverManager.registerDriver(new Driver());
} catch (SQLException var1) {
throw new RuntimeException("Can't register driver!");
}
}
Connection connection = DriverManager.getConnection(url, username, password);
//connection 代表数据库
//数据库设置自动提交
//事务提交
//事务回滚
resultSet.close();
statement.close();
connection.close();
url
url路径是指定到哪个数据库,并设置SSL(安全套接层)为true
String url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&&useSSL=true";
connection
此时,获取到的connection是含有指定数据库
Connection connection = DriverManager.getConnection(url, username, password);
statement
现在就可以获取编译sql的对象
Statement statement = connection.createStatement();
String sql = "select *from users";
resuleMap
statement.executeQuery(sql); //用于查询语句,返回的是一个结果集
statement.execute(sql);//用于任何一个sql语句
statement.executeUpdate(sql); //用于添加、删除、修改,返回的是一个被影响的行数
ResultSet 查询结果集;封装了所有的查询结果
获得指定的数据类型
resultSet.getObject();//在不知道指定类型的情况下使用
resultSet.getString();
参照狂神说MySQL撰写本篇博客
参照TTiamo_博主的博客撰写