JDBC 数据库编程基础
进入官网下载页面:https://www.mysql.com/cn/downloads/
以下红色横线标记的为mysql社区版,是免费的mysql版本:
这里笔者的计算机系统为windows,故选择页面下的Download,安装最新版本。
切换到MySQL安装的bin文件目录下,并输入语句,出现Service successfully installed
表明安装成功:
mysqld --install
打开我的电脑->属性->高级->环境变量,:
先新建系统变量,变量值为读者的mysql bin文件夹的路径 (如: D:\mysql\mysql-8.0.22-winx64\bin ),然后点击确定:
mysqld --initialize --console
net start mysql
如果读者出现如下错误:
可能会出现以下问题:
mysqld --console
切换到bin目录下,然后输入以下语句查看端口3306占用情况:
netstat -ano|findstr 3306
可以找到以下端口正在占用:
输入以下语句杀死3306端口的进程,即强制终止指定的进程和由它启用的子进程:
taskkill /pid 5476 -t -f
输入以下语句,然后输入保存的密码,检验mysql是否安装成功:
mysql -u root -p
由于密码复杂,通过dos窗口修改密码为: root( alter user ‘root’@‘localhost’ identified by ’这里为新密码‘ ):
alter user 'root'@'localhost' identified by 'root';
接下来在mysql目录下创建一个ini配置文件,在这里我创建的是ini配置文件,里面写的代码是mysql的一些基本配置:
[mysql]
default-character-set=utf8
[mysald]
port=3306
basedir=D:\mysql\mysql-8.0.22-winx64
datadir=D:\mysql\mysql-8.0.22-winx64\data
character-set-server=utf8
default-storage-engine=INNODB
[client]
default-character-set=utf8
笔者这里以navicat作为mysql图像化工具
百度云链接
提取码:7anc
成功安装后,连接MySQL:
首先测试连接:
成功后填写连接名,这个连接名只是为了自己在本地能快速识别连接的数据库是什么,只是针对电脑的使用者方便而自己起的名字,因此读者可以自定义填写。
新建查询检验:
以下安装适用于还未安装MySQL的情况下
官网下载链接:
https://www.mysql.com/products/connector/
如果已经安装了MySQL的免安装版,可以根据以下链接,直接下载驱动:
百度云链接
提取码:4z60
Eclipse下的MySQL连接过程如下:
新建java project后,右击文件名,点击构建路径,添加至构建路径:
点击Add External JARs:
找到对应jar包,引入,然后点击Apply and Close,最终可以在目录下找到:
到此就算完成了JDBC基础学习的预准备了…
JDBC简介:
JDBC(Java DataBase Connectivity,Java 数据库连接)由一组使用java语言编写的类与接口组成,可以为多种关系数据库提供统一访问。Sun公司提供了JDBC的接口标准 ——JDBC API ,而数据库厂商或第三方中间厂商根据该接口规范提供针对不同数据库的具体实现——JDBC 驱动,比如微软有自己研发的SQLServer数据库,甲骨文有Oracle数据库…这些API接口都是由各大产商的程序员具体实现的。
中途可能出现的警告:
Wed Nov 18 22:27:50 CST 2020 WARN: Establishing SSL
connection without server's identity verification is not recommended.
According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL
connection must be established by default if explicit option isn't set.
For compliance with existing applications not using SSL the
verifyServerCertificate property is set to 'false'. You need either to
explicitly disable SSL by setting useSSL=false, or set useSSL=true
and provide truststore for server certificate verification.
警告解释:
请注意:不建议在没有服务器身份验证的情况下建立SSL连接。如果不设置显式选项,则必须建立默认的SSL连接。您需要通过设置useSSL=false显式地禁用SSL,或者设置useSSL=true并为服务器证书验证提供信任存储
时区问题解决:
增:
package com.wu.JdbcDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class MySQLDemo {
private static final String DATABASE_DRIVER = "com.mysql.cj.jdbc.Driver"; // MySQL驱动
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/test"
+ "?characterEncoding=utf-8&useSSL=false"; // 确定数据库的端口以及数据库
private static final String DATABASE_USER = "root"; // 用户名
private static final String DATABASE_PASSWORD = "root"; // 用户密码
public static void main(String[] args) throws Exception{
Connection conn = null;
Statement stmt = null;
try {
Class.forName(DATABASE_DRIVER); // 注册驱动
conn = DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD); // 连接数据库
String sql = "insert into student(sno,sname,ssex,sage,sdept) values('201512520','张三','男',20,'is')";
stmt = conn.createStatement(); // 获取数据库操作对象
// 专门执行DML语句: insert delete update 返回值是 影响数据库中记录的条数
int count = stmt.executeUpdate(sql); // 执行sql语句
System.out.println("服务器连接: "+conn);
System.out.println(count == 1 ? "添加成功":"添加失败");
}catch(SQLException e) {
e.printStackTrace();
}finally{
// 释放资源
// 遵循从小到大依次关闭
try {
if(stmt != null) {
stmt.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
try {
if(conn != null) {
conn.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}
运行结果:
删:
package com.wu.JdbcDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class MySQLDemo {
private static final String DATABASE_DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/test"
+ "?characterEncoding=utf-8&useSSL=false";
private static final String DATABASE_USER = "root";
private static final String DATABASE_PASSWORD = "root";
public static void main(String[] args) throws Exception{
Connection conn = null;
Statement stmt = null;
try {
Class.forName(DATABASE_DRIVER);
conn = DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD);
stmt = conn.createStatement(); // 获取数据库操作对象
String sql = "delete from student where sname = '张三'";
int count = stmt.executeUpdate(sql);
System.out.println(count==1 ? "删除成功":"删除失败");
}catch(SQLException e) {
e.printStackTrace();
}finally {
try {
stmt.close();
}catch(SQLException e) {
e.printStackTrace();
}
try {
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}
运行结果:
改:
package com.wu.JdbcDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class MySQLDemo {
private static final String DATABASE_DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/test"
+ "?characterEncoding=utf-8&useSSL=false";
private static final String DATABASE_USER = "root";
private static final String DATABASE_PASSWORD = "root";
public static void main(String[] args) throws Exception{
Connection conn = null;
Statement stmt = null;
try {
Class.forName(DATABASE_DRIVER);
conn = DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD);
stmt = conn.createStatement();
String sql = "update student set sname = '赵四',sage = sage+1 where sname = '张三'";
int count = stmt.executeUpdate(sql);
System.out.println(count == 1 ? "修改成功":"修改失败");
}catch(SQLException e) {
e.printStackTrace();
}finally {
try {
if(stmt !=null) {
stmt.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
try {
if(conn != null) {
conn.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}
运行结果:
查:
package com.wu.JdbcDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MySQLDemo {
private static final String DATABASE_DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/test"
+ "?characterEncoding=utf-8&useSSL=false";
private static final String DATABASE_USER = "root";
private static final String DATABASE_PASSWORD = "root";
public static void main(String[] args) throws Exception{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(DATABASE_DRIVER);
conn = DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD);
stmt = conn.createStatement();
String sql = "select sno,sname,ssex,sage,sdept from student";
// 专门执行DQL语句 返回值为查询结果集
rs = stmt.executeQuery(sql);
while(rs.next()) {
String sno = rs.getString("sno");
String sname = rs.getString("sname");
String sage = rs.getString("sage");
String ssex = rs.getString("ssex");
String sdept = rs.getString("sdept");
// 也可以使用以下语句
// String sno = rs.getString(1);
// String sname = rs.getString(2);
// String sage = rs.getString(3);
// String ssex = rs.getString(4);
// String sdept = rs.getString(5);
System.out.println("学号:"+sno+" 姓名:"+sname+" 性别:"+ssex+" 年龄:"+sage+" 专业:"+sdept);
}
}catch(SQLException e) {
e.printStackTrace();
}finally {
try {
if(rs != null) {
rs.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
try {
if(stmt != null) {
stmt.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
try {
if(conn != null) {
conn.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}
运行结果:
Statement在数据库操作中需要一个完整正确的SQL命令,一旦需要通过变量进行内容接收,则需要进行SQL语句的拼接,而这样的代码不方便阅读也不方便维护。所以才使用PreparedStatement
如下:
package com.wu.JdbcDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class MySQLDemo {
private static final String DATABASE_DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/test"
+ "?characterEncoding=utf-8&useSSL=false";
private static final String DATABASE_USER = "root";
private static final String DATABASE_PASSWORD = "root";
public static void main(String[] args) throws Exception{
Connection conn = null;
Statement stmt = null;
String sno = "202011190";
String sname = "王五";
String ssex = "男";
int sage = 19;
String sdept = "cs";
try {
Class.forName(DATABASE_DRIVER); // 注册驱动
conn = DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD); // 连接数据库
String sql = "insert into student(sno , sname , ssex , sage , sdept) values"+
"('"+sno+"','"+sname+"','"+
ssex+ "','"+sage+"','"+
sdept+")"; // 这里漏了一个单引号
stmt = conn.createStatement(); // 获取数据库操作对象
// 专门执行DML语句: insert delete update 返回值是 影响数据库中记录的条数
int count = stmt.executeUpdate(sql); // 执行sql语句
System.out.println("服务器连接: "+conn);
System.out.println(count == 1 ? "添加成功":"添加失败");
}catch(SQLException e) {
e.printStackTrace();
}finally{
// 释放资源
// 遵循从小到大依次关闭
try {
if(stmt != null) {
stmt.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
try {
if(conn != null) {
conn.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}
结果显示:
package com.wu.JdbcDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class MySQLDemo {
private static final String DATABASE_DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/test"
+ "?characterEncoding=utf-8&useSSL=false";
private static final String DATABASE_USER = "root";
private static final String DATABASE_PASSWORD = "root";
public static void main(String[] args) throws Exception{
Connection conn = null;
PreparedStatement pstmt = null; // 这里为 PararedStatement 数据库操作对象
String sno = "202011190";
String sname = "王五";
String ssex = "男";
int sage = 19;
String sdept = "cs";
try {
Class.forName(DATABASE_DRIVER); // 注册驱动
conn = DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD); // 连接数据库
// 使用占位符 ?
String sql = "insert into student(sno , sname , ssex , sage , sdept) values(?,?,?,?,?)";
pstmt = conn.prepareStatement(sql); // 获取数据库操作对象
// 设置索引内容
pstmt.setString(1,sno);
pstmt.setString(2,sname);
pstmt.setString(3,ssex);
pstmt.setInt(4, sage);
pstmt.setString(5, sdept);
int count = pstmt.executeUpdate();
System.out.println(count == 1 ? "添加成功":"添加失败");
}catch(SQLException e) {
e.printStackTrace();
}finally{
// 释放资源
// 遵循从小到大依次关闭
try {
if(pstmt != null) {
pstmt.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
try {
if(conn != null) {
conn.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}
结果显示:
package com.wu.JdbcDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MySQLDemo {
private static final String DATABASE_DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/test"
+ "?characterEncoding=utf-8&useSSL=false";
private static final String DATABASE_USER = "root";
private static final String DATABASE_PASSWORD = "root";
public static void main(String[] args) throws Exception{
Connection conn = null;
PreparedStatement pstmt = null; // 这里为 PararedStatement 数据库操作对象
ResultSet rs = null;
String sdept = "is";
try {
Class.forName(DATABASE_DRIVER); // 注册驱动
conn = DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD); // 连接数据库
// 使用占位符 ?
String sql = "select sno , sname , ssex , sage , sdept from student where sdept = ?";
pstmt = conn.prepareStatement(sql); // 获取数据库操作对象
// 设置索引内容
pstmt.setString(1,sdept);
rs = pstmt.executeQuery(); // 这里执行DQL操作
while(rs.next()) {
String sno = rs.getString("sno");
String sname = rs.getString("sname");
String sage = rs.getString("sage");
String ssex = rs.getString("ssex");
System.out.println("学号:"+sno+" 姓名:"+sname+" 性别:"+ssex+" 年龄:"+sage+" 专业:"+sdept);
}
}catch(SQLException e) {
e.printStackTrace();
}finally{
// 释放资源
// 遵循从小到大依次关闭
try {
if(pstmt != null) {
pstmt.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
try {
if(conn != null) {
conn.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}
结果显示:
package com.wu.JdbcDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
class Student{
public String sno;
public String sname;
public String ssex;
public int sage ;
public String sdept;
public Student(String sno,String sname,String ssex,int sage,String sdept) {
this.sno = sno;
this.sname = sname;
this.ssex = ssex;
this.sage = sage;
this.sdept = sdept;
}
}
public class MySQLDemo {
private static final String DATABASE_DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/test"
+ "?characterEncoding=utf-8&useSSL=false";
private static final String DATABASE_USER = "root";
private static final String DATABASE_PASSWORD = "root";
public static void main(String[] args) throws Exception{
Connection conn = null;
PreparedStatement pstmt = null; // 这里为 PararedStatement 数据库操作对象
Student[] students = {new Student("202011191","李四一","男",19,"ma"),new Student("202011192","李四二","女",19,"ma"),
new Student("202011193","李四三","男",19,"ma"),new Student("202011194","李四四","女",19,"ma"),
new Student("202011195","李四五","男",19,"ma"),new Student("202011196","李四六","女",19,"ma")};
try {
Class.forName(DATABASE_DRIVER); // 注册驱动
conn = DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD); // 连接数据库
// 使用占位符 ?
String sql = "insert into student(sno , sname , ssex , sage , sdept) values(?,?,?,?,?)";
pstmt = conn.prepareStatement(sql); // 获取数据库操作对象
// 设置索引内容
for(Student s : students) {
pstmt.setString(1,s.sno);
pstmt.setString(2,s.sname);
pstmt.setString(3,s.ssex);
pstmt.setInt(4, s.sage);
pstmt.setString(5, s.sdept);
pstmt.addBatch(); // 追加批处理
}
int[] count = pstmt.executeBatch(); // 这里执行批处理MDL操作
System.out.println(Arrays.toString(count));
}catch(SQLException e) {
e.printStackTrace();
}finally{
// 释放资源
// 遵循从小到大依次关闭
try {
if(pstmt != null) {
pstmt.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
try {
if(conn != null) {
conn.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}
结果显示:
ACID简述:指数据库事务正确执行的四个基本要素的缩写。包含:原子性(Atomicity)、一致性(Consistency)、隔离性(Isolation)、持久性(Durability)。一个支持事务(Transaction)的数据库,必须要具有这四种特性,否则在事务过程(Transaction processing)当中无法保证数据的正确性,交易过程极可能达不到交易方的要求。
原子性:整个事务中的所有操作,要么全部完成,要么全部不完成,不可能停滞在中间某个环节。
一致性:是指在数据库操作的前后完全一致的。
隔离性:多个事务可以同时进行且彼此之间无法访问,只有当事务完成最终的操作的时候,才可以观察到结果。
持久性: 在事务完成以后,该事务对数据库所作的更改便持久的保存在数据库之中,并不会被回滚。
使用Statement批处理演示
第一种没有事务回滚的操作:
package com.wu.JdbcDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
public class MySQLDemo {
private static final String DATABASE_DRIVER = "com.mysql.cj.jdbc.Driver"; // MySQL驱动
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/test"
+ "?characterEncoding=utf-8&useSSL=false"; // 确定数据库的端口以及数据库
private static final String DATABASE_USER = "root"; // 用户名
private static final String DATABASE_PASSWORD = "root"; // 用户密码
public static void main(String[] args) throws Exception{
Connection conn = null;
Statement stmt = null;
try {
Class.forName(DATABASE_DRIVER); // 注册驱动
conn = DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD); // 连接数据库
stmt = conn.createStatement(); // 获取数据库操作对象
// Statement 批处理操作
stmt.addBatch("insert into student(sno,sname,ssex,sage,sdept) values('201215130','张三一','女',20,'is')");
stmt.addBatch("insert into student(sno,sname,ssex,sage,sdept) values('201215131','张三二','男',20,'is')");
stmt.addBatch("insert into student(sno,sname,ssex,sage,sdept) values('201215132','张三三','女',20,'is'')"); // 这里多了一个单引号
// 专门执行DML语句: insert delete update 返回值是 影响数据库中记录的条数
int[] count = stmt.executeBatch(); // 执行sql语句
conn.commit(); // 事务提交
System.out.println(Arrays.toString(count));
}catch(SQLException e) {
e.printStackTrace();
}finally{
// 释放资源
// 遵循从小到大依次关闭
try {
if(stmt != null) {
stmt.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
try {
if(conn != null) {
conn.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}
运行结果:
第二种具有事务回滚的操作:
package com.wu.JdbcDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
public class MySQLDemo {
private static final String DATABASE_DRIVER = "com.mysql.cj.jdbc.Driver"; // MySQL驱动
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/test"
+ "?characterEncoding=utf-8&useSSL=false"; // 确定数据库的端口以及数据库
private static final String DATABASE_USER = "root"; // 用户名
private static final String DATABASE_PASSWORD = "root"; // 用户密码
public static void main(String[] args) throws Exception{
Connection conn = null;
Statement stmt = null;
try {
Class.forName(DATABASE_DRIVER); // 注册驱动
conn = DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD); // 连接数据库
conn.setAutoCommit(false); // 这里自动提交事务要设置为false 默认为true
stmt = conn.createStatement(); // 获取数据库操作对象
// Statement 批处理操作
stmt.addBatch("insert into student(sno,sname,ssex,sage,sdept) values('201215133','张三四','女',21,'is')");
stmt.addBatch("insert into student(sno,sname,ssex,sage,sdept) values('201215134','张三五','男',21,'is')");
stmt.addBatch("insert into student(sno,sname,ssex,sage,sdept) values('201215135','张三六','女',21,'is'')"); // 这里多了一个单引号
// 专门执行DML语句: insert delete update 返回值是 影响数据库中记录的条数
int[] count = stmt.executeBatch(); // 执行sql语句
conn.commit(); // 事务提交
System.out.println(Arrays.toString(count));
}catch(SQLException e) {
e.printStackTrace();
conn.rollback(); // 事务回滚
}finally{
// 释放资源
// 遵循从小到大依次关闭
try {
if(stmt != null) {
stmt.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
try {
if(conn != null) {
conn.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}
运行结果:
首先需要在数据库中定义一个过程体:
create procedure search_student(in in_sno varchar(20))
begin
select * from student
where sno = in_sno;
end;
package com.wu.jdbc;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MySQLDemo {
private static final String DATABASE_DRIVER = "com.mysql.cj.jdbc.Driver"; // MySQL驱动
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/test"
+ "?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC"; // 确定数据库的端口以及数据库
private static final String DATABASE_USER = "root"; // 用户名
private static final String DATABASE_PASSWORD = "root"; // 用户密码
public static void main(String[] args) throws Exception{
Connection conn = null;
CallableStatement cs = null;
ResultSet rs = null;
String sno = "201215123";
try {
Class.forName(DATABASE_DRIVER); // 注册驱动
conn = DriverManager.getConnection(DATABASE_URL,DATABASE_USER,DATABASE_PASSWORD); // 连接数据库
String sql = "{call search_student(?)}";
cs = conn.prepareCall(sql); // 获取数据库操作对象
cs.setString(1,sno);
rs = cs.executeQuery();
if(rs.next()) { // 由于sno为主键,最多只有一个
String sname = rs.getString("sname");
String ssex = rs.getString("ssex");
String sage = rs.getString("sage");
String sdept = rs.getString("sdept");
System.out.println("学号:"+sno+" 姓名:"+sname
+" 性别:"+ssex+" 年龄:"+sage+" 专业:"+sdept);
}
}catch(SQLException e) {
e.printStackTrace();
}finally{
// 释放资源
// 遵循从小到大依次关闭
try {
if(rs != null) {
rs.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
try {
if(cs != null) {
cs.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
try {
if(conn != null) {
conn.close();
}
}catch(SQLException e) {
e.printStackTrace();
}
}
}
}
参考博客:https://blog.csdn.net/qq_37915248
参考博客:https://www.cnblogs.com/winton-nfs/p/11524007