## 一、JDBC驱动的使用方法 #### 1、下载`mysql-connector-java-5.1.10-bin.jar` [香肠狗网盘](https://pan.soki.me/s/vrUj) #### 2、在项目中新建`libs`目录 将`.jar`文件放进`libs`目录中 右键`.jar`包 -> 添加为库 发现其可以展开详细的内容,找到`com.mysql.jdbc.Driver` #### 3、创建驱动Driver对象 在`jdbc`包下新建`DBTest.java`类,创建`query()`方法 ```java package com.doooog.jdbc; public class DBTest { public void query(){ } } ``` 在方法中,需要加载外部库,注意`forName`需要包的全称 ```java try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException) { throw new RuntimeException(e); } ``` 为了方便数据库连接 创建`url`、`username`、`password`三个String类型变量用来存放数据库地址、数据库用户名、数据库密码 ```java String url = "jdbc:mysql://localhost:3306/databaseName?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8"; String username = "username"; String password = "password"; ``` **注意**:`url`中的`jdbc:mysql`是一种数据库连接协议,`localhost`为本机地址等同于`127.0.0.1`,`3306`为数据库的所在的端口,用来进行TCP通讯,`databaseName`为要连接的数据库名称,`useUnicode=true`是使用Unicode编码,`useSSL`是使用SSL加密协议,`serverTimezone`是时区设置,`characterEncoding`是数据库的字符集。 #### 4、创建连接(Java应用程序和数据库之间连接通道) ```java Connection con = DriverManager.getConnection(url,username,password); ``` #### 5、获得statement对象 ```java String sql = "select * from EMP"; PreparedStatement pst = con.prepareStatement(sql); ``` #### 6、通过statement对象执行sql语句 ```java ResultSet rs = pst.executeQuery(); ``` #### 7、处理操作结果(使用的为MySQL中的emp表) ```java /* * ResultSet的方法 * next():判断是否有下一项 * getInt("数据库字段名");:获取数据 * getString("字段"); * getDouble("字段"); * .... * */ while (rs.next()) { int EMPno = rs.getInt("EMPno"); String ename = rs.getString("ename"); String job = rs.getString("job"); String mgr = rs.getString("mgr"); String hiredate = rs.getString("hiredate"); double sal =rs.getDouble("sal"); double comm = rs.getDouble("comm"); int DEPTno = rs.getInt("DEPTno"); System.out.printf("EMPno:%d\tename:%s\tjob:%s\tmgr:%s\thiredate:%s\tsal:%1.2f\tcomm:%1.2f\tDEPTno:%d\n",EMPno,ename,job,mgr,hiredate,sal,comm,DEPTno); } ``` #### 8、关闭连接 为了不管运行时是否有异常都要关闭连接,所以要放到`finally`代码块中,所以要将`con`、`pst`、`rs`的定义放在`try-catch-finally`上面,完整的代码如下 ```java package com.doooog.jdbc; import org.junit.Test; import java.sql.*; public class DBTest { // 测试:Junit 单元块测试 // 要求测试的目标方法不能有返回值,不能有参数 // 使用注解@Test 包Junit4.12 @Test public void query() { Connection con = null; PreparedStatement pst = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://111.170.174.91:3306/company?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8"; String username = "root"; String password = "6540f55494774c0b"; con = DriverManager.getConnection(url, username, password); String sql = "select * from EMP"; pst = con.prepareStatement(sql); rs = pst.executeQuery(); while (rs.next()) { int EMPno = rs.getInt("EMPno"); String ename = rs.getString("ename"); String job = rs.getString("job"); String mgr = rs.getString("mgr"); String hiredate = rs.getString("hiredate"); double sal =rs.getDouble("sal"); double comm = rs.getDouble("comm"); int DEPTno = rs.getInt("DEPTno"); System.out.printf("EMPno:%d\tename:%s\tjob:%s\tmgr:%s\thiredate:%s\tsal:%1.2f\tcomm:%1.2f\tDEPTno:%d\n",EMPno,ename,job,mgr,hiredate,sal,comm,DEPTno); } } catch (ClassNotFoundException | SQLException e) { throw new RuntimeException(e); } finally { try { if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (SQLException e) { System.out.println(e.getMessage()); } } } } ``` **注意**:在关闭连接的时候要和定义的顺序反着来,从小到大关闭,因为一开始赋值为`null`所以要做空指针处理,否则可能会抛出空指针异常。 ## 二、封装表对象 为了方便存取`EMP`表中的数据,所以需要封装对象来存储字段值,对象类名为`Emp`(表名) ```java package com.doooog.jdbc; public class Emp { private Integer EMPno; private String ename; private String job; private Integer mgr; private String hiredate; private Double sal; private Double comm; private Integer DEPTno; public Emp() { } public Emp(Integer EMPno, String ename, String job, Integer mgr, String hiredate, Double sal, Double comm, Integer DEPTno) { this.EMPno = EMPno; this.ename = ename; this.job = job; this.mgr = mgr; this.hiredate = hiredate; this.sal = sal; this.comm = comm; this.DEPTno = DEPTno; } public Integer getDEPTno() { return DEPTno; } public void setDEPTno(Integer DEPTno) { this.DEPTno = DEPTno; } public Double getComm() { return comm; } public void setComm(Double comm) { this.comm = comm; } public Double getSal() { return sal; } public void setSal(Double sal) { this.sal = sal; } public String getHiredate() { return hiredate; } public void setHiredate(String hiredate) { this.hiredate = hiredate; } public Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public Integer getEMPno() { return EMPno; } public void setEMPno(Integer EMPno) { this.EMPno = EMPno; } @Override public String toString() { return "Emp{" + "EMPno=" + EMPno + ", ename='" + ename + '\'' + ", job='" + job + '\'' + ", mgr=" + mgr + ", hiredate='" + hiredate + '\'' + ", sal=" + sal + ", comm=" + comm + ", DEPTno=" + DEPTno + '}'; } } ``` ## 三、查询方法 修改`query()`方法并新建`getQuery()`测试方法 ```java @Test public void getQuery() { List list = query(); for (Emp e : list) { System.out.println(e); } } // 改造查询的结果处理 public List query() { Connection con = null; PreparedStatement pst = null; ResultSet rs = null; List list = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://111.170.174.91:3306/company?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8"; String username = "root"; String password = "6540f55494774c0b"; con = DriverManager.getConnection(url, username, password); String sql = "select * from EMP"; pst = con.prepareStatement(sql); rs = pst.executeQuery(); list = new ArrayList<>(); while (rs.next()) { Emp emp = new Emp( rs.getInt("EMPno"), rs.getString("ename"), rs.getString("job"), rs.getInt("mgr"), rs.getString("hiredate"), rs.getDouble("sal"), rs.getDouble("comm"), rs.getInt("DEPTno") ); list.add(emp); } } catch (ClassNotFoundException | SQLException e) { throw new RuntimeException(e); } finally { try { if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (SQLException e) { System.out.println(e.getMessage()); } } return list; } ``` **注意**:查询时需要用到`PreparedStatement.executeQuery()`方法提交事务,将返回一个`ResultSet`数据集合,使用`ResultSet.next()`来判断是否有下一项,用`ResultSet.get*()`来获取当前行的字段数据,`*`代表值对应的数据类型。 ## 四、新增方法 新建方法`add()`并新建`addEmp()`测试方法 ```java // 保存Emp @Test public void addEmp() { int result = add(new Emp( null, "张三", "MANAGER", 7788, "2025-10-14", 2000.0, 0.0, 10 )); System.out.println(result); } // 新增Emp public int add(Emp emp) { if (emp == null) return 0; Connection con = null; PreparedStatement pst = null; int result = 0; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://111.170.174.91:3306/company?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8"; String username = "root"; String password = "6540f55494774c0b"; con = DriverManager.getConnection(url, username, password); String sql = "insert into EMP(ename, job, mgr, hiredate, sal, comm, DEPTno) VALUES (?,?,?,?,?,?,?)"; pst = con.prepareStatement(sql); pst.setString(1, emp.getEname()); pst.setString(2, emp.getJob()); pst.setInt(3, emp.getMgr()); pst.setString(4, emp.getHiredate()); pst.setDouble(5, emp.getSal()); pst.setDouble(6, emp.getComm()); pst.setInt(7, emp.getDEPTno()); result = pst.executeUpdate(); } catch (ClassNotFoundException | SQLException e) { throw new RuntimeException(e); } finally { try { if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (SQLException e) { System.out.println(e.getMessage()); } } return result; } } ``` `SQL`语句动态的数据部分可以用`?`占位,使用`PreparedStatement`的`set*()`方法来修改对应位置的值,`*`代表数据类型。 **注意**:提交非查询SQL操作时需要用到`PreparedStatement.executeUpdate()`进行提交事务,返回值为`int`,代表影响到的行数。 ## 五、修改方法 ```java // 更新EmpById @Test public void updateEmpById(){ int result = update(new Emp( 7936, "李四", "CLERK", 7788, "2025-9-14", 3000.0, 100.0, 10 )); System.out.println(result); } // 更新 public int update(Emp emp) { if (emp == null || emp.getEMPno() <= 0) return 0; Connection con = null; PreparedStatement pst = null; int result = 0; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://111.170.174.91:3306/company?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8"; String username = "root"; String password = "6540f55494774c0b"; con = DriverManager.getConnection(url, username, password); String sql = "update EMP set ename=?,job=?, mgr=?, hiredate=?, sal=?, comm=?, DEPTno=? where EMPno=?"; pst = con.prepareStatement(sql); pst.setString(1, emp.getEname()); pst.setString(2, emp.getJob()); pst.setInt(3, emp.getMgr()); pst.setString(4, emp.getHiredate()); pst.setDouble(5, emp.getSal()); pst.setDouble(6, emp.getComm()); pst.setInt(7, emp.getDEPTno()); pst.setInt(8, emp.getEMPno()); // 执行SQL语句 result = pst.executeUpdate(); } catch (ClassNotFoundException | SQLException e) { throw new RuntimeException(e); } finally { try { if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (SQLException e) { System.out.println(e.getMessage()); } } return result; } ``` 与上面不同的只是SQL语句。 ## 六、删除方法 ```java // 删除EmpById @Test public void deleteEmpById(){ int result = delete(7937); System.out.println(result); } // 删除 public int delete(int EMPno){ if (EMPno <= 0) return 0; Connection con = null; PreparedStatement pst = null; int result = 0; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://111.170.174.91:3306/company?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8"; String username = "root"; String password = "6540f55494774c0b"; con = DriverManager.getConnection(url, username, password); String sql = "delete from EMP where EMPno=?"; pst = con.prepareStatement(sql); pst.setInt(1, EMPno); // 执行SQL语句 result = pst.executeUpdate(); } catch (ClassNotFoundException | SQLException e) { throw new RuntimeException(e); } finally { try { if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (SQLException e) { System.out.println(e.getMessage()); } } return result; } ``` 与上面不同的只是SQL语句。 ## 其他内容补充(使用Maven) [JDBC封装/使用DAO层](https://www.soki.me/archives/235.html) [JDBC基础模板](https://www.soki.me/archives/234.html) Loading... ## 一、JDBC驱动的使用方法 #### 1、下载`mysql-connector-java-5.1.10-bin.jar` [香肠狗网盘](https://pan.soki.me/s/vrUj) #### 2、在项目中新建`libs`目录 将`.jar`文件放进`libs`目录中 右键`.jar`包 -> 添加为库 <img src="https://www.soki.me/usr/uploads/2025/10/926641648.png" alt="image-20251014113257041" style="zoom: 33%;" style=""> 发现其可以展开详细的内容,找到`com.mysql.jdbc.Driver` #### 3、创建驱动Driver对象 在`jdbc`包下新建`DBTest.java`类,创建`query()`方法 ```java package com.doooog.jdbc; public class DBTest { public void query(){ } } ``` 在方法中,需要加载外部库,注意`forName`需要包的全称 ```java try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException) { throw new RuntimeException(e); } ``` 为了方便数据库连接 创建`url`、`username`、`password`三个String类型变量用来存放数据库地址、数据库用户名、数据库密码 ```java String url = "jdbc:mysql://localhost:3306/databaseName?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8"; String username = "username"; String password = "password"; ``` **注意**:`url`中的`jdbc:mysql`是一种数据库连接协议,`localhost`为本机地址等同于`127.0.0.1`,`3306`为数据库的所在的端口,用来进行TCP通讯,`databaseName`为要连接的数据库名称,`useUnicode=true`是使用Unicode编码,`useSSL`是使用SSL加密协议,`serverTimezone`是时区设置,`characterEncoding`是数据库的字符集。 #### 4、创建连接(Java应用程序和数据库之间连接通道) ```java Connection con = DriverManager.getConnection(url,username,password); ``` #### 5、获得statement对象 ```java String sql = "select * from EMP"; PreparedStatement pst = con.prepareStatement(sql); ``` #### 6、通过statement对象执行sql语句 ```java ResultSet rs = pst.executeQuery(); ``` #### 7、处理操作结果(使用的为MySQL中的emp表) ```java /* * ResultSet的方法 * next():判断是否有下一项 * getInt("数据库字段名");:获取数据 * getString("字段"); * getDouble("字段"); * .... * */ while (rs.next()) { int EMPno = rs.getInt("EMPno"); String ename = rs.getString("ename"); String job = rs.getString("job"); String mgr = rs.getString("mgr"); String hiredate = rs.getString("hiredate"); double sal =rs.getDouble("sal"); double comm = rs.getDouble("comm"); int DEPTno = rs.getInt("DEPTno"); System.out.printf("EMPno:%d\tename:%s\tjob:%s\tmgr:%s\thiredate:%s\tsal:%1.2f\tcomm:%1.2f\tDEPTno:%d\n",EMPno,ename,job,mgr,hiredate,sal,comm,DEPTno); } ``` #### 8、关闭连接 为了不管运行时是否有异常都要关闭连接,所以要放到`finally`代码块中,所以要将`con`、`pst`、`rs`的定义放在`try-catch-finally`上面,完整的代码如下 ```java package com.doooog.jdbc; import org.junit.Test; import java.sql.*; public class DBTest { // 测试:Junit 单元块测试 // 要求测试的目标方法不能有返回值,不能有参数 // 使用注解@Test 包Junit4.12 @Test public void query() { Connection con = null; PreparedStatement pst = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://111.170.174.91:3306/company?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8"; String username = "root"; String password = "6540f55494774c0b"; con = DriverManager.getConnection(url, username, password); String sql = "select * from EMP"; pst = con.prepareStatement(sql); rs = pst.executeQuery(); while (rs.next()) { int EMPno = rs.getInt("EMPno"); String ename = rs.getString("ename"); String job = rs.getString("job"); String mgr = rs.getString("mgr"); String hiredate = rs.getString("hiredate"); double sal =rs.getDouble("sal"); double comm = rs.getDouble("comm"); int DEPTno = rs.getInt("DEPTno"); System.out.printf("EMPno:%d\tename:%s\tjob:%s\tmgr:%s\thiredate:%s\tsal:%1.2f\tcomm:%1.2f\tDEPTno:%d\n",EMPno,ename,job,mgr,hiredate,sal,comm,DEPTno); } } catch (ClassNotFoundException | SQLException e) { throw new RuntimeException(e); } finally { try { if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (SQLException e) { System.out.println(e.getMessage()); } } } } ``` **注意**:在关闭连接的时候要和定义的顺序反着来,从小到大关闭,因为一开始赋值为`null`所以要做空指针处理,否则可能会抛出空指针异常。 ## 二、封装表对象 为了方便存取`EMP`表中的数据,所以需要封装对象来存储字段值,对象类名为`Emp`(表名) ```java package com.doooog.jdbc; public class Emp { private Integer EMPno; private String ename; private String job; private Integer mgr; private String hiredate; private Double sal; private Double comm; private Integer DEPTno; public Emp() { } public Emp(Integer EMPno, String ename, String job, Integer mgr, String hiredate, Double sal, Double comm, Integer DEPTno) { this.EMPno = EMPno; this.ename = ename; this.job = job; this.mgr = mgr; this.hiredate = hiredate; this.sal = sal; this.comm = comm; this.DEPTno = DEPTno; } public Integer getDEPTno() { return DEPTno; } public void setDEPTno(Integer DEPTno) { this.DEPTno = DEPTno; } public Double getComm() { return comm; } public void setComm(Double comm) { this.comm = comm; } public Double getSal() { return sal; } public void setSal(Double sal) { this.sal = sal; } public String getHiredate() { return hiredate; } public void setHiredate(String hiredate) { this.hiredate = hiredate; } public Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public Integer getEMPno() { return EMPno; } public void setEMPno(Integer EMPno) { this.EMPno = EMPno; } @Override public String toString() { return "Emp{" + "EMPno=" + EMPno + ", ename='" + ename + '\'' + ", job='" + job + '\'' + ", mgr=" + mgr + ", hiredate='" + hiredate + '\'' + ", sal=" + sal + ", comm=" + comm + ", DEPTno=" + DEPTno + '}'; } } ``` ## 三、查询方法 修改`query()`方法并新建`getQuery()`测试方法 ```java @Test public void getQuery() { List<Emp> list = query(); for (Emp e : list) { System.out.println(e); } } // 改造查询的结果处理 public List<Emp> query() { Connection con = null; PreparedStatement pst = null; ResultSet rs = null; List<Emp> list = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://111.170.174.91:3306/company?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8"; String username = "root"; String password = "6540f55494774c0b"; con = DriverManager.getConnection(url, username, password); String sql = "select * from EMP"; pst = con.prepareStatement(sql); rs = pst.executeQuery(); list = new ArrayList<>(); while (rs.next()) { Emp emp = new Emp( rs.getInt("EMPno"), rs.getString("ename"), rs.getString("job"), rs.getInt("mgr"), rs.getString("hiredate"), rs.getDouble("sal"), rs.getDouble("comm"), rs.getInt("DEPTno") ); list.add(emp); } } catch (ClassNotFoundException | SQLException e) { throw new RuntimeException(e); } finally { try { if (rs != null) { rs.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (SQLException e) { System.out.println(e.getMessage()); } } return list; } ``` **注意**:查询时需要用到`PreparedStatement.executeQuery()`方法提交事务,将返回一个`ResultSet`数据集合,使用`ResultSet.next()`来判断是否有下一项,用`ResultSet.get*()`来获取当前行的字段数据,`*`代表值对应的数据类型。 ## 四、新增方法 新建方法`add()`并新建`addEmp()`测试方法 ```java // 保存Emp @Test public void addEmp() { int result = add(new Emp( null, "张三", "MANAGER", 7788, "2025-10-14", 2000.0, 0.0, 10 )); System.out.println(result); } // 新增Emp public int add(Emp emp) { if (emp == null) return 0; Connection con = null; PreparedStatement pst = null; int result = 0; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://111.170.174.91:3306/company?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8"; String username = "root"; String password = "6540f55494774c0b"; con = DriverManager.getConnection(url, username, password); String sql = "insert into EMP(ename, job, mgr, hiredate, sal, comm, DEPTno) VALUES (?,?,?,?,?,?,?)"; pst = con.prepareStatement(sql); pst.setString(1, emp.getEname()); pst.setString(2, emp.getJob()); pst.setInt(3, emp.getMgr()); pst.setString(4, emp.getHiredate()); pst.setDouble(5, emp.getSal()); pst.setDouble(6, emp.getComm()); pst.setInt(7, emp.getDEPTno()); result = pst.executeUpdate(); } catch (ClassNotFoundException | SQLException e) { throw new RuntimeException(e); } finally { try { if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (SQLException e) { System.out.println(e.getMessage()); } } return result; } } ``` `SQL`语句动态的数据部分可以用`?`占位,使用`PreparedStatement`的`set*()`方法来修改对应位置的值,`*`代表数据类型。 **注意**:提交非查询SQL操作时需要用到`PreparedStatement.executeUpdate()`进行提交事务,返回值为`int`,代表影响到的行数。 ## 五、修改方法 ```java // 更新EmpById @Test public void updateEmpById(){ int result = update(new Emp( 7936, "李四", "CLERK", 7788, "2025-9-14", 3000.0, 100.0, 10 )); System.out.println(result); } // 更新 public int update(Emp emp) { if (emp == null || emp.getEMPno() <= 0) return 0; Connection con = null; PreparedStatement pst = null; int result = 0; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://111.170.174.91:3306/company?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8"; String username = "root"; String password = "6540f55494774c0b"; con = DriverManager.getConnection(url, username, password); String sql = "update EMP set ename=?,job=?, mgr=?, hiredate=?, sal=?, comm=?, DEPTno=? where EMPno=?"; pst = con.prepareStatement(sql); pst.setString(1, emp.getEname()); pst.setString(2, emp.getJob()); pst.setInt(3, emp.getMgr()); pst.setString(4, emp.getHiredate()); pst.setDouble(5, emp.getSal()); pst.setDouble(6, emp.getComm()); pst.setInt(7, emp.getDEPTno()); pst.setInt(8, emp.getEMPno()); // 执行SQL语句 result = pst.executeUpdate(); } catch (ClassNotFoundException | SQLException e) { throw new RuntimeException(e); } finally { try { if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (SQLException e) { System.out.println(e.getMessage()); } } return result; } ``` 与上面不同的只是SQL语句。 ## 六、删除方法 ```java // 删除EmpById @Test public void deleteEmpById(){ int result = delete(7937); System.out.println(result); } // 删除 public int delete(int EMPno){ if (EMPno <= 0) return 0; Connection con = null; PreparedStatement pst = null; int result = 0; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://111.170.174.91:3306/company?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8"; String username = "root"; String password = "6540f55494774c0b"; con = DriverManager.getConnection(url, username, password); String sql = "delete from EMP where EMPno=?"; pst = con.prepareStatement(sql); pst.setInt(1, EMPno); // 执行SQL语句 result = pst.executeUpdate(); } catch (ClassNotFoundException | SQLException e) { throw new RuntimeException(e); } finally { try { if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (SQLException e) { System.out.println(e.getMessage()); } } return result; } ``` 与上面不同的只是SQL语句。 ## 其他内容补充(使用Maven) [JDBC封装/使用DAO层](https://www.soki.me/archives/235.html) [JDBC基础模板](https://www.soki.me/archives/234.html) 最后修改:2025 年 10 月 22 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 👆请简单粗暴地爱我。