Jade Dungeon

Derby

Derby介绍

内嵌模式

内嵌(embedded)模式非常轻便,是在JVM进程内部的。 这样,如果有第二个个JVM访问同一个目录的数据会报错。

内嵌模式下驱动的名字应为org.apache.derby.jdbc.EmbeddedDriver

创建新数据,在协议后面加入create=true

conn = DriverManager.getConnection("jdbc:derby:helloDB;create=true", props);  

数据库以文件的文件存在,默认是在当前目录,也可以指定完整路径:

jdbc:derby://opt/workspace1/idb;create=true

也可以把路径指定在系统属性中:

System.setProperty("derby.system.home","/opt/xxxx/");

关闭数据库,shutdown=true

DriverManager.getConnection("jdbc:derby:helloDB;shutdown=true ");

关闭整个Derby引擎:

DriverManager.getConnection("jdbc:derby:;shutdown=true");  

例子:

public class HelloJavaDB {  

	public static void main(String[] args) {  
		try {
			// load the driver  
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();  
			System.out.println("Load the embedded driver");  

			Connection conn = null;  
			Properties props = new Properties();  
			props.put("user", "user1");
			props.put("password", "user1");  

			//create and connect the database named helloDB   
			conn=DriverManager.getConnection("jdbc:derby:helloDB;create=true", props);  
			System.out.println("create and connect to helloDB");  
			conn.setAutoCommit(false);  
  
			// create a table and insert two records  
			Statement s = conn.createStatement();  
			s.execute("create table hellotable(name varchar(40), score int)");  
			System.out.println("Created table hellotable");  
			s.execute("insert into hellotable values ('Ruth Cao', 86)");  
			s.execute("insert into hellotable values ('Flora Shi', 92)");  

			// list the two records  
			ResultSet rs = s.executeQuery(  
				"SELECT name, score FROM hellotable ORDER BY score");  
			System.out.println("name\t\tscore");  
			while(rs.next()) {  
				StringBuilder builder = new StringBuilder(rs.getString(1));  
				builder.append("\t");  
				builder.append(rs.getInt(2));  
				System.out.println(builder.toString());  
			}  

			// delete the table  
			s.execute("drop table hellotable");  
			System.out.println("Dropped table hellotable");  
			  
			rs.close();  
			s.close();  
			System.out.println("Closed result set and statement");  

			conn.commit();  
			conn.close();  
			System.out.println("Committed transaction and closed connection");  
			  
			try {
				// perform a clean shutdown   
				DriverManager.getConnection("jdbc:derby:;shutdown=true");  
			} catch (SQLException se) {  
				System.out.println("Database shut down normally");  
			}  
		} catch (Throwable e) {  
			// handle the exception  
		}  
		System.out.println("SimpleApp finished");  
	}

}  

运行:

java –cp .;$JAVA_HOME/db/lib/derby.jar HelloJavaDB

网络模式

客户端

驱动类为:org.apache.derby.jdbc.ClientDriver

据库的协议:

jdbc:derby://server[:port]/databaseName[;attributeKey=value]

Derby默认1527端口,例如:

jdbc:derby://localhost:1527/

客户端例子:

public class HelloJavaDB {  

	public static String driver = "org.apache.derby.jdbc.EmbeddedDriver";  
	public static String protocol = "jdbc:derby:";  
  
	public static void main(String[] args) {  
		// same as before  
	}  
	
	private static void parseArguments(String[] args) {  
		if (args.length == 0 || args.length > 1) {  
			return;  
		}  
		if (args[0].equalsIgnoreCase("derbyclient")) {  
			framework = "derbyclient";  
			driver = "org.apache.derby.jdbc.ClientDriver";  
			protocol = "jdbc:derby://localhost:1527/";  
		}  
	}  
}  

网络服务

Derby 中控制网络服务器的类是:org.apache.derby.drda.NetworkServerControl

参数start启动服务,不带参数的话列出帮助选项。

java -cp .:$JAVA_HOME/db/lib/derby.jar:$JAVA_HOME/db/lib/derbynet.jar \
	org.apache.derby.drda.NetworkServerControl start

如果不再需要服务器,那么使用NetworkServerControlshutdown参数就能够关闭服务器。

java -cp .:$JAVA_HOME/db/lib/derby.jar:$JAVA_HOME/db/lib/derbynet.jar \
	org.apache.derby.drda.NetworkServerControl shutdown

相对应的,网络客户端的实现被包含在derbyclient.jar中。 所以,只需要在classpath中加入该 jar 文件,修改后的客户端就可以顺利地读取数据了。 再一次尝试着使用两个命令行窗口去连接数据库,就能够得到正确的结果了。

Derby作为Servlet

在web应用中启动网络服务器NetworkServer:这个类是在derby库目录下的derbynet.jar中

启动NetworkServer可以远程维护数据库。 derby提供了一个简单的servlet:org.apache.derby.drda.NetServlet, 这个servlet可以在web应用启动时自动加载NetworkServer。配置方法如下:

<servlet> 
	<servlet-name>derbynet</servlet-name> 
	<servlet-class>org.apache.derby.drda.NetServlet</servlet-class> 
	<init-param> 
		<param-name>startNetworkServerOnInit</param-name> 
		<param-value>true</param-value> 
	</init-param> 
	<init-param> 
		<param-name>portNumber</param-name> 
		<param-value>1527</param-value> 
	</init-param> 
	<init-param> 
		<param-name>host</param-name> 
		<param-value>localhost</param-value> 
	</init-param> 
	<init-param> 
		<param-name>tracingDirectory</param-name> 
		<param-value /> 
	</init-param> 
	<load-on-startup>1</load-on-startup> 
</servlet>

如果参数host设置为localhost,则服务器仅接受来自本机的连接,如果设定为网络域名 ,就可以接受外部连接,但会导致安全问题,在接受外部连接前,应将其运行在Java security manager下,并启用用户认证。

其他工具

简单介绍几个 Java DB 的小工具来加快开发速度。 它们都位于org.apache.derby.tools包内,在开发过程中需要获取信息或者测试可以用到。

  • ij:一个用来运行 SQL 脚本的工具;
  • dblook:为 Derby 数据库作模式提取(Schema extraction),生成 DDL 的工具;
  • sysinfo:显示系统以及 Derby 信息的工具类;

管理工具ij

要加入jar包和配置环境变量DERBY_INSTALL

export JAVA_HOME=/usr/lib/jvm/default-java
export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/db/lib/derbytools.jar:$JAVA_HOME/db/lib/derby.jar
export DERBY_INSTALL=$JAVA_HOME/db

检查一下配置得是不是正确:

$ java org.apache.derby.tools.sysinfo

------------------ Java Information ------------------
Java Version:    1.8.0_60
Java Vendor:     Oracle Corporation
Java home:       /usr/lib/jvm/java-8-oracle/jre
Java classpath:  .:/usr/lib/jvm/default-java/jre/lib:/usr/lib/jvm/default-java/db/lib/derbytools.jar:/usr/lib/jvm/default-java/db/lib/derby.jar
OS name:         Linux
OS architecture: amd64
OS version:      3.13.0-65-generic
Java user name:  jade
Java user home:  /home/jade
Java user dir:   /home/jade/tmp
java.specification.name: Java Platform API Specification
java.specification.version: 1.8
java.runtime.version: 1.8.0_60-b27
--------- Derby Information --------
[/usr/lib/jvm/java-8-oracle/db/lib/derby.jar] 10.11.1.2 - (1629631)
[/usr/lib/jvm/java-8-oracle/db/lib/derbytools.jar] 10.11.1.2 - (1629631)
------------------------------------------------------
----------------- Locale Information -----------------
Current Locale :  [English/United States [en_US]]
Found support for locale: [cs]
         version: 10.11.1.2 - (1629631)
Found support for locale: [de_DE]
         version: 10.11.1.2 - (1629631)
Found support for locale: [es]
         version: 10.11.1.2 - (1629631)
Found support for locale: [fr]
         version: 10.11.1.2 - (1629631)

启动ij

java org.apache.derby.tools.ij

在ij中创建及连接数据库

connect 'jdbc:derby:<数据库路径>[;create=True/False]';

简单的使用例子:

$ java org.apache.derby.tools.ij
ij version 10.11
ij> connect 'jdbc:derby:testdb;create=True';

ij> CREATE TABLE test_table(name VARCHAR(40), score INT);
0 rows inserted/updated/deleted

ij> INSERT INTO test_table values ('Ruth Cao', 86);
1 row inserted/updated/deleted

ij> INSERT INTO test_table values ('Flora Shi', 92);
1 row inserted/updated/deleted

ij> SELECT name, score From test_table ORDER BY score;
NAME                                    |SCORE      
----------------------------------------------------
Ruth Cao                                |86         
Flora Shi                               |92         

2 rows selected

使用run命令运行SQL脚本:

run filename.sql

例,students.sql

create table students(
	id  numeric(20),
	name  varchar(30),
	age   numeric(6)
);
insert into students values(10001,'Aa',10);
insert into students values(10002,'Bb',20);
insert into students values(10003,'Cc',30);
select * from students;  

调用:

ij> run '/opt/tmp/students.sql';
ij> create table students(
	id  numeric(20),
	name  varchar(30),
	age   numeric(6)
);
0 rows inserted/updated/deleted
ij> insert into students values(10001,'Aa',10);
1 row inserted/updated/deleted
ij> insert into students values(10002,'Bb',20);
1 row inserted/updated/deleted
ij> insert into students values(10003,'Cc',30);
1 row inserted/updated/deleted
ij> select * from students;
ID                   |NAME                          |AGE    
------------------------------------------------------------
10001                |Aa                            |10     
10002                |Bb                            |20     
10003                |Cc                            |30     

3 rows selected

退出:

  • disconnect;:断开数据库连接
  • Exit;:退出ij 工具