Thêm sửa xóa trong java
Trong ví dụ như này mình sẽ thực hiện kết nối java cho tới MySQL thực hiện jOOQ nhằm insert/update/delete dữ liệu
Tạo database
Đầu tiên mình sản xuất database library với table tự động như sau:
CREATE DATABASE `library`;USE `library`;CREATE TABLE `author` ( `id` int NOT NULL, `first_name` varchar(255) default NULL, `last_name` varchar(255) default NULL, PRIMARY KEY (`id`));
Tạo Maven Project
Bạn đang xem: Thêm sửa xóa trong java

Thư viện sử dụng:
4.0.0 giayphutyeuthuong.vn jOOQDemo 0.0.1-SNAPSHOT 1.8 1.8 mysql mysql-connector-java 8.0.16 org.jooq jooq 3.11.11 org.jooq jooq-meta 3.11.11 org.jooq jooq-codegen 3.11.11 org.jooq jooq-codegen-maven jooq generate-sources generate $maven.generate-sources.skip mysql mysql-connector-java 8.0.16 com.mysql.cj.jdbc.Driver jdbc:mysql://localhost:3306/library root admin1234 org.jooq.codegen.JavaGenerator org.jooq.meta.mysql.MySQLDatabase .* library giayphutyeuthuong.vn.jooq src/main/java Phần class entity mình sẽ thực hiện generate tự database
(Xem lại: Code ví dụ như jOOQ, generate/tạo class trường đoản cú table, database)
Thực hiện tại insert data
package giayphutyeuthuong.vn.jooq.test;import java.sql.Connection;import java.sql.DriverManager;import org.jooq.DSLContext;import org.jooq.SQLDialect;import org.jooq.impl.DSL;import giayphutyeuthuong.vn.jooq.Tables;import giayphutyeuthuong.vn.jooq.tables.records.AuthorRecord;public class DemoInsert public static void main(String args) throws Exception String user = "root"; String password = "admin1234"; String url = "jdbc:mysql://localhost:3306/library"; try (Connection connection = DriverManager.getConnection(url, user, password)) DSLContext dslContext = DSL.using(connection, SQLDialect.MYSQL); AuthorRecord r1 = new AuthorRecord(1, "Tolstoy", "Lev Nikolayevich"); AuthorRecord r2 = new AuthorRecord(2, "Shakespeare", "William"); dslContext.newRecord(Tables.AUTHOR, r1).store(); dslContext.newRecord(Tables.AUTHOR, r2).store(); // or dslContext .insertInto(Tables.AUTHOR, Tables.AUTHOR.ID, Tables.AUTHOR.FIRST_NAME, Tables.AUTHOR.LAST_NAME) .values(3, "Conan Doyle", "Arthur") .values(4, "Hugo", "Victor") .execute(); // or dslContext .insertInto(Tables.AUTHOR) .set(Tables.AUTHOR.ID, 5) .set(Tables.AUTHOR.FIRST_NAME, "Rowling") .set(Tables.AUTHOR.LAST_NAME, "J.K") .execute(); System.out.println("Inserted!"); catch (Exception e) e.printStackTrace(); Để thực hiện xử lý (truy vấn, thêm, sửa, xóa) data với database ta dùng đối tượng người tiêu dùng dslContext
dslContext trong jOOQ giống như như session/entityManager trong hibernate giỏi Statement trong jdbc
Ta hoàn toàn có thể thực hiện insert đối tượng người sử dụng giống như Hibernate bằng phương pháp sử dụng newRecord(). Hoặc thực hiện hàm insertInto() để chứng minh giá trị từng field.
Chạy tệp tin DemoInsert.java
Kết quả:
Inserted!
Xem thêm: : Phong Trào Cách Mạng 1930 1931 Có Ý Nghĩa Như Một Cuộc Tập Dượt Đầu Tiên Của

Thực hiện tại update dữ liệu:
Ví dụ sửa last name thành Stepenie với first name thành Meyer đối với phiên bản ghi gồm id = 5
package giayphutyeuthuong.vn.jooq.test;import java.sql.Connection;import java.sql.DriverManager;import org.jooq.DSLContext;import org.jooq.SQLDialect;import org.jooq.impl.DSL;import giayphutyeuthuong.vn.jooq.Tables;public class DemoUpdate public static void main(String args) throws Exception String user = "root"; String password = "admin1234"; String url = "jdbc:mysql://localhost:3306/library"; try (Connection connection = DriverManager.getConnection(url, user, password)) DSLContext dslContext = DSL.using(connection, SQLDialect.MYSQL); dslContext.update(Tables.AUTHOR).set(Tables.AUTHOR.FIRST_NAME, "Meyer") .set(Tables.AUTHOR.LAST_NAME, "Stephenie").where(Tables.AUTHOR.ID.eq(5)).execute(); System.out.println("Updated!"); catch (Exception e) e.printStackTrace(); Để update tài liệu ta sử dụng hàm update() với điều kiện where() (Trong bài bác truy vấn data bởi jOOQ mình đã nói rõ về method where())
Chạy file DemoUpdate.java
Updated!
Xem thêm: Cách Xét Học Sinh Giỏi Thpt Năm 2022 Chính Xác Nhất, Cách Xét Học Sinh Giỏi Thpt Năm 2021

Xóa dữ liệu
Tương từ bỏ với update, để xóa dữ liệu ta dùng method delete()
Ví dụ xóa các phiên bản ghi gồm id > 3
package giayphutyeuthuong.vn.jooq.test;import java.sql.Connection;import java.sql.DriverManager;import org.jooq.DSLContext;import org.jooq.SQLDialect;import org.jooq.impl.DSL;import giayphutyeuthuong.vn.jooq.Tables;public class DemoDelete public static void main(String args) throws Exception String user = "root"; String password = "admin1234"; String url = "jdbc:mysql://localhost:3306/library"; try (Connection connection = DriverManager.getConnection(url, user, password)) DSLContext dslContext = DSL.using(connection, SQLDialect.MYSQL); dslContext.deleteFrom(Tables.AUTHOR).where(Tables.AUTHOR.ID.gt(3)).execute(); System.out.println("Deleted!"); catch (Exception e) e.printStackTrace(); Chạy file DemoDelete.java