Detailed explanation of java derby

download db-derby-10.10.2.0-bin

Derby is a relational database that comes with Java. It is completely developed based on Java and is very small.

Since JDK1.6, each installation of jdk will bring its own derby database. Since the version of the database that comes with jdk is not new enough, it can also be downloaded separately from the Internet. I downloaded db-derby-10.10.2.0-bin. After downloading, unzip the large D drive.

Configure environment variables

Since derby is developed in pure Java, environment variables must be configured. First of all, jdk must be configured. I will omit this configuration and focus on the configuration of derby.

Inside the environment variables:

Placement DERBY_HOME


 config: PATH

Configure CLASSPATH

To use Derby, the tool must be included in the CLASSPATH as follows:

c:/Program Files/Java/jdk1.6.0/db/derby.jar

c:/Program Files/Java/jdk1.6.0/db/derbytools.jar

In the derby.jar file contains the driver for the JDBC embedded schema; in the derbytools.jar contains the ij tool.

After the configuration is complete, enter at the command line: ij

At this point, the derby configuration is successful.

Two operating modes of the Derby database

1) Embedded mode. The Derby database shares the same JVM with the application, which automatically starts or stops the relational engine on startup and shutdown, respectively. The derby.jar file of the Derby package contains the Derby database engine and embedded JDBC driver;

  1. Go to the directory C:\Users\Administrator\Desktop\test\database  
  2. enter ij  
  3. Enter connect 'jdbc:derby:test;'  

Note: Inline mode has one and only one connection.

2) Network server mode. The Derby database occupies a single JVM and runs as a separate process on the server. In this mode, multiple applications are allowed to access the same Derby database. Derby's derbynet.jar file contains

  1. Start the service  
  2.   
  3. java -jarD:\db-derby-10.9.1.0-bin\lib\derbyrun.jar server start(改成你的derby目录)  
  4.   
  5. 连接数据库  
  6.   
  7. connect'jdbc:derby://localhost:1527/myDemo';  

连接数据库

connect 'jdbc:derby:D:/db-derby-10.10.2.0-bin/demo/databases/toursdb;user=ylsoft;password=hwyl;create=true';

通过内嵌模式连接数据库toursdb,如果数据库不存在则创建一个并连接,如果存在,则直接连接。

创建一个up_temp表

 

create table up_temp(id varchar(36) not null,address varchar(100) not null,content varchar(500),version varchar(10) not null,node_id varchar(36) not null,type varchar(2) not null,local_temp varchar(200) not null,createtime timestamp not null,filename varchar(50) not null,primary key(id));

插入一条数据

 

INSERT INTO up_temp (id,address,content,version,node_id,type,local_temp,createtime) VALUES ('2f843624-cb6f-49f0-aab6-59c4307f5954','http://36.110.44.138:44685/downloadAction!download.action?fid=2ff67da3-74bf-43a0-9a6c-301e6b4af325','测试','1.0.0.0','2f843624-cb6f-49f0-aab6-59c4307f5954','1','http://36.110.44.138:44685/downloadAction!download.action?fid=2ff67da3-74bf-43a0-9a6c-301e6b4af325','2017-04-10 15:35:00');

此时为止,数据库里面有数据了。

下面看个例子

这个例子我数据库的位置在:D:\Program Files\Java\jdk1.7.0_79\jre\cacheup

所以我创建的是cacheup数据库

嵌入式的驱动:

 

<dependency>
	  <groupId>org.apache.derby</groupId>
	  <artifactId>derby</artifactId>
	  <version>10.10.2.0</version>
	</dependency>
 注意:嵌入式模式与网络服务器模式的驱动不同。

 

网络模式驱动:org.apache.derby.jdbc.ClientDriver

 

package derby;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

public class DerbyTest{
  private static String driver = "org.apache.derby.jdbc.EmbeddedDriver";
  private static String protocol = "jdbc:derby:";
  private static String dbName = System.getProperty("java.home") + "\\cacheup";
  private static String username = "ylsoft";
  private static String userpwd = "hwyl";

  public static void loadDriver() {
    try {
      Class.forName(driver).newInstance();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) throws Exception{
    System.out.println(protocol + dbName + ";user=" + username + ";password=" + userpwd + ";create=true");
    loadDriver();
    System.out.println(get("up_temp"));
  }

  public static String get(String tablename) throws Exception{
    Map rowData = new HashMap();
    try {
      Connection conn = DriverManager.getConnection(protocol + dbName + ";user=" + username + ";password=" + userpwd + ";create=true");
      Statement statement = conn.createStatement();
      ResultSet resultSet = statement.executeQuery("select * from " + tablename);
      ResultSetMetaData md = resultSet.getMetaData();
      int columnCount = md.getColumnCount();
      while (resultSet.next()) {
        for (int i = 1; i <= columnCount; i++) {
          rowData.put(md.getColumnName(i), resultSet.getObject(i));
        }
      }
      conn.close();
      statement.close();
      resultSet.close();
    } catch (Exception e1) {
      e1.printStackTrace();
    }
    return rowData.toString();
  }
}
 The operation is the same as the jdbc operation, which is very simple.

 

Web server mode:

       Network Mode Application of Derby Database

        The derby database can not only work in embedded mode, like other databases, but also in network mode

Serving multiple applications at the same time, handling database operation requests from different JVMs .

1. The working principle of network mode derby

        If you have used some other large databases, such as oracle, db2, etc., then the network of the database should be

The principle of the working mode (c/s mode) is quite familiar.

        In this mode derby works like other databases on the network as an independent server, waiting for other

Connection requests for java applications. In this mode, derby can serve different java applications at the same time, processing data from different JVMs

database operation requests, that is to say, multiple applications can access the same derby database at the same time.

        Description: When it is not suitable to operate with embedded, such as the application and the database are not on the same machine,

Or when the database requires concurrent access by multiple users, derby in network mode can be used.

2. Operate derby in network mode

        Next, we will introduce how to operate derby in network mode, including starting derby in network mode.

and derby using the ij tool to connect network mode.

①Start derby in network mode

        Before operating the derby database in network mode, first start the derby database server in network mode.

下面给出了启动命令的基本格式:

        java org.apache.derby.drda.NetworkServerControl start [-h <host> [-p <port>]]

        NetworkServerControl是系统提供的一个用于启动derby服务器的类,位于derbynet.jar文件中。

因此需要将derbynet.jar文件路径添加到系统的classpath环境变量中。

        方括号中为可选参数,"-h <host>"用来指定主机,"-p <port>"用来指定端口号,

默认的主机为localhost,默认的端口号为1527。

        例如,下面给出了一个启动derby数据库服务器工作在本机1527端口的命令。

        java org.apache.derby.drda.NetworkServerControl start -h localhost -p 1572

        当成功启动derby数据库的网络服务后,可以从网路中的其他机器或本机来对该机器中的derby数据库

进行操作了。需要特别注意的是,在执行网路操作的过程中,cmd窗口不能关闭,一旦关闭则将断开网路服务。

②使用ij工具连接网络模式的derby

        当derby数据库的网路服务启动后,可以使用ij工具进行连接和操作,

下面给出了ij中连接网络模式derby的名称格式。

        connect 'jdbc:derby://<服务器主机地址>:<服务器端口号>/<数据库路径>[;create=True|False]';

        提示:使用上述命令连接derby数据库服务器之前首先需要将derbyclient.jar文件路径添加到系统的

classpath环境变量中,

例如,下面的命令连接上了上面启动的derby数据库服务。

         connect  'jdbc:derby://localhost:1572/E:/roway';

        如果上述命令在ij中成功执行,则与指定数据库的连接成功建立,接着就可以使用各种命令对所连接的

数据库进行操作了。对网络derby数据库进行操作的命令与嵌入式derby完全相同。

3、开发启动derby网络服务的程序

       实际应用中,不但可以通过前面介绍的命令来启动的derby数据库的网路服务,也可以通过自己开发的

java程序来启动derby数据库的网络服务。

       从前面的介绍中可以发现,通过命令启动derby数据库的网络服务需要使用

org.apache.derby.drda.NetworkServerControl类。同样,自己开发程序启动derby数据库的网络服务也需要

使用这个类,有兴趣的可以自行查阅API。

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326525110&siteId=291194637