Skip to content

JDBC 删除数据

数据库和表设计

sql
-- 创建数据库
CREATE DATABASE `bank`;

-- 切换到 bank 库
USE `bank`;

-- 创建表
CREATE TABLE `user_balance` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `name` VARCHAR(45) NOT NULL ,
  `balance` BIGINT NOT NULL ,
  PRIMARY KEY (`id`)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_general_ci;

使用 Statement 删除数据

user_balance 表中准备两条数据:

plain
mysql> select * from user_balance;
+----+--------+---------+
| id | name   | balance |
+----+--------+---------+
|  1 | xigua |    1000 |
|  2 | fanqie |    1001 |
+----+--------+---------+
2 rows in set (0.00 sec)

以下是更新/删除数据的示例:

java
package demo;

import java.sql.*;

/**
 * 使用 Statement 删除数据
 */
public class StatemenDeleteDemo {

    private static final String USER = "root";
    private static final String PASSWORD = "123456";

    private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    private static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/bank";

    /**
     * 根据 name 删除数据
     */
    public static void delete(String name) throws ClassNotFoundException, SQLException {
        Class.forName(JDBC_DRIVER);
        Connection conn =  DriverManager.getConnection(DB_URL, USER, PASSWORD);
        Statement stmt = null;
        try {
            stmt = conn.createStatement();
            String sql = String.format("DELETE FROM user_balance where name='%s'", name);
            int affectRowsNum = stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
            System.out.println("影响的行数:" + affectRowsNum);
        } finally {
            if (stmt != null) {
                stmt.close();
            }
            conn.close();
        }
    }


    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        delete("fanqie");
    }
}

运行后输出:

plain
影响的行数:1

查看 user_balance表中内容:

plain
mysql> select * from user_balance;
+----+--------+---------+
| id | name   | balance |
+----+--------+---------+
|  1 | xigua |    1000 |
+----+--------+---------+
1 row in set (0.00 sec)

可以看到 name 为 fanqie 的记录被删掉了。

使用 PreparedStatement 删除数据

java
package demo;

import java.sql.*;

/**
 * 使用 PreparedStatement 删除数据
 */
public class PreparedStatemenDeleteDemo {

    private static final String USER = "root";
    private static final String PASSWORD = "123456";

    private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    private static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/bank";

    public static void delete(String name) throws ClassNotFoundException, SQLException {
        Class.forName(JDBC_DRIVER);
        Connection conn =  DriverManager.getConnection(DB_URL, USER, PASSWORD);
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement("DELETE FROM user_balance where name=?");
            pstmt.setString(1, name);
            int affectRowsNum = pstmt.executeUpdate();
            System.out.println("影响的行数:" + affectRowsNum);
        } finally {
            if (pstmt != null) {
                pstmt.close();
            }
            conn.close();
        }
    }


    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        delete("fanqie");
    }
}

执行结果:

影响的行数:1

查看 user_balance表中内容:

plain
mysql> select * from user_balance;
+----+--------+---------+
| id | name   | balance |
+----+--------+---------+
|  1 | xigua |    1000 |
+----+--------+---------+
1 row in set (0.00 sec)

可以看到 name 为 fanqie 的记录被删掉了。