1、下载
下载对应系统版本的安装包,解压即可。下载地址
2、部署方式
2.1、内嵌数据库
2.1.1、ij客户端连接使用
1.运行ij.bat;
2.创建数据库
jdbc:协议;
derby:本地连接;
d:/derbydb:数据库路径,即文件夹路径
create=true:不存在则创建
3.连接数据库
4.创建表、插入数据、执行脚本
2.1.2、java程序使用
1.引入依赖
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.14.2.0</version>
</dependency>
2.使用
public class DerbyTest {
static String baseDir = System.getProperty("user.dir");
public Connection getConnection(String dbPath) {
Connection connection = null;
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
connection = DriverManager.getConnection("jdbc:derby:" + dbPath + ";create=true;");
System.out.println(connection == null ? "未获取到连接" : "成功获取到连接");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return connection;
}
@Test
public void createTable() throws SQLException {
String dbPath = baseDir + File.separator + "db01";
System.out.println(baseDir);
String sql = "create table test(id int primary key ,name varchar(50))";
Connection connection = getConnection(dbPath);
Statement statement = connection.createStatement();
statement.execute(sql);
// PreparedStatement preparedStatement = connection.prepareStatement(sql);
// boolean execute = preparedStatement.execute();
connection.close();
}
@Test
public void insert() throws SQLException {
String dbPath = baseDir + File.separator + "db01";
System.out.println(baseDir);
String sql = "insert into test values(1,'apple'),(2,'banana')";
Connection connection = getConnection(dbPath);
Statement statement = connection.createStatement();
statement.execute(sql);
connection.close();
}
@Test
public void select() throws SQLException {
String dbPath = baseDir + File.separator + "db01";
System.out.println(baseDir);
String sql = "select * from test";
Connection connection = getConnection(dbPath);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int anInt = resultSet.getInt(1);
String string = resultSet.getString(2);
System.out.println("id=" + anInt + ",name=" + string);
}
connection.close();
}