详解JDBC

编程入门 行业动态 更新时间:2024-10-17 15:31:21

<a href=https://www.elefans.com/category/jswz/34/1770044.html style=详解JDBC"/>

详解JDBC

JDBC简介

概念:

  • jdbc就是使用java语言操作关系型数据库的一套API

  • 全称 : (Java DataBase Connectivity) Java数据库连接

本质:

  • 官方(sun公司)定义的一套操作所有关系型数据库的规则,即接口;

  • 各个数据库厂商实现这套接口,提供数据库驱动jar包;

  • 可以使用这套接口(JDBC)编程,真正执行代码的是驱动jar包中的实现类

JDBC好处 :

  • 各数据厂商使用相同的接口,Java代码不需要针对不同的数据库分别开发

  • 可随时替换底层数据库,访问数据库的Java代码基本不变;

JDBC快速入门

  1. 创建工程,导入驱动jar包;

  2. 注册驱动

    Class.forName("com.mysql.jdbc.Driver");

  3. 获取连接

    Connection conn = DriverManager.getConnection(url, username, password);

    url : 表示要与那个数据库建立连接,username表示连接数据库的用户名,password表示密码;Java代码需要发送SQL给MySQL服务端,就需要先建立连接;

  4. 定义SQL语句

    String sql =  “update…” ;

  5. 获取执行SQL对象

    执行SQL语句需要SQL执行对象,而这个执行对象就是Statement对象

    Statement stmt = conn.createStatement();

  6. 执行SQL

    stmt.executeUpdate(sql);  

  7. 处理返回结果 : 根据需求处理结果;

  8. 释放资源 :

              后定义的资源先释放即可,一般用close()方法

简单JDBC案例代码 :

package com.it.jdbc;
​
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
​
/*** JDBC快速入门*/
public class JdbcDemo {public static void main(String[] args) throws Exception {// 1.注册驱动Class.forName("com.mysql.cj.jdbc.Driver");
​// 2.获取连接String url = "jdbc:mysql://localhost:3306/pcm?serverTimezone=GMT";String username = "root";String password = "1234";Connection conn = DriverManager.getConnection(url,username,password);
​// 3.定义sql语句String sql = "update user set password = 456789 where id = 1";
​
​// 4. 获取执行sql的对象 StatementStatement stmt = conn.createStatement();
​// 5.执行sqlint count = stmt.executeUpdate(sql);
​// 6.处理结果System.out.println(count);
​// 7.释放资源stmt.close();conn.close();}
}
​

JDBC API详解

3.1 DriverManager

DriverManager(驱动管理类)作用:

  • 注册驱动

    registerDriver方法是用于注册驱动的,但是我们之前做的入门案例并不是这样写的。而是如下实现

Class.forName("com.mysql.jdbc.Driver");

我们查询MySQL提供的Driver类,看它是如何实现的,源码如下:

在该类中的静态代码块中已经执行了 DriverManager 对象的 registerDriver() 方法进行驱动的注册了,那么我们只需要加载 Driver 类,该静态代码块就会执行。而 Class.forName("com.mysql.jdbc.Driver"); 就可以加载 Driver 类。

==提示:==

  • MySQL 5之后的驱动包,可以省略注册驱动的步骤

  • 自动加载jar包中META-INF/services/java.sql.Driver文件中的驱动类

  • 获取数据库连接

    getConnection()参数说明:

    • url : 连接路径

      语法:jdbc:mysql://ip地址(域名):端口号/数据库名称?参数键值对1&参数键值对2…

      示例:jdbc:mysql://127.0.0.1:3306/db1

      ==细节:==

      • 如果连接的是本机mysql服务器,并且mysql服务默认端口是3306,则url可以简写为:jdbc:mysql:///数据库名称?参数键值对

      • 配置 useSSL=false 参数,禁用安全连接方式,解决警告提示

    • user :用户名

    • poassword :密码

3.2 Connection

Connection(数据库连接对象)作用:

  • 获取执行 SQL 的对象

  • 管理事务

3.2.1 获取执行对象
  • 普通执行SQL对象

    Statement createStatement()

    入门案例中就是通过该方法获取的执行对象。

  • 预编译SQL的执行SQL对象:防止SQL注入

    PreparedStatement  prepareStatement(sql)

    通过这种方式获取的 PreparedStatement SQL语句执行对象是我们一会重点要进行讲解的,它可以防止SQL注入。

  • 执行存储过程的对象

    CallableStatement prepareCall(sql)

    通过这种方式获取的 CallableStatement 执行对象是用来执行存储过程的,而存储过程在MySQL中不常用,所以这个我们将不进行讲解。

3.2.2 事务管理

先回顾一下MySQL事务管理的操作:

  • 开启事务 : BEGIN; 或者 START TRANSACTION;

  • 提交事务 : COMMIT;

  • 回滚事务 : ROLLBACK;

MySQL默认是自动提交事务

接下来学习JDBC事务管理的方法。

Connection几口中定义了3个对应的方法:

  • 开启事务 void setAutoCommit(boolean autoCommit)

    参与autoCommit 表示是否自动提交事务,true表示自动提交事务,false表示手动提交事务。而开启事务需要将该参数设为为false。

  • 提交事务 : void commit()

  • 回滚事务 : void rollback();

具体代码实现如下:

/*** JDBC API 详解:Connection*/
public class JDBCDemo3_Connection {
​public static void main(String[] args) throws Exception {//1. 注册驱动//Class.forName("com.mysql.jdbc.Driver");//2. 获取连接:如果连接的是本机mysql并且端口是默认的 3306 可以简化书写String url = "jdbc:mysql:///db1?useSSL=false";String username = "root";String password = "1234";Connection conn = DriverManager.getConnection(url, username, password);//3. 定义sqlString sql1 = "update account set money = 3000 where id = 1";String sql2 = "update account set money = 3000 where id = 2";//4. 获取执行sql的对象 StatementStatement stmt = conn.createStatement();
​try {// ============开启事务==========conn.setAutoCommit(false);//5. 执行sqlint count1 = stmt.executeUpdate(sql1);//受影响的行数//6. 处理结果System.out.println(count1);int i = 3/0;//5. 执行sqlint count2 = stmt.executeUpdate(sql2);//受影响的行数//6. 处理结果System.out.println(count2);
​// ============提交事务==========//程序运行到此处,说明没有出现任何问题,则需求提交事务connmit();} catch (Exception e) {// ============回滚事务==========//程序在出现异常时会执行到这个地方,此时就需要回滚事务conn.rollback();e.printStackTrace();}
​//7. 释放资源stmt.close();conn.close();}
}

3.3 Statement

3.3.1 概述

Statement对象的作用就是用来执行SQL语句。而针对不同类型的SQL语句使用的方法也不一样。

  • 执行DDL、DML语句 : int excuteUpdate(String sql);

  • 执行DQL语句 : ResultSet excuteQuery(String sql);

    该方法涉及到了 ResultSet 对象,而这个对象我们还没有学习,一会再重点讲解。

3.3.2 代码实现
  • 执行DML语句

    /*** 执行DML语句* @throws Exception*/
    @Test
    public void testDML() throws  Exception {//1. 注册驱动//Class.forName("com.mysql.jdbc.Driver");//2. 获取连接:如果连接的是本机mysql并且端口是默认的 3306 可以简化书写String url = "jdbc:mysql:///db1?useSSL=false";String username = "root";String password = "1234";Connection conn = DriverManager.getConnection(url, username, password);//3. 定义sqlString sql = "update account set money = 3000 where id = 1";//4. 获取执行sql的对象 StatementStatement stmt = conn.createStatement();//5. 执行sqlint count = stmt.executeUpdate(sql);//执行完DML语句,受影响的行数//6. 处理结果//System.out.println(count);if(count > 0){System.out.println("修改成功~");}else{System.out.println("修改失败~");}//7. 释放资源stmt.close();conn.close();
    }

  • 执行DDL语句

    /*** 执行DDL语句* @throws Exception*/
    @Test
    public void testDDL() throws  Exception {//1. 注册驱动//Class.forName("com.mysql.jdbc.Driver");//2. 获取连接:如果连接的是本机mysql并且端口是默认的 3306 可以简化书写String url = "jdbc:mysql:///db1?useSSL=false";String username = "root";String password = "1234";Connection conn = DriverManager.getConnection(url, username, password);//3. 定义sqlString sql = "drop database db2";//4. 获取执行sql的对象 StatementStatement stmt = conn.createStatement();//5. 执行sqlint count = stmt.executeUpdate(sql);//执行完DDL语句,可能是0//6. 处理结果System.out.println(count);
    ​//7. 释放资源stmt.close();conn.close();
    }

    注意:

    • 以后开发很少使用java代码操作DDL语句

3.4 ResultSet

3.4.1 概述

ResultSet(结果集对象)作用:

  • ==封装了SQL查询语句的结果。==

而执行了DQL语句后就会返回该对象,对应执行DQL语句的方法如下:

ResultSet  executeQuery(sql):执行DQL 语句,返回 ResultSet 对象

那么我们就需要从 ResultSet 对象中获取我们想要的数据。ResultSet 对象提供了操作查询结果数据的方法,如下:

boolean next()

  • 将光标从当前位置向前移动一行

  • 判断当前行是否为有效行

方法返回值说明:

  • true : 有效航,当前行有数据

  • false : 无效行,当前行没有数据

xxx getXxx(参数):获取数据

  • xxx : 数据类型;如: int getInt(参数) ;String getString(参数)

  • 参数

    • int类型的参数:列的编号,从1开始

    • String类型的参数: 列的名称

一开始光标指定于第一行前,如图所示红色箭头指向于表头行。当我们调用了 next() 方法后,光标就下移到第一行数据,并且方法返回true,此时就可以通过 getInt("id") 获取当前行id字段的值,也可以通过 getString("name") 获取当前行name字段的值。如果想获取下一行的数据,继续调用 next() 方法,以此类推。

3.4.2 代码实现
/*** 执行DQL* @throws Exception*/
@Test
public void testResultSet() throws  Exception {//1. 注册驱动//Class.forName("com.mysql.jdbc.Driver");//2. 获取连接:如果连接的是本机mysql并且端口是默认的 3306 可以简化书写String url = "jdbc:mysql:///db1?useSSL=false";String username = "root";String password = "1234";Connection conn = DriverManager.getConnection(url, username, password);//3. 定义sqlString sql = "select * from account";//4. 获取statement对象Statement stmt = conn.createStatement();//5. 执行sqlResultSet rs = stmt.executeQuery(sql);//6. 处理结果, 遍历rs中的所有数据/* // 6.1 光标向下移动一行,并且判断当前行是否有数据while (rs.next()){//6.2 获取数据  getXxx()int id = rs.getInt(1);String name = rs.getString(2);double money = rs.getDouble(3);
​System.out.println(id);System.out.println(name);System.out.println(money);
​System.out.println("--------------");
​}*/// 6.1 光标向下移动一行,并且判断当前行是否有数据while (rs.next()){//6.2 获取数据  getXxx()int id = rs.getInt("id");String name = rs.getString("name");double money = rs.getDouble("money");
​System.out.println(id);System.out.println(name);System.out.println(money);
​System.out.println("--------------");}
​//7. 释放资源rs.close();stmt.close();conn.close();
}

3.5 PreparedStatement

PreparedStatement作用:

  • 预编译SQL语句并执行:预防SQL注入问题

对上面的作用中SQL注入问题大家肯定不理解。那我们先对SQL注入进行说明.

3.5.1 SQL注入

SQL注入是通过操作输入来修改事先定义好的SQL语句,用以达到执行代码对服务器进行攻击的方法。

可以模拟sql执行密码输入成 : ' or '1' ='1,然后就能够直接进入数据库;

那么如何解决SQL注入问题呢?这里就可以将SQL执行对象 Statement 换成 PreparedStatement 对象。

3.6.2 代码模拟SQL注入问题
@Test
public void testLogin() throws  Exception {//2. 获取连接:如果连接的是本机mysql并且端口是默认的 3306 可以简化书写String url = "jdbc:mysql:///db1?useSSL=false";String username = "root";String password = "1234";Connection conn = DriverManager.getConnection(url, username, password);
​// 接收用户输入 用户名和密码String name = "sjdljfld";String pwd = "' or '1' = '1";String sql = "select * from tb_user where username = '"+name+"' and password = '"+pwd+"'";// 获取stmt对象Statement stmt = conn.createStatement();// 执行sqlResultSet rs = stmt.executeQuery(sql);// 判断登录是否成功if(rs.next()){System.out.println("登录成功~");}else{System.out.println("登录失败~");}
​//7. 释放资源rs.close();stmt.close();conn.close();
}

上面代码是将用户名和密码拼接到sql语句中,拼接后的sql语句如下

select * from tb_user where username = 'sjdljfld' and password = ''or '1' = '1'

从上面语句可以看出条件 username = 'sjdljfld' and password = '' 不管是否满足,而 or 后面的 '1' = '1' 是始终满足的,最终条件是成立的,就可以正常的进行登陆了。

接下来我们来学习PreparedStatement对象.

3.6.3 PreparedStatement概述

PreparedStatement作用:

  • 预编译SQL语句并执行:预防SQL注入问题

  • 获取 PreparedStatement 对象

    // SQL语句中的参数值,使用?占位符替代
    String sql = "select * from user where username = ? and password = ?";
    // 通过Connection对象获取,并传入对应的sql语句
    PreparedStatement pstmt = conn.prepareStatement(sql);

  • 设置参数值

    上面的sql语句中参数使用 ? 进行占位,在之前之前肯定要设置这些 ? 的值。

    PreparedStatement对象:setXxx(参数1,参数2):给 ? 赋值

    • Xxx:数据类型 ; 如 setInt (参数1,参数2)

    • 参数:

      • 参数1: ?的位置编号,从1 开始

      • 参数2: ?的值

  • 执行SQL语句

    executeUpdate(); 执行DDL语句和DML语句

    executeQuery(); 执行DQL语句

    ==注意:==

    • 调用这两个方法时不需要传递SQL语句,因为获取SQL语句执行对象时已经对SQL语句进行预编译了。

3.6.4 使用PreparedStatement改进
 @Test
public void testPreparedStatement() throws  Exception {//2. 获取连接:如果连接的是本机mysql并且端口是默认的 3306 可以简化书写String url = "jdbc:mysql:///db1?useSSL=false";String username = "root";String password = "1234";Connection conn = DriverManager.getConnection(url, username, password);
​// 接收用户输入 用户名和密码String name = "zhangsan";String pwd = "' or '1' = '1";
​// 定义sqlString sql = "select * from tb_user where username = ? and password = ?";// 获取pstmt对象PreparedStatement pstmt = conn.prepareStatement(sql);// 设置?的值pstmt.setString(1,name);pstmt.setString(2,pwd);// 执行sqlResultSet rs = pstmt.executeQuery();// 判断登录是否成功if(rs.next()){System.out.println("登录成功~");}else{System.out.println("登录失败~");}//7. 释放资源rs.close();pstmt.close();conn.close();
}

执行上面语句就可以发现不会出现SQL注入漏洞问题了。那么PreparedStatement又是如何解决的呢?它是将特殊字符进行了转义,转义的SQL如下:

select * from tb_user where username = 'sjdljfld' and password = '\'or \'1\' = \'1'

4,数据库连接池

4.1 数据库连接池简介

  • 数据库连接池是个容器,负责分配、管理数据库连接(Connection)

  • 它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个;

  • 释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏

  • 好处

    • 资源重用

    • 提升系统响应速度

    • 避免数据库连接遗漏

之前我们代码中使用连接是没有使用都创建一个Connection对象,使用完毕就会将其销毁。这样重复创建销毁的过程是特别耗费计算机的性能的及消耗时间的。

连接池是在一开始就创建好了一些连接(Connection)对象存储起来。用户需要连接数据库时,不需要自己创建连接,而只需要从连接池中获取一个连接进行使用,使用完毕后再将连接对象归还给连接池;这样就可以起到资源重用,也节省了频繁创建连接销毁连接所花费的时间,从而提升了系统响应的速度。

4.2 数据库连接池实现

  • 标准接口:==DataSource==

    官方(SUN) 提供的数据库连接池标准接口,由第三方组织实现此接口。该接口提供了获取连接的功能:

    Connection getConnection()

    那么以后就不需要通过 DriverManager 对象获取 Connection 对象,而是通过连接池(DataSource)获取 Connection 对象。

  • 常见的数据库连接池

    • DBCP

    • C3P0

    • Druid

    我们现在使用更多的是Druid,它的性能比其他两个会好一些。

  • Druid(德鲁伊)

    • Druid连接池是阿里巴巴开源的数据库连接池项目

    • 功能强大,性能优秀,是Java语言最好的数据库连接池之一

4.3 Driud使用

  • 导入jar包 druid-1.1.12.jar

  • 定义配置文件

  • 加载配置文件

  • 获取数据库连接池对象

  • 获取连接

现在通过代码实现,首先需要先将druid的jar包放到项目下的lib下并添加为库文件

编写配置文件如下:

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///db1?useSSL=false&useServerPrepStmts=true
username=root
password=1234
# 初始化连接数量
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000

使用druid的代码如下:

/*** Druid数据库连接池演示*/
public class DruidDemo {
​public static void main(String[] args) throws Exception {//1.导入jar包//2.定义配置文件//3. 加载配置文件Properties prop = new Properties();prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));//4. 获取连接池对象DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
​//5. 获取数据库连接 ConnectionConnection connection = dataSource.getConnection();System.out.println(connection); //获取到了连接后就可以继续做其他操作了
​//System.out.println(System.getProperty("user.dir"));}
}

5,JDBC练习

5.1 需求

完成商品品牌数据的增删改查操作

  • 查询:查询所有数据

  • 添加:添加品牌

  • 修改:根据id修改

  • 删除:根据id删除

5.2 案例实现

5.2.1 环境准备
  • 数据库表 tb_brand

    -- 删除tb_brand表
    drop table if exists tb_brand;
    -- 创建tb_brand表
    create table tb_brand (-- id 主键id int primary key auto_increment,-- 品牌名称brand_name varchar(20),-- 企业名称company_name varchar(20),-- 排序字段ordered int,-- 描述信息description varchar(100),-- 状态:0:禁用  1:启用status int
    );
    -- 添加数据
    insert into tb_brand (brand_name, company_name, ordered, description, status)
    values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火', 0),('华为', '华为技术有限公司', 100, '华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界', 1),('小米', '小米科技有限公司', 50, 'are you ok', 1);
  • 在pojo包下实体类 Brand

    /*** 品牌* alt + 鼠标左键:整列编辑* 在实体类中,基本数据类型建议使用其对应的包装类型*/
    public class Brand {// id 主键private Integer id;// 品牌名称private String brandName;// 企业名称private String companyName;// 排序字段private Integer ordered;// 描述信息private String description;// 状态:0:禁用  1:启用private Integer status;
    ​public Integer getId() {return id;}
    ​public void setId(Integer id) {this.id = id;}
    ​public String getBrandName() {return brandName;}
    ​public void setBrandName(String brandName) {this.brandName = brandName;}
    ​public String getCompanyName() {return companyName;}
    ​public void setCompanyName(String companyName) {thispanyName = companyName;}
    ​public Integer getOrdered() {return ordered;}
    ​public void setOrdered(Integer ordered) {this.ordered = ordered;}
    ​public String getDescription() {return description;}
    ​public void setDescription(String description) {this.description = description;}
    ​public Integer getStatus() {return status;}
    ​public void setStatus(Integer status) {this.status = status;}
    ​@Overridepublic String toString() {return "Brand{" +"id=" + id +", brandName='" + brandName + '\'' +", companyName='" + companyName + '\'' +", ordered=" + ordered +", description='" + description + '\'' +", status=" + status +'}';}
    }
5.2.2 查询所有
 /*** 查询所有* 1. SQL:select * from tb_brand;* 2. 参数:不需要* 3. 结果:List<Brand>*/
​
@Test
public void testSelectAll() throws Exception {//1. 获取Connection//3. 加载配置文件Properties prop = new Properties();prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));//4. 获取连接池对象DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
​//5. 获取数据库连接 ConnectionConnection conn = dataSource.getConnection();//2. 定义SQLString sql = "select * from tb_brand;";//3. 获取pstmt对象PreparedStatement pstmt = conn.prepareStatement(sql);//4. 设置参数//5. 执行SQLResultSet rs = pstmt.executeQuery();//6. 处理结果 List<Brand> 封装Brand对象,装载List集合Brand brand = null;List<Brand> brands = new ArrayList<>();while (rs.next()){//获取数据int id = rs.getInt("id");String brandName = rs.getString("brand_name");String companyName = rs.getString("company_name");int ordered = rs.getInt("ordered");String description = rs.getString("description");int status = rs.getInt("status");//封装Brand对象brand = new Brand();brand.setId(id);brand.setBrandName(brandName);brand.setCompanyName(companyName);brand.setOrdered(ordered);brand.setDescription(description);brand.setStatus(status);
​//装载集合brands.add(brand);}System.out.println(brands);//7. 释放资源rs.close();pstmt.close();conn.close();
}
5.2.3 添加数据
/*** 添加* 1. SQL:insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);* 2. 参数:需要,除了id之外的所有参数信息* 3. 结果:boolean*/
@Test
public void testAdd() throws Exception {// 接收页面提交的参数String brandName = "香飘飘";String companyName = "香飘飘";int ordered = 1;String description = "绕地球一圈";int status = 1;
​//1. 获取Connection//3. 加载配置文件Properties prop = new Properties();prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));//4. 获取连接池对象DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);//5. 获取数据库连接 ConnectionConnection conn = dataSource.getConnection();//2. 定义SQLString sql = "insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);";//3. 获取pstmt对象PreparedStatement pstmt = conn.prepareStatement(sql);//4. 设置参数pstmt.setString(1,brandName);pstmt.setString(2,companyName);pstmt.setInt(3,ordered);pstmt.setString(4,description);pstmt.setInt(5,status);
​//5. 执行SQLint count = pstmt.executeUpdate(); // 影响的行数//6. 处理结果System.out.println(count > 0);
​//7. 释放资源pstmt.close();conn.close();
}
5.2.4 修改数据
/*** 修改* 1. SQL:
​update tb_brandset brand_name  = ?,company_name= ?,ordered     = ?,description = ?,status      = ?where id = ?
​* 2. 参数:需要,所有数据* 3. 结果:boolean*/
​
@Test
public void testUpdate() throws Exception {// 接收页面提交的参数String brandName = "香飘飘";String companyName = "香飘飘";int ordered = 1000;String description = "绕地球三圈";int status = 1;int id = 4;
​//1. 获取Connection//3. 加载配置文件Properties prop = new Properties();prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));//4. 获取连接池对象DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);//5. 获取数据库连接 ConnectionConnection conn = dataSource.getConnection();//2. 定义SQLString sql = " update tb_brand\n" +"         set brand_name  = ?,\n" +"         company_name= ?,\n" +"         ordered     = ?,\n" +"         description = ?,\n" +"         status      = ?\n" +"     where id = ?";
​//3. 获取pstmt对象PreparedStatement pstmt = conn.prepareStatement(sql);
​//4. 设置参数pstmt.setString(1,brandName);pstmt.setString(2,companyName);pstmt.setInt(3,ordered);pstmt.setString(4,description);pstmt.setInt(5,status);pstmt.setInt(6,id);
​//5. 执行SQLint count = pstmt.executeUpdate(); // 影响的行数//6. 处理结果System.out.println(count > 0);
​//7. 释放资源pstmt.close();conn.close();
}
5.2.5 删除数据
/*** 删除* 1. SQL:delete from tb_brand where id = ?* 2. 参数:需要,id* 3. 结果:boolean*/
@Test
public void testDeleteById() throws Exception {// 接收页面提交的参数int id = 4;//1. 获取Connection//3. 加载配置文件Properties prop = new Properties();prop.load(new FileInputStream("jdbc-demo/src/druid.properties"));//4. 获取连接池对象DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);//5. 获取数据库连接 ConnectionConnection conn = dataSource.getConnection();//2. 定义SQLString sql = " delete from tb_brand where id = ?";//3. 获取pstmt对象PreparedStatement pstmt = conn.prepareStatement(sql);//4. 设置参数pstmt.setInt(1,id);//5. 执行SQLint count = pstmt.executeUpdate(); // 影响的行数//6. 处理结果System.out.println(count > 0);
​//7. 释放资源pstmt.close();conn.close();
}

更多推荐

详解JDBC

本文发布于:2023-11-15 01:04:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1590922.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:详解   JDBC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!