轻松掌握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中的增加、删除、修改和查询操作。根据您的实际需求和数据库类型,您可以修改这些示例来适应您的应用程序。写的可能不全面,欢迎添加补充!

相关推荐
摇滚侠1 小时前
Spring Boot 3零基础教程,自动配置机制,笔记07
spring boot·笔记·后端
兮动人1 小时前
Eureka注册中心通用写法和配置
java·云原生·eureka
程序员爱钓鱼2 小时前
Go语言实战案例——进阶与部署篇:性能优化与 pprof 性能分析实践
后端·google·go
爱编程的小白L3 小时前
基于springboot志愿服务管理系统设计与实现(附源码)
java·spring boot·后端
聪明的笨猪猪5 小时前
Java Redis “持久化”面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
聪明的笨猪猪5 小时前
Java Redis “核心基础”面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
稚辉君.MCA_P8_Java7 小时前
JVM第二课:一文讲透运行时数据区
jvm·数据库·后端·容器
奋斗的小monkey7 小时前
Spring Boot 3.x核心特性与性能优化实战
java·spring boot·微服务·性能优化·响应式编程
程序猿DD8 小时前
将 GPU 级性能带到企业级 Java:CUDA 集成实用指南
java·架构