In -depth interpretation of the technical principles of Apache Derby database engine and embedded JDBC drive

Apache Derby is a pure Java relationship database management system (RDBMS), which is designed as lightweight, easy to use and has a small memory occupation.This article will in -depth interpretation of the technical principles of the Apache Derby database engine and embedded JDBC -driven, and provide Java code examples. ## Apache Derby database engine technical principle Apache Derby uses a architectural mode called "embedded", which means that it can run in the same process of the application and share the same JVM as the application.Its internal architecture adopts scalable-oriented interface programming model.The following are the key concepts and technical principles of the Apache Derby database engine: ### data storage Apache Derby uses tables to store data, and each table can contain multiple columns.The database engine stores the metadata of the table and column in the system directory (Sys.Sysysystem and SYS.Syscolumns).Derby stores data and tables and index metadata on a physical file, and uses transaction logs for persistent storage.By default, Derby stored the database in a directory called "Derby". ### sql execute Apache Derby supports SQL query and update operations.When the application sends SQL statements to Derby database, these statements will be parsed, optimized and executed by Derby.During the parsing phase, Derby decomposed SQL statements into internal data structures and verified grammar and semantics.Then, in the optimization stage, Derby evaluates multiple execution plans and selects the optimal plan.Finally, in the execution phase, Derby executes the query or update operation, and returns the result to the application. ### affairs management Apache Derby supports ACID (atomic, consistency, isolation and persistence).In Derby, transactions are automatically managed by database engines.Applications can control the boundaries of transactions by starting and submitting transactions.Derby uses a logging mechanism to ensure the durability of the transaction so that it can be returned to the unsurmitable changes after the system collapse or power off. ### concurrent control Apache Derby supports multiple users' concurrent access.Derby uses row locks to achieve concurrent control.When multiple transactions access the same data at the same time, Derby will process the access permissions and locks of data according to the isolation level of the transaction (such as unsurremmation, reading, repeated reading, and serialization). ## embedded JDBC driver technology principle Apache Derby provides an interface for interactive JDBC drivers as an application.The following are the key concepts and technical principles of embedded JDBC drive: ### Connection management Applications can be connected to the Derby database using the embedded JDBC driver.By providing connection parameters such as JDBC URL, database user name and password, applications can create a connection object.Derby uses an internal connection manager to manage the connection to ensure the correct release of resources and the correct execution of transactions. ### sql execute Through the JDBC connection object, the application can send SQL statements to Derby database.The embedded JDBC driver transforms the SQL statement into a Derby's internal execution plan and returns the result to the application.Applications can obtain query results, update data, and perform storage procedures through the JDBC API. ### affairs management Applications can use the embedded JDBC driver to manage transactions.By setting the transaction processing attribute of the connection object, the application can start and submit transactions.Derby embedded JDBC driver to ensure the isolation and persistence of transactions to ensure the integrity and consistency of the data. ### abnormal processing The embedded JDBC driver will throw various abnormalities to deal with possible errors, such as failed connection, errors of SQL execution and rolling transactions.Applications can use Try-Catch blocks to deal with these abnormalities and take corresponding measures to restore or roll back. ## java code example Here are a simple Java code example using Apache Derby to embed the JDBC driver to show the process of connecting to Derby database, creating tables, inserting data, and executing query: import java.sql.*; public class DerbyExample { public static void main(String[] args) { try (Connection conn = DriverManager.getConnection("jdbc:derby:myDB;create=true"); Statement stmt = conn.createStatement()) { // Create a table stmt.executeUpdate("CREATE TABLE Customers (id INT PRIMARY KEY, name VARCHAR(255))"); // Insert data stmt.executeUpdate("INSERT INTO Customers (id, name) VALUES (1, 'John')"); stmt.executeUpdate("INSERT INTO Customers (id, name) VALUES (2, 'Jane')"); // Execute the query ResultSet rs = stmt.executeQuery("SELECT * FROM Customers"); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); System.out.println("ID: " + id + ", Name: " + name); } } catch (SQLException e) { e.printStackTrace(); } } } The above example demonstrates tables, insert data, and execute queries that are connected to Derby database and created "Customers".By using the JDBC driver, Java applications can easily interact with the Derby database.