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

2023年 9月 25日 51.1k 0

前言

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

代码实现

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

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):移除无用数据

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):更新数据内容

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):获取所需数据

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

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论