本文参考资料: http://www.cecs./~monge/classes/share/usingEclipseDerby.html http://www.ibm.com/developerworks/opensource/library/os-ad-eclipse/ http://db./derby/integrate/plugin_howto.html http://db./derby/papers/DerbyTut/ij_intro.html 安装Derby Plugin到Eclipse: 首先从Derby下载页面 查找最新官方发布的版本(或者需要的版本),下载 derby_core_plugin 和
derby_ui_plugin这两个必须的Eclipse插件下载到计算, org.apache.derby.core_10.6.2 ,
org.apache.derby.ui_1.1.2 , 和
org.apache.derby.plugin.doc_1.1.2这样3个名称的 使用Derby同Eclipse : ------------------
Java Information ------------------
在选择sysInfo的同时,能看到一个叫ij(Interactive
SQL)的选项,顾名思义,这是Derby提供的结构查询语言脚本的运行工具,用来执行SQL脚本语句。Java Version: 1.6.0_18 Java Vendor: Sun Microsystems Inc. Java home: /usr/local/jdk1.6.0_18/jre Java classpath: /windows/E/Computer Study/Software Study/Programming/Eclipse Workspace On Ubuntu 10.10/AccountManagement/bin:/opt/eclipse/plugins/org.apache.derby.core_10.6.2/derbytools.jar:/opt/eclipse/plugins/org.apache.derby.core_10.6.2/derby.jar:/opt/eclipse/plugins/org.apache.derby.core_10.6.2/derbynet.jar:/opt/eclipse/plugins/org.apache.derby.core_10.6.2/derbyclient.jar OS name: Linux OS architecture: i386 OS version: 2.6.35-22-generic Java user name: knowyourself1010 Java user home: /home/knowyourself1010 Java user dir: /windows/E/Computer Study/Software Study/Programming/Eclipse Workspace On Ubuntu 10.10/AccountManagement java.specification.name: Java Platform API Specification java.specification.version: 1.6 java.runtime.version: 1.6.0_18-b07 --------- Derby Information -------- JRE - JDBC: Java SE 6 - JDBC 4.0 [/opt/eclipse/plugins/org.apache.derby.core_10.6.2/derby.jar] 10.6.2.1 - (999685) [/opt/eclipse/plugins/org.apache.derby.core_10.6.2/derbytools.jar] 10.6.2.1 - (999685) [/opt/eclipse/plugins/org.apache.derby.core_10.6.2/derbynet.jar] 10.6.2.1 - (999685) [/opt/eclipse/plugins/org.apache.derby.core_10.6.2/derbyclient.jar] 10.6.2.1 - (999685) ------------------------------------------------------ ----------------- Locale Information ----------------- ------------------------------------------------------ 选择它,Eclipse的控制台会显示: ij
version 10.6
在选择sysInfo的位置选择start Derby Network
Server启动Derby网络服务,接着就可以在eclipse控制台中ij>後输入SQL脚本语句了。ij> 首先,按照http://db./derby/papers/DerbyTut/ij_intro.html#ij_create 创建一个数据库命令如下: ij>
connect 'jdbc:derby:MyDbTest;create=true';
如果得到:IJ ERROR: Unable to
establish connection的提示,可能是你的Derby Network
Server没有开启。如果执行成功,刷新PackageExplorer中的项目目录,便能看到一个名为MyDbTest的文件夹和一个derby.log文件。可以打开derby.log文件瞧瞧。 在ij中输入help;可以查看帮助: 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 SHOW SCHEMAS; -- lists all schemas in the current database SHOW [ TABLES | VIEWS | PROCEDURES | FUNCTIONS | SYNONYMS ] { IN schema }; -- lists tables, views, procedures, functions or synonyms SHOW INDEXES { IN schema | FROM table }; -- lists indexes in a schema, or for a table SHOW ROLES; -- lists all defined roles in the database, -- sorted SHOW ENABLED_ROLES; -- lists the enabled roles for the current -- connection (to see current role use -- VALUES CURRENT_ROLE), sorted SHOW SETTABLE_ROLES; -- lists the roles which can be set for the -- current connection, sorted DESCRIBE name; -- lists columns in the named table 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] [WITH {HOLD | NOHOLD}] CURSOR name AS 'SQL-J query'; -- gets a cursor (JDBC result set) on the query -- the default is a forward-only cursor with holdability 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 is not 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. 输入show connections;可以看到当前链接,如下: ij>
show connections;
CONNECTION0* - jdbc:derby:MyDbTest * = current connection 接在来根据ij介绍,在当前数据库(MyDbTest)中创建表derbyDB,并增加修改数据,控制台输入输出如下: ij>
create table derbyDB(num int, addr varchar(40));
依次执行下面命令,断开链接并退出ij:0 rows inserted/updated/deleted ij> insert into derbyDB values (1956,'Webster St.'); 1 row inserted/updated/deleted ij> insert into derbyDB values (1910,'Union St.'); 1 row inserted/updated/deleted ij> select * from derbyDB; NUM |ADDR ---------------------------------------------------- 1956 |Webster St. 1910 |Union St. 2 rows selected ij> update derbyDB set num=180,addr='Grand Ave.' where num=1956; 1 row inserted/updated/deleted ij> select * from derbyDB; NUM |ADDR ---------------------------------------------------- 180 |Grand Ave. 1910 |Union St. 2 rows selected ij>
disconnect;
另,执行SQL脚本文件的命令为:ij> show connections; No connections available. ij> exit; ij>
run 'my_file.sql';
在程序中链接MyDbTest数据库的命令有: Connection conn =DriverManager.getConnection("jdbc:derby:MyDbTest");
或者:
jdbc:derby:../otherDirectory/MyDbTest
jdbc:derby:directory:MyDbTest
jdbc:derby:jar:(c:/derby/lib/jar2.jar)MyDbTest
更多链接方式请见:http://db./derby/docs/dev/devguide/cdevdvlp34964.html |
|
来自: 漂泊的燕子 > 《eclipase》