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

相关推荐
摇滚侠28 分钟前
Spring Boot 3零基础教程,创建第一个 Spring Boot 3 应用,Spring Boot 3 外部配置,笔记03
java·spring boot·笔记
Arva .29 分钟前
面试题02
后端
没有bug.的程序员3 小时前
服务网格 Service Mesh:微服务通信的终极进化
java·分布式·微服务·云原生·service_mesh
uzong5 小时前
一次慢接口背后,竟藏着40+种可能!你中过几个
后端·面试·程序员
G探险者5 小时前
滴滴P0级故障背后:互联网公司是如何分级处理线上事故的?
后端
G探险者5 小时前
从 Tomcat 与 Jetty 的对比,聊聊影响一个服务并发能力的关键因素
后端
你的人类朋友6 小时前
“签名”这个概念是非对称加密独有的吗?
前端·后端·安全
南尘NCA86666 小时前
企业微信防封防投诉拦截系统:从痛点解决到技术实现
java·网络·企业微信
幼稚园的山代王6 小时前
go语言了解
开发语言·后端·golang