1. Java的数据库编程:JDBC
JDBC:Java 通过JDBC这样的技术来操作 MySQL
MySQL 是一个基于 C/C++ 实现的数据库。
本身也提供了一系列的 API (Application Progromming Interface),让程序员调用,从而通过代码来操作数据库,也可以理解为 ” 一组类/一组方法“。
- 编程语言,如Java,C、C++、Python等
- 数据库,如Oracle,MySQL,SQL Server等
- 数据库驱动包:不同的数据库,对应不同的编程语言提供了不同的数据库 API ,如:MySQL提供了Java的驱动包 mysql-connector-java,需要基于Java操作MySQL即需要该驱动包。同样的,要基于Java操作Oracle数据库则需要Oracle的数据库驱动包ojdbc。
2. JDBC工作原理
3. JDBC使用
3.1 驱动包的下载与导入
下载地址:中央仓库
- 下载MySQL驱动包
- 搜索MySQL驱动包
- 找到和自己的mysql大版本对应的驱动包
- 点击 jar 下载
- 查看
- 导入 jar 包到项目中使用
- 新建目录
- 通过 CTRL + C 和 CTRL + V 来拷贝到目录中
- 告诉IDEA,当前这个目录是存放第三方库的目录
- 这就是展开的效果,代表IDEA已经识别到了驱动包
那么就可以编写代码了!!!
3.2 JDBC使用步骤(插入)
- 首先,想编写JDBC,还需要准备好数据库和数据表(虽然 JDBC 也可以进行建表操作,但是一般都是提前创建好)
创建数据源对象,数据源对象就描述了要访问的数据库是什么和数据库位置在哪里
- 建立数据库连接
每种数据库都会提供对应的类来实现DataSourse接口
- 让代码和数据库服务器建立连接
- 解决方式:
3. 创建操作命令
// 3. 构造操作数据库语句的 sql语句
/**
* PreparedStatement "预编译的语句"
* 将字符串语句的对象转换成sql语句
*/
String sql = "insert into test values(1,'张三')";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
- 发给数据库服务器和释放资源
//4. 执行sql,把刚才解析好的语句发给数据库服务器
/**
* 返回值是int类型,表示这个操作影响了几行数据
*/
int n = preparedStatement.executeUpdate();
System.out.println("n = "+n);
//5. 执行完毕,有收尾操作,释放前面创建的各种资源
// 主要释放 语句对象 和连接对象,DataSource 是不必释放的
/**
* 资源释放
* 释放资源的顺序,要和创建资源的顺序相反
* 建立连接/创建语句的时候,就需要消耗一定的内存/硬盘/网络等资源
* 这些资源,都是跟随这个连接来走的
* 如果连接不再使用了,就需要把这些资源释放掉
*/
preparedStatement.close();
connection.close();
- 但是要实现用户自行输入怎么办
// 3. 构造操作数据库语句的 sql语句
/**
* PreparedStatement "预编译的语句"
* 将字符串语句的对象转换成sql语句
*/
System.out.print("请输入学号:");
Scanner scanner = new Scanner(System.in);
int id = scanner.nextInt();
System.out.print("请输入姓名:");
String name = scanner.next();
//String sql = "insert into test values(1,'张三')";
String sql = "insert into test values("+ id +",'"+ name +"')";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
这种拼接字符串的做法可以,但是不推荐
- 影响可读性,引号忒多了
- 写法不安全,可能引起 sql 注入攻击这样的漏洞
- 建议这样写:
String sql = "insert into test values(?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,id);
preparedStatement.setString(2,name);
4. JDBC修改删除查询
(1)修改:
import com.mysql.cj.jdbc.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class JDBCDemo2 {
public static void main(String[] args) throws SQLException {
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/mysqltest?characterEncoding=utf8&useSSL=false");
((MysqlDataSource) dataSource).setUser("root");
((MysqlDataSource) dataSource).setPassword("1234");
Connection connection = dataSource.getConnection();
Scanner scanner = new Scanner(System.in);
System.out.print("请输入需要修改的学号:");
int id = scanner.nextInt();
System.out.print("请输入需要修改的姓名:");
String name = scanner.next();
String sql = "update test set name = ? where id= ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,name);
preparedStatement.setInt(2,id);
int n = preparedStatement.executeUpdate();
System.out.println("n = "+n);
preparedStatement.close();
connection.close();
}
}
(2)删除:
import com.mysql.cj.jdbc.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class JDBCDemo3 {
public static void main(String[] args) throws SQLException {
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/mysqltest?characterEncoding=utf8&useSSL=false");
((MysqlDataSource) dataSource).setUser("root");
((MysqlDataSource) dataSource).setPassword("1234");
Connection connection = dataSource.getConnection();
Scanner scanner = new Scanner(System.in);
System.out.print("请输入需要删除的学号:");
int id = scanner.nextInt();
String sql = "delete from test where id = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,id);
int n = preparedStatement.executeUpdate();
System.out.println("n = "+n);
preparedStatement.close();
connection.close();
}
}
(3)查询:
import com.mysql.cj.jdbc.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCDemo4 {
public static void main(String[] args) throws SQLException {
// 创建一个数据源对象,用于连接MySQL数据库
DataSource dataSource = new MysqlDataSource();
// 设置数据库连接的URL,包括数据库地址、端口、数据库名以及编码和SSL设置
((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/mysqltest?characterEncoding=utf8&useSSL=false");
// 设置数据库连接的用户名
((MysqlDataSource) dataSource).setUser("root");
// 设置数据库连接的密码
((MysqlDataSource) dataSource).setPassword("1234");
// 通过数据源获取数据库连接
Connection connection = dataSource.getConnection();
// 定义SQL查询语句,查询test表中的所有数据
String sql = "select * from test";
// 创建PreparedStatement对象,用于执行SQL语句
PreparedStatement preparedStatement = connection.prepareStatement(sql);
// 执行查询操作,返回结果集
ResultSet resultSet = preparedStatement.executeQuery();
// 遍历结果集,获取查询结果
while (resultSet.next()){
// 获取结果集中当前行的id列的值
int id = resultSet.getInt("id");
// 获取结果集中当前行的name列的值
String name = resultSet.getString("name");
// 打印查询结果
System.out.println("id = " + id + " name = " + name);
}
// 关闭结果集,释放资源
resultSet.close();
// 关闭PreparedStatement,释放资源
preparedStatement.close();
// 关闭数据库连接,释放资源
connection.close();
}
}
/**
* id = 2 name = 李四
* id = 3 name = 王五
*/
- 如果是要使用条件查询也是可以的
import com.mysql.cj.jdbc.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class JDBCDemo4 {
public static void main(String[] args) throws SQLException {
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/mysqltest?characterEncoding=utf8&useSSL=false");
((MysqlDataSource) dataSource).setUser("root");
((MysqlDataSource) dataSource).setPassword("1234");
Connection connection = dataSource.getConnection();
Scanner scanner = new Scanner(System.in);
System.out.print("请输入需要查询的学号:");
int id = scanner.nextInt();
String sql = "select * from test where id = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,id);
ResultSet n = preparedStatement.executeQuery();
while (n.next()){
int i = n.getInt("id");
String name = n.getString("name");
System.out.println("id = "+i+" name = "+name);
}
n.close();
preparedStatement.close();
connection.close();
}
}
/**
* 请输入需要查询的学号:3
* id = 3 name = 王五
*/