gpt4 book ai didi

java - 使用 Apache Derby 的内存嵌入式服务器

转载 作者:行者123 更新时间:2023-12-01 10:09:48 27 4
gpt4 key购买 nike

是否有一个很好的示例来说明如何启动嵌入式服务器(即,仅当应用程序在线时才在线的数据库服务器)

另外,它是否可以仅存在于内存中?

最佳答案

下载 Apache Derby 并将项目中的引用添加到以下文件:

derby.jar
derbynet.jar
derbyclient.jar

使用以下代码启动服务器并创建内存数据库:

import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLNonTransientConnectionException;
import java.util.ArrayList;
import java.util.List;
import org.apache.derby.drda.NetworkServerControl;

public class DerbyInMemoryEmbeddedServer {

public static void main(String[] args) throws Exception {
NetworkServerControl server = new NetworkServerControl(InetAddress.getByName("localhost"), 1528);
server.start(null);

String connectionString = "jdbc:derby://localhost:1528/memory:hello_world_database;create=true";
Connection conn = DriverManager.getConnection(connectionString);
System.out.println("Server started");

//create the database here. The following is an example:

//create the users table
try (PreparedStatement ps = conn.prepareStatement(
"create table users ("
+ "id int not null generated always as identity, "
+ "username varchar(8000), "
+ "first_name varchar(8000), "
+ "surname varchar(8000), "
+ "primary key (id), "
+ "constraint uc_username unique (username)"
+ ")")) {
ps.execute();
}

//create an index
try (PreparedStatement ps = conn.prepareStatement(
"create index ix_surname on users (surname)")) {
ps.execute();
}

//add some users
List<String> names = new ArrayList<>();
names.add("jhuff|Jeffrey|Huffines");
names.add("woodsey223|Sherwood|Gurule");
names.add("tim_w|Tim|Woods");
try (PreparedStatement ps = conn.prepareStatement("insert into users (username, first_name, surname) values (?, ?, ?)")) {

for (String name : names) {
String[] tokens = name.split("\\|");

ps.setString(1, tokens[0]);
ps.setString(2, tokens[1]);
ps.setString(3, tokens[2]);
ps.addBatch();
}

ps.executeBatch();
}

//create the team table
try (PreparedStatement ps = conn.prepareStatement(
"create table team ("
+ "id int not null generated always as identity, "
+ "name varchar(8000), "
+ "primary key (id),"
+ "constraint uc_team_name unique (name)"
+ ")")) {
ps.execute();
}

//insert a team
try (PreparedStatement ps = conn.prepareStatement(
"insert into team (name) values (?)")) {
ps.setString(1, "Wombats");
ps.executeUpdate();
}

//create a many-to-many table to link teams to users. (Since a user can belong to more than one team)
try (PreparedStatement ps = conn.prepareStatement(
"create table team_member ("
+ "id int not null generated always as identity, "
+ "team_id int not null, "
+ "user_id int not null,"
+ "constraint fk_team_member_team foreign key (team_id) references team(id), "
+ "constraint fl_team_member_user foreign key (user_id) references users(id), "
+ "primary key (id)"
+ ")")) {
ps.execute();
}

//create team members
List<String> teamMembers = new ArrayList<>();
teamMembers.add("jhuff|Wombats");
teamMembers.add("tim_w|Wombats");

try (PreparedStatement ps = conn.prepareStatement("insert into team_member (team_id, user_id) values ("
+ "(select id from team where name = ?),"
+ "(select id from users where username = ?)"
+ ")")) {

for (String teamMember : teamMembers) {
String[] tokens = teamMember.split("\\|");

ps.setString(1, tokens[1]);
ps.setString(2, tokens[0]);
ps.addBatch();
}
ps.executeBatch();
}

System.out.println("Server ready");
System.in.read();

System.out.println("Disconnecting");
String shutdownConnectionString = connectionString + ";shutdown=true";

try {
DriverManager.getConnection(shutdownConnectionString);
} catch (SQLNonTransientConnectionException ex) {
//derby throws an exception on disconnection
}

System.out.println("Disconnected");
}
}

如果您最终希望数据库在应用程序重新启动之间保持不变,请改用此连接字符串:

jdbc:derby://localhost:1528/jdbc:derby:C:\db_files\proof_of_concept;create=true;

关于java - 使用 Apache Derby 的内存嵌入式服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36199916/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com