set CLASSPATH=%CLASSPATH%;%DERBY_INSTALL%\lib\derbytools.jar;.
注意:在这两个示例中,CLASSPATH
环境变量的初始化(通过 export
或 set
shell 命令)都用两个步骤完成。这纯粹是因为本文篇幅所限。可以在自己的变量初始化中使用一行或两行。
现在只要打开新命令提示,就可以运行该批处理文件。不管是 Windows 还是 UNIX® 脚本文件,一定要确保正确指定安装 Derby 软件的目录。
正确设置了环境之后,可以运行 ij 工具来创建新的目录,如 清单 1
所示。
清单 1. 使用 ij 工具
rb$ echo $CLASSPATH
/opt/Apache/db-derby-10.1.2.1-bin/lib/derby.jar:
/opt/Apache/db-derby-10.1.2.1-bin/lib/derbytools.jar:.
rb$ mkdir derbyWork
rb$ cd derbyWork
rb$ java org.apache.derby.tools.ij
ij version 10.1
ij> connect 'jdbc:derby:test;create=true' ;
ij> exit ;
rb$ ls -CF
derby.log test/
首先,显示 CLASSPATH
环境变量。这样做有时是避免以后出错的好办法。在这里,Derby JAR
文件明显存在。下一步是创建并更改为可以工作的新目录(在本例中目录叫做 derbyWork,但您可以随意使用任何名称)。这就更容易看清使用 ij
工具创建新数据库时发生的事情。预备工作完成之后,可以开始启动 ij 了。
因为 ij 工具是 Java 应用程序,因此需要启动 Java Virtual Machine (JVM),并指出要运行的相应主类的名称,在这里是 ji 工具。如果获得 Java 异常,则重新检查 CLASSPATH
以确保 derbytools JAR 文件存在。
ij 工具功能十分强大。可用它(在将来的文章中)执行范围广泛的操作,包括创建数据库(如前所述)和在数据库内创建新项。ij 工具包括一个 help
命令,可以列出和描述要使用的一些常见命令,如 清单 3
所示。注意,为了适应空间限制,该清单已被重新格式化,所以您的版本看起来可能稍有不同。
清单 3. 获得 ij 工具的帮助
rb$ java org.apache.derby.tools.ij
ij version 10.1
ij> help ;
Supported commands include:
PROTOCOL 'JDBC protocol' [ AS ident ];
-- sets a default or named protocol
DRIVER 'class for driver'; -- loads the named class
CONNECT 'url for database' [ PROTOCOL namedProtocol ]
[ AS connectionName ];
-- connects to database URL
-- and may assign identifier
SET CONNECTION connectionName; -- switches to the specified
connection
SHOW CONNECTIONS; -- lists all connections
AUTOCOMMIT [ ON | OFF ]; -- sets autocommit mode for the
-- connection
DISCONNECT [ CURRENT | connectionName | ALL ];
-- drop current, named, or all
-- connections; the default is CURRENT
COMMIT; -- commits the current transaction
ROLLBACK; -- rolls back the current transaction
PREPARE name AS 'SQL-J text'; -- prepares the SQL-J text
EXECUTE { name | 'SQL-J text' } [ USING { name | 'SQL-J text' } ] ;
-- executes the statement with
-- parameter values from the USING
-- result set row
REMOVE name; -- removes the named previously
-- prepared statement
RUN 'filename'; -- run commands from the named file
ELAPSEDTIME [ ON | OFF ]; -- sets elapsed time mode for ij
MAXIMUMDISPLAYWIDTH integerValue;
-- sets the maximum display width for
-- each column to integerValue
ASYNC name 'SQL-J text'; -- run the command in another thread
WAIT FOR name; -- wait for result of ASYNC'd command
GET [SCROLL INSENSITIVE] CURSOR name AS 'SQL-J query';
-- gets a cursor (JDBC result set)
-- on the query
-- SCROLL cursors are only available
-- in JDBC 2.0 and higher.
-- (Cursor scroll type is ignored in
-- JDBC 1.X.)
NEXT name; -- gets the next row from the
-- named cursor
FIRST name; -- gets the first row from the
-- named scroll cursor
LAST name; -- gets the last row from the
-- named scroll cursor
PREVIOUS name; -- gets the previous row from the
-- named scroll cursor
ABSOLUTE integer name; -- positions the named scroll cursor
-- at the absolute row number
-- (A negative number denotes
-- position from the last row.)
RELATIVE integer name; -- positions the named scroll cursor
-- relative to the current row
-- (integer is number of rows)
AFTER LAST name; -- positions the named scroll cursor
-- after the last row
BEFORE FIRST name; -- positions the named scroll cursor
-- before the first row
GETCURRENTROWNUMBER name; -- returns the row number for the
-- current position of the named
-- scroll cursor (0 is returned when
-- the cursor isn't positioned
-- on a row.)
CLOSE name; -- closes the named cursor
LOCALIZEDDISPLAY [ ON | OFF ];
-- controls locale sensitive data
-- representation
EXIT; -- exits ij
HELP; -- shows this message
Any unrecognized commands are treated as potential SQL-J commands
and executed directly.
ij>
清单 3
所示的大多数命令似乎都很陌生,当然啦,您还刚开始学习 Apache Derby。
还可以使用 ij 工具与数据库建立多个连接,如 清单 4
所示。
清单 4. 用 ij 工具建立连接
ij> connect 'jdbc:derby:test;create=true' ;
WARNING 01J01: Database 'test' not created,
connection made to existing database instead.
ij> connect 'jdbc:derby:test' ;
ij(CONNECTION1)> show connections ;
CONNECTION0 - jdbc:derby:test
CONNECTION1* - jdbc:derby:test
* = current connection
ij(CONNECTION1)> disconnect ;
ij> show connections ;
CONNECTION0 - jdbc:derby:test
No current connection
ij> set connection CONNECTION0 ;
ij> show connections ;
CONNECTION0* - jdbc:derby:test
* = current connection
ij> exit ;
该结果说明,尽管有警告,但创建数据库连接的第一次尝试成功了。为了展示已经有到同一数据库的两个连接,发出 show connections ;
命令,将显示两个连接、关联的 URL 以及当前连接(在本例为 CONNECTION1
)。
现在不需要到同一数据库的两个连接,因此可以使用 disconnect ;
命令关闭当前连接。发出另一个 show connections ;
命令将显示当前惟一打开的连接,但还会看到没有当前连接。因为需要具有活动的或当前的连接向特定数据库发送命令,所以应该相应地更改当前连接。使用 set connections
命令,将目标连接名称作为最终参数,可以容易地实现这一点。现在,发出另一 show connections ;
命令时,将看到当前连接的列表以及当前连接的名称。最后,发出 exit ;
命令断开所有当前连接并终止 ij 工具。