Java实现CRUD操作

本文介绍了如何使用Java进行基本的数据库CRUD操作,主要聚焦在MySQL环境下的实现,包括创建、读取、更新和删除数据的详细步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java实现CURD操作,最基础的版本

import org.junit.Test;

import java.sql.*;

public class JDBCDemo01 {
    public static void main(String[] args) {
//        testSelect();
//        testInsert();
//        testUpdate();
        testDelete();
    }

    //删除
    public static void testDelete(){
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            String url = "jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC";
            connection = DriverManager.getConnection(url, "root", "123456");

            String sql = "delete from orderlist where id=9";
            statement = connection.createStatement();
            int row = statement.executeUpdate(sql);     //进行添加行数,返回影响行数
            System.out.println(row);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                statement.close();
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }

    }


    //修改
    public static void testUpdate(){
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            String url = "jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC";
            connection = DriverManager.getConnection(url, "root", "123456");

            String sql = "update orderlist set uid='100' where id=9";
            statement = connection.createStatement();
            int row = statement.executeUpdate(sql);     //进行添加行数,返回影响行数
            System.out.println(row);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                statement.close();
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }

    }


    //添加
    public static void testInsert(){
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            String url = "jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC";
            connection = DriverManager.getConnection(url, "root", "123456");

            String sql = "insert into orderlist (id, number, uid) values (5, 1005, 3)";
            statement = connection.createStatement();
            int row = statement.executeUpdate(sql);     //进行添加行数,返回影响行数
            System.out.println(row);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                statement.close();
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }

    }



    //查询
    public static void testSelect() {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;


        try {
            // 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 创建连接
            /*
            * @param url a database url of the form
             * <code>jdbc:<em>subprotocol</em>:<em>subname</em></code>
             * @param user the database user on whose behalf the connection is being
             *   made
             * @param password the user's password
            * */
            String url = "jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC";
            connection = DriverManager.getConnection(url, "root", "123456");

            // 编写sql,创建statement,执行sql语句
            String sql = "select * from orderlist";
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);

            // 查询返回结果,遍历打印
            while (resultSet.next()){
                int id = resultSet.getInt("id");
                String number = resultSet.getString("number");
                int uid = resultSet.getInt("uid");
                System.out.println(id + " " + number + " " + uid);
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 关闭资源
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值