Derby 数据库先容(2)--利用

金歌  金牌会员 | 2024-12-22 18:43:39 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 919|帖子 919|积分 2757

本文主要先容 Derby 的根本利用(简介可参考:Derby 数据库先容(1)--简介),文中所利用到的软件版本:Java 1.8.0_341、Derby 10.14.2.0。
1、嵌入模式

直接利用 JDBC 连接数据库,可创建和启动数据库。
1.1、内存数据库
  1. @Test
  2. public void embeddedMemory() throws SQLException {
  3.     String user = "admin";
  4.     Connection con = JdbcUtil.getConnection("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:memory:myDb;create=true", user, "");
  5.     business(con, user);
  6.     con.close();
  7. }
  8. private void business(Connection con, String user) throws SQLException {
  9.     String tableName = "a_student";
  10.     Statement st = con.createStatement();
  11.     String sql = "select 1 from sys.sysTABLES a,sys.sysschemas b where a.schemaid=b.schemaid and upper(b.schemaname)=? and upper(a.tablename)=?";
  12.     PreparedStatement pst = con.prepareStatement(sql);
  13.     pst.setString(1, user.toUpperCase());
  14.     pst.setString(2, tableName.toUpperCase());
  15.     ResultSet rs = pst.executeQuery();
  16.     if (!rs.next()) {//表不存在则创建并初始化数据,这里根据业务需要进行操作
  17.         st.executeUpdate("create table " + tableName + "(id int, name varchar(32))");
  18.         st.executeUpdate("insert into " + tableName + "(id,name) values (1,'李白')");
  19.         st.executeUpdate("insert into " + tableName + "(id,name) values (2,'杜甫')");
  20.     }
  21.     rs = st.executeQuery("select * from " + tableName);
  22.     while (rs.next()) {
  23.         log.info("id={},name={}", rs.getInt("id"), rs.getString("name"));
  24.     }
  25. }
复制代码
1.2、文件数据库
  1. @Test
  2. public void embeddedFile() throws SQLException {
  3.     String user = "admin";
  4.     Connection con = JdbcUtil.getConnection("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:d:/temp/myDb;create=true", user, "");
  5.     business(con, user);
  6.     con.close();
  7. }
复制代码
1.3、资源数据库
  1. @Test
  2. public void embeddedJar() throws SQLException {
  3.     String user = "admin";
  4.     Connection con = JdbcUtil.getConnection("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:classpath:myDb", user, "");
  5.     business(con, user);
  6.     con.close();
  7. }
复制代码
jar 包不在 classpath 之中:
  1. @Test
  2. public void embeddedJar2() throws SQLException {
  3.     String user = "admin";
  4.     Connection con = JdbcUtil.getConnection("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:jar:(d:/temp/my.jar)myDb", user, "");
  5.     business(con, user);
  6.     con.close();
  7. }
复制代码
2、服务器模式

在服务端启动 Derby 数据库服务:
  1. cd ${DERBY_HOME}/bin
  2. NetworkServerControl.bat start -p 1527 -noSecurityManager
复制代码
客户端通过 JDBC 访问 Derby 服务。
2.1、内存数据库
  1. @Test
  2. public void networkMemory() throws SQLException {
  3.     String user = "admin";
  4.     Connection con = JdbcUtil.getConnection("org.apache.derby.jdbc.ClientDriver", "jdbc:derby://localhost:1527/memory:myDb;create=true", user, "123456");
  5.     business(con, user);
  6.     con.close();
  7. }
复制代码
2.2、文件数据库
  1. @Test
  2. public void networkFile() throws SQLException {
  3.     String user = "admin";
  4.     Connection con = JdbcUtil.getConnection("org.apache.derby.jdbc.ClientDriver", "jdbc:derby://localhost:1527/d:/temp/myDb;create=true", user, "123456");
  5.     business(con, user);
  6.     con.close();
  7. }
复制代码
2.3、资源数据库
  1. @Test
  2. public void networkJar() throws SQLException {
  3.     String user = "admin";
  4.     Connection con = JdbcUtil.getConnection("org.apache.derby.jdbc.ClientDriver", "jdbc:derby://localhost:1527/classpath:myDb", user, "123");
  5.     business(con, user);
  6.     con.close();
  7. }
复制代码
jar 包不在 classpath 之中:
  1. @Test
  2. public void networkJar2() throws SQLException {
  3.     String user = "admin";
  4.     Connection con = JdbcUtil.getConnection("org.apache.derby.jdbc.ClientDriver", "jdbc:derby://localhost:1527/jar:(d:/temp/my.jar)myDb", user, "123");
  5.     business(con, user);
  6.     con.close();
  7. }
复制代码
3、混合模式

应用通过代码的方式启动数据库服务,应用内访问数据库可以利用进程模式,其他应用通过服务器模式访问。
  1. @Test
  2. public void networkServerControl() throws Exception {
  3.     NetworkServerControl server = new NetworkServerControl(InetAddress.getByName("0.0.0.0"), 1527);
  4.     server.start (null);
  5.     CountDownLatch countDownLatch = new CountDownLatch(1);
  6.     new Thread(() -> {
  7.         try {
  8.             //模拟其他应用访问
  9.             networkMemory();
  10.         } catch (Exception e) {
  11.             e.printStackTrace();
  12.         }
  13.         countDownLatch.countDown();
  14.     }).start();
  15.     countDownLatch.await();
  16.     server.shutdown();
  17. }
复制代码
 
 
完整代码:
  1. package com.abc.demo.db;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.derby.drda.NetworkServerControl;
  4. import org.junit.Test;
  5. import java.net.InetAddress;
  6. import java.sql.*;
  7. import java.util.concurrent.CountDownLatch;
  8. @Slf4j
  9. public class DerbyCae {
  10.     @Test
  11.     public void embeddedMemory() throws SQLException {
  12.         String user = "admin";
  13.         Connection con = JdbcUtil.getConnection("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:memory:myDb;create=true", user, "");
  14.         business(con, user);
  15.         con.close();
  16.     }
  17.     @Test
  18.     public void embeddedFile() throws SQLException {
  19.         String user = "admin";
  20.         Connection con = JdbcUtil.getConnection("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:d:/temp/myDb;create=true", user, "");
  21.         business(con, user);
  22.         con.close();
  23.     }
  24.     @Test
  25.     public void embeddedJar() throws SQLException {
  26.         String user = "admin";
  27.         Connection con = JdbcUtil.getConnection("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:classpath:myDb", user, "");
  28.         business(con, user);
  29.         con.close();
  30.     }
  31.     @Test
  32.     public void embeddedJar2() throws SQLException {
  33.         String user = "admin";
  34.         Connection con = JdbcUtil.getConnection("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:jar:(d:/temp/my.jar)myDb", user, "");
  35.         business(con, user);
  36.         con.close();
  37.     }
  38.     @Test
  39.     public void networkMemory() throws SQLException {
  40.         String user = "admin";
  41.         Connection con = JdbcUtil.getConnection("org.apache.derby.jdbc.ClientDriver", "jdbc:derby://localhost:1527/memory:myDb;create=true", user, "123456");
  42.         business(con, user);
  43.         con.close();
  44.     }
  45.     @Test
  46.     public void networkFile() throws SQLException {
  47.         String user = "admin";
  48.         Connection con = JdbcUtil.getConnection("org.apache.derby.jdbc.ClientDriver", "jdbc:derby://localhost:1527/d:/temp/myDb;create=true", user, "123456");
  49.         business(con, user);
  50.         con.close();
  51.     }
  52.     @Test
  53.     public void networkJar() throws SQLException {
  54.         String user = "admin";
  55.         Connection con = JdbcUtil.getConnection("org.apache.derby.jdbc.ClientDriver", "jdbc:derby://localhost:1527/classpath:myDb", user, "123");
  56.         business(con, user);
  57.         con.close();
  58.     }
  59.     @Test
  60.     public void networkJar2() throws SQLException {
  61.         String user = "admin";
  62.         Connection con = JdbcUtil.getConnection("org.apache.derby.jdbc.ClientDriver", "jdbc:derby://localhost:1527/jar:(d:/temp/my.jar)myDb", user, "123");
  63.         business(con, user);
  64.         con.close();
  65.     }
  66.     @Test
  67.     public void networkServerControl() throws Exception {
  68.         NetworkServerControl server = new NetworkServerControl(InetAddress.getByName("0.0.0.0"), 1527);
  69.         server.start (null);
  70.         CountDownLatch countDownLatch = new CountDownLatch(1);
  71.         new Thread(() -> {
  72.             try {
  73.                 //模拟其他应用访问
  74.                 networkMemory();
  75.             } catch (Exception e) {
  76.                 e.printStackTrace();
  77.             }
  78.             countDownLatch.countDown();
  79.         }).start();
  80.         countDownLatch.await();
  81.         server.shutdown();
  82.     }
  83.     private void business(Connection con, String user) throws SQLException {
  84.         String tableName = "a_student";
  85.         Statement st = con.createStatement();
  86.         String sql = "select 1 from sys.sysTABLES a,sys.sysschemas b where a.schemaid=b.schemaid and upper(b.schemaname)=? and upper(a.tablename)=?";
  87.         PreparedStatement pst = con.prepareStatement(sql);
  88.         pst.setString(1, user.toUpperCase());
  89.         pst.setString(2, tableName.toUpperCase());
  90.         ResultSet rs = pst.executeQuery();
  91.         if (!rs.next()) {//表不存在则创建并初始化数据,这里根据业务需要进行操作
  92.             st.executeUpdate("create table " + tableName + "(id int, name varchar(32))");
  93.             st.executeUpdate("insert into " + tableName + "(id,name) values (1,'李白')");
  94.             st.executeUpdate("insert into " + tableName + "(id,name) values (2,'杜甫')");
  95.         }
  96.         rs = st.executeQuery("select * from " + tableName);
  97.         while (rs.next()) {
  98.             log.info("id={},name={}", rs.getInt("id"), rs.getString("name"));
  99.         }
  100.     }
  101. }
复制代码
DerbyCae.java
  1. package com.abc.demo.db;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.sql.*;
  4. @Slf4j
  5. public class JdbcUtil {
  6.     private JdbcUtil() {}
  7.     public static Connection getConnection(String driver, String url, String username, String password) {
  8.         Connection con = null;
  9.         try {
  10.             Class.forName(driver);
  11.             con = DriverManager.getConnection(url, username, password);
  12.         } catch (ClassNotFoundException | SQLException e) {
  13.             log.warn("url={},username={},password={}", url, username, password);
  14.             e.printStackTrace();
  15.         }
  16.         return con;
  17.     }
  18. }
复制代码
JdbcUtil.java 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

金歌

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表