java连接mysql增删改查四层结构

1. 数据源层:JDBC连接MySQL数据库,获取Connection对象。,2. 持久层:使用DAO(Data Access Object)模式,封装CRUD操作。,3. 业务层:处理业务逻辑,调用持久层方法。,4. 表示层:展示数据,接收用户输入,调用业务层方法。

Java连接MySQL数据库

1、下载MySQL Connector/J驱动:访问MySQL官网(https://dev.mysql.com/downloads/connector/j/)下载对应版本的MySQL Connector/J驱动。

2、将下载的jar包添加到Java项目的类路径中。

java连接mysql增删改查四层结构

创建数据库连接

1、导入相关包:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

2、编写方法创建数据库连接:

public static Connection getConnection() {
    Connection connection = null;
    try {
        // 加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 获取数据库连接
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名?useSSL=false&serverTimezone=UTC", "用户名", "密码");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return connection;
}

执行增删改查操作

1、插入数据:

java连接mysql增删改查四层结构

public static void insertData(String name, int age) {
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    try {
        connection = getConnection();
        String sql = "INSERT INTO users (name, age) VALUES (?, ?)";
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, name);
        preparedStatement.setInt(2, age);
        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        closeResource(connection, preparedStatement);
    }
}

2、查询数据:

public static List<Map<String, Object>> queryData() {
    List<Map<String, Object>> resultList = new ArrayList<>();
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    try {
        connection = getConnection();
        String sql = "SELECT * FROM users";
        preparedStatement = connection.prepareStatement(sql);
        resultSet = preparedStatement.executeQuery();
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();
        while (resultSet.next()) {
            Map<String, Object> rowData = new HashMap<>();
            for (int i = 1; i <= columnCount; i++) {
                rowData.put(metaData.getColumnLabel(i), resultSet.getObject(i));
            }
            resultList.add(rowData);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        closeResource(connection, preparedStatement, resultSet);
    }
    return resultList;
}

3、更新数据:

public static void updateData(int id, String newName, int newAge) {
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    try {
        connection = getConnection();
        String sql = "UPDATE users SET name = ?, age = ? WHERE id = ?";
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, newName);
        preparedStatement.setInt(2, newAge);
        preparedStatement.setInt(3, id);
        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        closeResource(connection, preparedStatement);
    }
}

4、删除数据:

java连接mysql增删改查四层结构

public static void deleteData(int id) {
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    try {
        connection = getConnection();
        String sql = "DELETE FROM users WHERE id = ?";
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, id);
        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        closeResource(connection, preparedStatement);
    }
}

原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/640813.html

(0)
未希新媒体运营
上一篇 2024-05-21 08:54
下一篇 2024-05-21 08:55

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

云产品限时秒杀。精选云产品高防服务器,20M大带宽限量抢购  >>点击进入