Apache Derby database engine and embedded JDBC driver technology in the Java class library
Apache Derby database engine and embedded JDBC driver technology principles
Apache Derby (formerly known as Java DB) is a Java -based relationship database management system (RDBMS). It is a pure Java -written SQL and JDBC API, which is widely used in Java applications.Derby can run as an independent server, or it can be integrated into the Java application in an embedded manner.
This article will introduce the technical principles of the Apache Derby database engine and embedded JDBC driver, and provide some related Java code examples.
1. Apache Derby database engine
1. The principle of database engine
The Apache Derby database engine is a full Java database engine that is developed by IBM and provided in an open source manner.Derby uses B-Tree index to optimize data access to support transactions and concurrency control, and at the same time provides many advanced functions such as storage procedures and triggers.
2. Configuration and startup of Derby database
To use the Derby database engine, you first need to add Derby's dependencies to your Java project. You can implement it through Maven or manually downloading Derby's jar files.Next, you need to configure the attributes of the Derby database, such as the database name, user name, password, etc.When the application starts, you can use the following code to start the Derby database:
import org.apache.derby.impl.jdbc.EmbeddedDriver;
public class DerbyDB {
public static void main(String[] args) {
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Connection connection = DriverManager.getConnection("jdbc:derby:sampleDB;create=true");
// Execute the database operation
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
In the above examples, we use DriverManager to obtain database connections by loading Derby's embedded drivers and using DriverManager.Parameter `create = true` indicates that if the database does not exist, create a new database.
3. CRUD operation of Derby database
Next, we will demonstrate the example code of the CRUD (creation, reading, updating, and deleting) operation of the Derby database:
import java.sql.*;
public class DerbyCRUD {
public static void main(String[] args) {
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Connection connection = DriverManager.getConnection("jdbc:derby:sampleDB");
Statement statement = connection.createStatement();
// Create a table
String createTable = "CREATE TABLE employees (id INT PRIMARY KEY, name VARCHAR(30))";
statement.executeUpdate(createTable);
// Insert data
String insertData = "INSERT INTO employees VALUES (1, 'Alice'), (2, 'Bob')";
statement.executeUpdate(insertData);
// Query data
ResultSet resultSet = statement.executeQuery("SELECT * FROM employees");
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println("ID: " + id + ", Name: " + name);
}
// update data
String updateData = "UPDATE employees SET name='Charlie' WHERE id=1";
statement.executeUpdate(updateData);
// delete data
String deleteData = "DELETE FROM employees WHERE id=2";
statement.executeUpdate(deleteData);
resultSet.close();
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
In the above code, we first used the SQL DDL (data definition language) to create a table called "Employees", and then inserted two data with SQL DML (data operation language).Next, we used the SQL query statement to retrieve all the data in the table and update and delete data through SQL DML.
Second, embedded JDBC driver technology
1. Embedded JDBC driver principle
Embedded JDBC driver refers to integrating database engines into Java applications and accessing and operating databases through simple Java APIs.Compared with independent database servers, embedded JDBC drivers can achieve lighterweight and higher -performance database access.
2. Derby's embedded JDBC driver implementation
Derby provides a class called `Embeddeddriver`, which is the embedded JDBC driver of Derby.The class name of the embedded driver is `ORG.APACHE.DERBY.JDBC.embeddeddriver`, which can be loaded and registered the driver by calling the` newInstance` method.
3. Use embedded JDBC driver to connect Derby database
The code of code using an embedded JDBC driver connect to the Derby database is as follows:
import org.apache.derby.jdbc.EmbeddedDriver;
public class DerbyJDBCEmbedded {
public static void main(String[] args) {
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Connection connection = DriverManager.getConnection("jdbc:derby:sampleDB;create=true");
// Execute the database operation
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
In the above examples, we use the embedded JDBC driver to load and register the Derby's embedded driver, and obtain the database connection through the `DriverManager.getConnection" method.
in conclusion
This article introduces the principle of Apache Derby database engine and embedded JDBC drive, and provides corresponding Java code examples.By using the DERBY database engine and embedded JDBC driver, you can easily integrate and use relationship databases in Java applications to achieve durable and efficient database operations of data.