1、下载

下载对应系统版本的安装包,解压即可。下载地址
image.png

2、部署方式

2.1、内嵌数据库

可以不用配置环境变量。

2.1.1、ij客户端连接使用

1.运行ij.bat;
image.png

2.创建数据库
image.png
jdbc:协议;
derby:本地连接;
d:/derbydb:数据库路径,即文件夹路径
create=true:不存在则创建
image.png

3.连接数据库
image.png

4.创建表、插入数据、执行脚本
image.png

2.1.2、java程序使用

1.引入依赖

  1. <dependency>
  2. <groupId>org.apache.derby</groupId>
  3. <artifactId>derby</artifactId>
  4. <version>10.14.2.0</version>
  5. </dependency>

2.使用

  1. public class DerbyTest {
  2. static String baseDir = System.getProperty("user.dir");
  3. public Connection getConnection(String dbPath) {
  4. Connection connection = null;
  5. try {
  6. Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
  7. connection = DriverManager.getConnection("jdbc:derby:" + dbPath + ";create=true;");
  8. System.out.println(connection == null ? "未获取到连接" : "成功获取到连接");
  9. } catch (ClassNotFoundException | SQLException e) {
  10. e.printStackTrace();
  11. }
  12. return connection;
  13. }
  14. @Test
  15. public void createTable() throws SQLException {
  16. String dbPath = baseDir + File.separator + "db01";
  17. System.out.println(baseDir);
  18. String sql = "create table test(id int primary key ,name varchar(50))";
  19. Connection connection = getConnection(dbPath);
  20. Statement statement = connection.createStatement();
  21. statement.execute(sql);
  22. // PreparedStatement preparedStatement = connection.prepareStatement(sql);
  23. // boolean execute = preparedStatement.execute();
  24. connection.close();
  25. }
  26. @Test
  27. public void insert() throws SQLException {
  28. String dbPath = baseDir + File.separator + "db01";
  29. System.out.println(baseDir);
  30. String sql = "insert into test values(1,'apple'),(2,'banana')";
  31. Connection connection = getConnection(dbPath);
  32. Statement statement = connection.createStatement();
  33. statement.execute(sql);
  34. connection.close();
  35. }
  36. @Test
  37. public void select() throws SQLException {
  38. String dbPath = baseDir + File.separator + "db01";
  39. System.out.println(baseDir);
  40. String sql = "select * from test";
  41. Connection connection = getConnection(dbPath);
  42. Statement statement = connection.createStatement();
  43. ResultSet resultSet = statement.executeQuery(sql);
  44. while (resultSet.next()) {
  45. int anInt = resultSet.getInt(1);
  46. String string = resultSet.getString(2);
  47. System.out.println("id=" + anInt + ",name=" + string);
  48. }
  49. connection.close();
  50. }

2.2、独立数据库