轻松掌握Java增删改查(CRUD)操作的代码示例!

前言

在Java开发中,CRUD操作是基础且必不可少的一部分。本文将带您通过具体的代码示例,轻松学习如何使用Java实现增加、删除、修改和查询功能。

代码实现

1. 增加(Create):插入新数据

java 复制代码
import java.sql.*;

public class InsertExample {
   public static void main(String[] args) {
      // 建立数据库连接
      Connection connection = null;
      Statement statement = null;
      try {
         Class.forName("com.mysql.jdbc.Driver");
         connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
         statement = connection.createStatement();

         // 准备插入数据的SQL语句
         String sql = "INSERT INTO employees (id, name, age) VALUES (1, 'John Doe', 30)";
         statement.executeUpdate(sql);

         System.out.println("Data inserted successfully!");

      } catch (ClassNotFoundException | SQLException e) {
         e.printStackTrace();
      } finally {
         // 关闭连接和资源
         try {
            if (statement != null)
               statement.close();
            if (connection != null)
               connection.close();
         } catch (SQLException e) {
            e.printStackTrace();
         }
      }
   }
}

2. 删除(Delete):移除无用数据

java 复制代码
import java.sql.*;

public class DeleteExample {
   public static void main(String[] args) {
      // 建立数据库连接
      Connection connection = null;
      Statement statement = null;
      try {
         Class.forName("com.mysql.jdbc.Driver");
         connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
         statement = connection.createStatement();

         // 准备删除数据的SQL语句
         String sql = "DELETE FROM employees WHERE id = 1";
         statement.executeUpdate(sql);

         System.out.println("Data deleted successfully!");

      } catch (ClassNotFoundException | SQLException e) {
         e.printStackTrace();
      } finally {
         // 关闭连接和资源
         try {
            if (statement != null)
               statement.close();
            if (connection != null)
               connection.close();
         } catch (SQLException e) {
            e.printStackTrace();
         }
      }
   }
}

3. 修改(Update):更新数据内容

java 复制代码
import java.sql.*;

public class UpdateExample {
   public static void main(String[] args) {
      // 建立数据库连接
      Connection connection = null;
      Statement statement = null;
      try {
         Class.forName("com.mysql.jdbc.Driver");
         connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
         statement = connection.createStatement();

         // 准备更新数据的SQL语句
         String sql = "UPDATE employees SET name='Jane Smith' WHERE id = 1";
         statement.executeUpdate(sql);

         System.out.println("Data updated successfully!");

      } catch (ClassNotFoundException | SQLException e) {
         e.printStackTrace();
      } finally {
         // 关闭连接和资源
         try {
            if (statement != null)
               statement.close();
            if (connection != null)
               connection.close();
         } catch (SQLException e) {
            e.printStackTrace();
         }
      }
   }
}

4. 查询(Retrieve):获取所需数据

java 复制代码
import java.sql.*;

public class RetrieveExample {
   public static void main(String[] args) {
      // 建立数据库连接
      Connection connection = null;
      Statement statement = null;
      ResultSet resultSet = null;
      try {
         Class.forName("com.mysql.jdbc.Driver");
         connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
         statement = connection.createStatement();

         // 准备查询数据的SQL语句
         String sql = "SELECT * FROM employees";
         resultSet = statement.executeQuery(sql);

         // 遍历结果集并输出数据
         while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int age = resultSet.getInt("age");

            System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
         }

      } catch (ClassNotFoundException | SQLException e) {
         e.printStackTrace();
      } finally {
         // 关闭连接和资源
         try {
            if (resultSet != null)
               resultSet.close();
            if (statement != null)
               statement.close();
            if (connection != null)
               connection.close();
         } catch (SQLException e) {
            e.printStackTrace();
         }
      }
   }
}

结语

通过以上几个示例,您可以快速了解Java中的增加、删除、修改和查询操作。根据您的实际需求和数据库类型,您可以修改这些示例来适应您的应用程序。写的可能不全面,欢迎添加补充!

相关推荐
你为她披上外套时我正站在窗外8 分钟前
5 分钟玩转 siwi-download:CLI + Rust 库上手指南
后端
橘子海全栈攻城狮10 分钟前
【最新源码】基于SpringBoot + Vue的超市管理系统的设计与实现D002
java·开发语言·vue.js·spring boot·后端·spring
smartvxworks16 分钟前
Linux 实时内核(Linux Real-Time Kernel)详解:原理、实践与优化
java·linux·服务器
qizayaoshuap16 分钟前
# [特殊字符] 密码生成器 — 鸿蒙ArkTS安全算法与密码强度评估系统
java·算法·安全·华为·harmonyos
AC赳赳老秦19 分钟前
OpenClaw 采集任务日志审计:全程记录采集行为,满足合规溯源与企业审计要求
java·大数据·python·数据挖掘·数据分析·php·openclaw
2501_9185823730 分钟前
HarmonyOS应用开发实战:小事记 - Stage 模型下 EntryAbility 的启动流程与 Want 解析机制
后端
风起洛阳@不良使1 小时前
spring中xml和注解开发的对比
xml·java·spring
Tenifs1 小时前
关闭 IDEA 的英文单词拼写检查
java·intellij-idea
captain3761 小时前
创建多线程程序
java·java-ee
野蛮人6号1 小时前
黑马天机学堂Day01-08.搭建项目环境-本地开发部署方式——不知道nacos的密码是什么
java·spring cloud·黑马程序员·天机学堂·黑马天机学堂