今日学习的 jdbc statement的增删改

首先要获取jdbc文件

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

连接数据库(数据库要提前打完在写增删改查)

         Connection  connection=
DriverManager.getConnection("jdbc:mysql://localhost:3306/db_day11","root","root");

后面两个字段是账户和密码下载数据库设置的密码

Statement就是用来向数据库发送要执行的SQL语句的对象。

	Statement stmt = con.createStatement();

添加

  @Test
    public void insert() throws Exception {
        //1.获取连接对象,与数据库建立连接
        Connection con = getConnection();
        //2.获取sql执行对象
        Statement stmt = con.createStatement();
        //3.编写sql
        String sql = "insert into user values(null,'zhaoliu', '789')";
        //4.执行sql
        int i = stmt.executeUpdate(sql);
        System.out.println("插入成功,影响的行数:"+i);
        //5.释放资源
        stmt.close();
        con.close();
    }

修改数据

   public static void main(String[] args) throws Exception {
      Class.forName("com.mysql.jdbc.Driver");
        Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/db_day11","root","root");
       Statement statement=connection.createStatement();
        int i1=statement.executeUpdate("update student set name='小伙'where id=3");
        System.out.println(i1);
       statement.close();
        connection.close();
    }

删除数据

 public static void fun2(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/db_day11","root","root");
        Statement statement=connection.createStatement();
        ResultSet resultSet = statement.executeQuery(" delete from student where name='小伙'");

        statement.close();
        connection.close();
    }

重要的东西说三遍!

关闭数据要采用后行先关!!

关闭数据要采用后行先关!!

关闭数据要采用后行先关!!

  • 16
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当我在前面说了什么时, 为什么狗会汪汪叫? 以下是 Java JDBC 批量增删数据库操作的代码示例: // 假设有一个名为 "users" 的表 // 数据库连接对象 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "password"); // 要执行的 SQL 语句 String insertSql = "INSERT INTO users (name, age) VALUES (?, ?)"; String updateSql = "UPDATE users SET age = ? WHERE id = ?"; String deleteSql = "DELETE FROM users WHERE id = ?"; // 创建 PreparedStatement 对象 PreparedStatement insertStmt = conn.prepareStatement(insertSql); PreparedStatement updateStmt = conn.prepareStatement(updateSql); PreparedStatement deleteStmt = conn.prepareStatement(deleteSql); // 批量插入多条数据 insertStmt.setString(1, "Alice"); insertStmt.setInt(2, 18); insertStmt.addBatch(); insertStmt.setString(1, "Bob"); insertStmt.setInt(2, 20); insertStmt.addBatch(); insertStmt.setString(1, "Charlie"); insertStmt.setInt(2, 22); insertStmt.addBatch(); // 批量更新多条数据 updateStmt.setInt(1, 25); updateStmt.setInt(2, 1); updateStmt.addBatch(); updateStmt.setInt(1, 22); updateStmt.setInt(2, 2); updateStmt.addBatch(); // 批量删除多条数据 deleteStmt.setInt(1, 3); deleteStmt.addBatch(); deleteStmt.setInt(1, 4); deleteStmt.addBatch(); // 执行批量操作 int[] insertCounts = insertStmt.executeBatch(); int[] updateCounts = updateStmt.executeBatch(); int[] deleteCounts = deleteStmt.executeBatch(); // 关闭资源 insertStmt.close(); updateStmt.close(); deleteStmt.close(); conn.close(); 上述代码示例中,我们首先获取了一个 MySQL 数据库连接对象,然后创建了三个 PreparedStatement 对象分别用于执行插入、更新和删除操作。接着,我们使用 PreparedStatement 的 addBatch() 方法向批处理中添加多条记录,最后使用 executeBatch() 方法执行批量操作并获取每次操作所影响的行数。最后,我们关闭了资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白时期的我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值