注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Computer Science

I Pressed My Words Here. Will You Read ?

 
 
 

日志

 
 

在Eclipse下,从安装到使用Derby插件  

2010-11-26 13:54:54|  分类: With Ubuntu |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
正文:为什么选择Derby?Derby数据库的性能比较:PDF文档,点击下载


本文参考资料:
http://www.cecs.csulb.edu/~monge/classes/share/usingEclipseDerby.html
http://www.ibm.com/developerworks/opensource/library/os-ad-eclipse/
http://db.apache.org/derby/integrate/plugin_howto.html
http://db.apache.org/derby/papers/DerbyTut/ij_intro.html

装Derby Plugin到Eclipse:
首先从Derby下载页面 查找最新官方发布的版本(或者需要的版本),下载derby_core_pluginderby_ui_plugin这两个必须的Eclipse插件下载到计算机,并解压缩到Eclipse目录下的plugins文件夹下。注意,压缩包中所有文件可能都默认在plugin文件夹下,所以解压缩後,將得到plugin文件夹(也可能会叫plugin(1))下的所有文件拷贝到eclipse安装目录下的plugin文件夹中。
被拷贝的文件应该是
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
首先,添加DerbyJava项目。运行Eclipse(如果Eclipse在安装Derby前就开着,请重新启动Eclipse)。在Package Explorer中选择Java项目,右键,依次选择Apache Derby,Add Apache Derby nature.到此,添加Derby完成。
你可以再次在项目上点击右键,Apache Derby,选择sysInfo(Derby System Information),便能在Eclipse的控制台输出中看到Derby系统信息。如下:
------------------ Java Information ------------------
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 -----------------
------------------------------------------------------
在选择sysInfo的同时,能看到一个叫ij(Interactive SQL)的选项,顾名思义,这是Derby提供的结构查询语言脚本的运行工具,用来执行SQL脚本语句。选择它,Eclipse的控制台会显示:
ij version 10.6
ij>
在选择sysInfo的位置选择start Derby Network Server启动Derby网络服务,接着就可以在eclipse控制台中ij>後输入SQL脚本语句了。
首先,按照http://db.apache.org/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));
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:
ij> disconnect;
ij> show connections;
No connections available.
ij> exit;
另,执行SQL脚本文件的命令为:
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.apache.org/derby/docs/dev/devguide/cdevdvlp34964.html

WillMill - Computer ScienceThe next version of Ubuntu is coming soonThe next version of Ubuntu is coming soonThe next version of Ubuntu is coming soonThe next version of Ubuntu is coming soon
广而告之
  评论这张
 
阅读(1761)| 评论(1)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017