在 Eclipse 中开发 Apache Derby 应用程序

合集下载

Eclipse IDE开发环境使用教程说明书

Eclipse IDE开发环境使用教程说明书

iAbout the T utorialEclipse is an integrated development environment (IDE) for Java and other programming languages like C, C++, PHP, and Ruby etc. Development environment provided by Eclipse includes the Eclipse Java development tools (JDT) for Java, Eclipse CDT for C/C++, and Eclipse PDT for PHP, among others.This tutorial will teach you how to use Eclipse in your day-2-day life while developing any software project using Eclipse IDE. We will give special emphasis on Java project. AudienceThis tutorial has been prepared for beginners to help them understand basic functionality of Eclipse tool. After completing this tutorial, you will find yourself at a moderate level of expertise in using Eclipse IDE from where you can take yourself to next levels. PrerequisitesWe assume you are going to use Eclipse IDE to handle all levels of Java projects development. So it will be good if you have knowledge of software development using any programming language specially Java programming.Copyright & Disclaimer© Copyright 2015 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book can retain a copy for future reference but commercial use of this data is not allowed. Distribution or republishing any content or a part of the content of this e-book in any manner is also not allowed without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or inthistutorial,******************************************iT able of ContentsAbout the Tutorial (i)Audience (i)Prerequisites (i)Copyright & Disclaimer (i)Table of Contents .................................................................................................................................... i i 1.OVERVIEW (1)What is Eclipse? (1)Licensing (1)Eclipse Releases (1)2.INSTALLATION (3)Downloading Eclipse (3)Installing Eclipse (3)Launching Eclipse (4)3.EXPLORE WINDOWS (5)Parts of an Eclipse Window (5)Using Multiple Windows (6)4.EXPLORE MENUS (7)Typical Eclipse Menus (7)Brief Description of Menus (8)Customizing Menus (8)5.EXPLORE VIEWS (9)About Views (9)Organizing Views (9)Moving Views (9)Creating View Folders (10)Opening a view (10)6.PERSPECTIVES (13)What is a Perspective? (13)Opening a Perspective (13)Switching between Perspectives (13)Closing a Perspective (14)Customizing a Perspective (14)7.WORKSPACES (16)About Eclipse Workspace (16)UI Elements for Managing the Workspace (16)8.CREATE JAVA PROJECT (18)Opening the New Java Project wizard (18)Using the New Java Project wizard (18)Viewing the Newly Created Project (20)9.CREATE JAVA PACKAGE (21)Opening the New Java Package wizard (21)Using the New Java Package Wizard (21)Viewing the Newly Created Package (22)10.CREATE JAVA CLASS (23)Opening the New Java Class Wizard (23)Using the New Java Class Wizard (23)Viewing the Newly Created Java class (24)11.CREATE JAVA INTERFACE (25)Opening the New Java Interface Wizard (25)Using the New Java Interface Wizard (25)Viewing the Newly Created Java Interface (26)12.CREATE XML FILE (27)Opening the New XML File wizard (27)Using the New XML File wizard (28)Viewing the Newly Created XML File (29)13.JAVA BUILD PATH (30)Setting the Java Build Path (30)14.RUN CONFIGURATION (31)Creating and Using a Run Configuration (31)15.RUNNING A PROGRAM (33)Running a Java Program (33)16.CREATE JAR FILES (35)Opening the Jar File wizard (35)Using the Jar File wizard (35)17.CLOSE PROJECT (37)Why Close a Project? (37)How to Close a Project? (37)Closed Project in Package Explorer (38)18.REOPEN PROJECT (39)Reopening a Closed Project (39)19.BUILD PROJECT (40)Building a Java Project (40)20.DEBUG CONFIGURATION (42)Creating and Using a Debug Configuration (42)21.DEBUGGING A PROGRAM (44)Debugging a Java Program (44)22.PREFERENCES (48)Setting Preferences (48)23.CONTENT ASSIST (50)Using Content Assist (50)24.QUICK FIX (52)Using Quix Fix (52)25.HOVER HELP (54)Using Hover Help (54)26.SEARCH MENU (56)Searching the Workspace (56)27.NAVIGATION (58)Navigating the Eclipse Workspace (58)Open Type (58)Open Type in Hierarchy (60)Open Resource (61)28.REFACTORING (63)Refactoring using Eclipse (63)29.ADD BOOKMARKS (64)About Bookmarks (64)Adding a Bookmark (64)Opening the Bookmarks View (64)Using the Bookmarks View (65)30.TASK MANAGEMENT (66)Managing Tasks (66)Opening the Tasks View (67)Using the Tasks View (67)31.INSTALL PLUGINS (69)Locating and Installing Plug-ins (69)32.CODE TEMPLATES (73)Using Code Templates (73)Modifying/Adding code templates (74)33.SHORTCUTS (75)About Shortcuts (75)34.RESTART OPTION (78)Restarting Eclipse (78)35.TIPS & TRICKS (79)36.WEB BROWSERS (81)Internal Web Browser (81)Eclipse 7What is Eclipse?In the context of computing, Eclipse is an integrated development environment (IDE) for developing applications using the Java programming language and other programming languages such as C/C++, Python, PERL, Ruby etc.The Eclipse platform which provides the foundation for the Eclipse IDE is composed of plug-ins and is designed to be extensible using additional plug-ins. Developed using Java, the Eclipse platform can be used to develop rich client applications, integrated development environments, and other tools. Eclipse can be used as an IDE for any programming language for which a plug-in is available.The Java Development Tools (JDT) project provides a plug-in that allows Eclipse to be used as a Java IDE, PyDev is a plugin that allows Eclipse to be used as a Python IDE, C/C++ Development Tools (CDT) is a plug-in that allows Eclipse to be used for developing application using C/C++, the Eclipse Scala plug-in allows Eclipse to be used an IDE to develop Scala applications and PHPeclipse is a plug-in to eclipse that provides complete development tool for PHP.LicensingEclipse platform and other plug-ins from the Eclipse foundation is released under the Eclipse Public License (EPL). EPL ensures that Eclipse is free to download and install. It also allows Eclipse to be modified and distributed.Eclipse ReleasesEvery year, since 2006, the Eclipse foundation releases the Eclipse Platform and a number of other plug-ins in June.1.Eclipse8Eclipse 9Downloading EclipseYou can download eclipse from /downloads/. The download page lists a number of flavors of eclipse.The capabilities of each packaging of eclipse are different. Java developers typically use Eclipse Classic or Eclipse IDE for developing Java applications.The drop down box in the right corner of the download page allows you to set the operating system on which eclipse is to be installed. You can choose between Windows, Linux and Mac. Eclipse is packaged as a zip file.Installing EclipseTo install on windows, you need a tool that can extract the contents of a zip file. For example you can use:∙7-zip ∙PeaZip ∙ IZArcUsing any one of these tools, extract the contents of the eclipse zip file to any folder of your choice.2.Launching EclipseOn the windows platform, if you extracted the contents of the zip file to c:\, then you can start eclipse by using c:\eclipse\eclipse.exeWhen eclipse starts up for the first time it prompts you for the location of the workspace folder. All your data will be stored in the workspace folder. You can accept the default or choose a new location.1011If you select "Use this as the default and do not ask again", this dialog box will not come up again. You can change this preference using the Workspaces Preference Page. See the Preference tutorialpage for more details.Eclipse 12Parts of an Eclipse WindowThe major visible parts of an eclipse window are:∙Views ∙Editors (all appear in one editor area) ∙Menu Bar ∙ ToolbarAn eclipse perspective is the name given to an initial collection and arrangement of views and an editor area. The default perspective is called java. An eclipse window can have multiple perspectives open in it but only one perspective can be active at any point of time. A user can switch between open perspectives or open a new perspective. A perspective controls what appears in some menus and tool bars.3.EclipseA perspective has only one editor area in which multiple editors can be open. The editor area is usually surrounded by multiple views. In general, editors are used to edit the project data and views are used to view the project metadata. For example, the package explorer shows the java files in the project and the java editor is used to edit a java file.The eclipse window can contain multiple editors and views but only one of them is active at any given point of time. The title bar of the active editor or view looks different from all the others.The UI elements on the menu bar and tool bar represent commands that can be triggered by an end user.Using Multiple WindowsMultiple Eclipse Windows can be open at the same time. To open a new window, click on the Windows menu and select the New Window menu item.Each window can have a different perspective open in them. For example you could open two Eclipse windows one in the Java perspective and the other in the Debug perspective. The window showing the Java perspective can be used for editing the java code and the window showing the debug perspective can be used for debugging the application being developed.13Eclipse 14T ypical Eclipse MenusThe typical menus available on the menu bar of an Eclipse window are:∙File menu ∙Edit menu ∙Navigate menu ∙Search menu ∙Project menu ∙Run menu ∙Window menu ∙ Help menu4.Plug-ins can add new menus and menu items. For example when the java editor is open, you will see the Source menu and when the XML editor is open, you will see the Design menu. Brief Description of Menus15Customizing MenusThe visible menu items on a menu depend on the installed plug-ins and customization done using the Customize Perspective dialog box.16Eclipse 17About ViewsEclipse views allow users to see a graphical representation of project metadata. For example the project navigator view presents a graphical representation of the folders and files associated with a project and properties view presents a graphical representation of an element selected in another view or editor.An eclipse perspective can show any number of views and editors. All editor instances appear in a single editor area, whereas views are placed inside view folders. A workbench window can display any number of view folders. Each view folder can display one or more views. Organizing ViewsThe following picture shows four views arranged in a view folder.The picture given below shows the same four views arranged in two view folders.5.Moving ViewsTo move a view from one view folder to another, just click on the view title and drag to the title bar area of another view folder. The green line shown below is a result of dragging the title bar of the Properties view from one view folder to the title bar area of another view folder. The Properties view can be moved to where the green line is by releasing the mouse button and sending out a drop event.Creating View FoldersView folders can be dynamically created by dragging the title bar of a view to anywhere outside the editor area and title bar of another view folder. As you drag the title bar around, green lines will indicate where exactly the new view folder will be created.Moving the drag icon to the bottom of a window allows you to create a view folder that spans the entire width of the window. Moving the drag icon to the left or right edge of window allows you to create a view folder that spans the entire height of the window.18Opening a viewTo open a view, click on the Window menu and select the Show View menu item.19Clicking on the Other menu item brings up the Show View dialog box that allows you to locate and activate a view.20The views are organized by category. To quickly locate a view, just type the name of a view into the filter text box. To open a view, select it and click on the OK button. The subsequent pages of this tutorial introduce you to a number of useful views.21End of ebook previewIf you liked what you saw…Buy it from our store @ https://22。

Java程序设计精编教程(第3版)第14章-JDBC数据库操作

Java程序设计精编教程(第3版)第14章-JDBC数据库操作

本章选用Derby数据库,不仅是 为了教学的方便,更重要的是在 Java应用程序中掌握使用Derby数 据库也是十分必要的。本章并非 讲解数据库本身的知识体系,而 是讲解怎样在Java程序中使用数 据库
2021/10/18 1
§14.1.1 准备工作
1.平台的搭建
把Java安装目录\db\lib, 例如: E:\jdk1.8\db\lib
将Java安装目录下的db\bin作为系统环境变量path的一个值,以便随时在 命令行窗口中使用db\bin中的命令。
对于Windows 7/Windows XP,用鼠标右键单击“计算机”/“我的电脑”, 在弹出的快捷菜单中选择“属性”命令弹出“系统特性”对话框,再单击该 对话框中的“高级系统设置”/“高级选项”。然后单击按钮“环境变量”, 添加系统环境变量。如果曾经设置过环境变量path, 可单击该变量进行编辑 操作,将需要的值,例如E:\jdk1.8\db\bin加入即可,如图14.1所示。
delete from <表名> where <条件子句> 例如:
delete mess where number='002';
6.在数据库中删除表 drop table 表名;
2021/10/18 1
14.2.4 Derby数据库常用的基本数据类型
• smallint 取值范围 -215~215-1。例如,age smallint,其中age是字段名。
2.向表中插入记录(行) 一次插入一条记录: insert into 表名 values(字段1值,字段2值,…字段n值);
一次插入多条记录: insert into 表名 values (字段1值,字段2值,…字段n值), (字段1值,字段2值,…字段n值)…;

derby 的安装和使用

derby 的安装和使用

Derby数据库是一个纯用Java实现的内存数据库,属于Apache的一个开源项目。

由于是用Java实现的,所以可以在任何平台上运行;另外一个特点是体积小,免安装,只需要几个小jar包就可以运行了。

Derby数据库有两种运行模式:1)内嵌模式。

Derby数据库与应用程序共享同一个JVM,通常由应用程序负责启动和停止,对除启动它的应用程序外的其它应用程序不可见,即其它应用程序不可访问它;2)网络模式。

Derby数据库独占一个JVM,做为服务器上的一个独立进程运行。

在这种模式下,允许有多个应用程序来访问同一个Derby数据库。

在Apache上,Derby有4种发布包,这里以bin发布包为例。

bin发布包中含有包含了执行derby数据库工具、设置derby数据库环境的脚本文件、Demo、jar文件等。

1、安装Derby数据库只需要从Derby官方网站下载Derby的zip或者tar包,解压就可以了。

这里以db-derby-10.4.1.3-bin版本为例,解压后得到以下目录:1)bin目录,包含了一些工具脚本和设备环境的脚本;2)demo目录,包含了一些实例程序;3)docs目录,包含了Derby的文档;4)javadoc目录,包含了Derby的API文档;5)lib目录,包含了Derby数据库的jar文件;6)test目录,Derby的一些测试jar包;2、使用Derby脚本Derby提供了几个脚本来操作Derby数据库,在使用这些脚本前,你必须先设置好Derby的运行环境。

下面的例子都是在命令行下设置环境变量,这些设置都是临时的,如果你新开一个命令行窗口,则需要重新设置一遍,如果想要使环境变量永久生效,可以在我的电脑中进行设置。

首先设置好DERBY_HOME这个环境变量,为DERBY_HOME指定你的derby目录,假如你的derby解压到了E:\ db-derby-10.4.1.3-bin目录下,则可以在命令行中如下设置:set DERBY_HOME=E:\ db-derby-10.4.1.3-bin将DERBY_HOME\bin目录添加到PATH环境变量中:set path=%DERBY_HOME%\bin;%PATH%这样可以简化你稍后在命令行中的输入,否则你每次都必须使用脚本的全路径或者你必须到DERBY_HOME\bin目录中才能执行脚本。

如何用Tomcat_和Eclipse开发Web_应用程序(部署+发布)配置

如何用Tomcat_和Eclipse开发Web_应用程序(部署+发布)配置

用Tomcat 和Eclipse开发Web 应用程序(部署+发布)配置用Tomcat 和Eclipse开发Web 应用程序(部署+发布)配置。

(/developerworks/cn/opensource/os-eclipse-tomcat/)创建一个新的 Web 项目从 Eclipse IDE,选择File > New > Project查看项目向导。

在 Wizards 框,输入 Web,而 New Project 窗口会过滤向导以只显示匹配的那些项。

利用这种方法可以很容易找到向导,而无需遍历每一项。

从列表中选择Dynamic Web Project。

我们要使用 Standard Web Project Wizard 来建立静态 HTML Web 项目。

虽然您也可以在技术上使用 Tomcat 运行静态Web 站点,但 Web 服务器,比如 Apache Web Server 更适合于静态内容。

专门为服务于静态页面而设计的 Web 服务器拥有的开销更少,且专为此目的作了调优。

以运行库为目标在Target Runtime下,可以看到 <None>,如图 4 所示,由于您尚未为 Apache Tomcat 创建运行库,所以需要单击New打开New Target Runtime Wizard。

从 Apache 文件夹中选择 Apache Tomcat V5.5,如图 5 所示,然后单击Next。

图 4.创建一个新的动态 Web 项目图 5.创建一个新的服务器运行库现在,您可以命名此运行库,当然保留 Apache Tomcat V5.5 的默认名也可以。

单击Browse来定位用于 Apache Tomcat 配置的基本文件夹(我的是C:\apps\apache-tomcat-5.5.20,如图 6 所示)。

若无特殊原因,我建议不要更改,保留IBM® Java Runtime Environment (JRE) 版本作为工作台的默认 JRE。

Eclipse和MyEclipse配置derby数据库

Eclipse和MyEclipse配置derby数据库

Eclipse和MyEclipse配置derby数据库今天看到一个文档,里面给了一个例子,使用的是derby数据库,开发工具是Eclipse(3.3.0)在做项目用的是MyEclipse(6.0)为了能看看其功能,我就照配了下环境,发现了几个容易出错的问题,首先是把derby的插件复制到MyEclipse的plugins包下,然后在MyEclipse 目录打开mylipse.ini配置文件:-showsplashcom.genuitec.myeclipse.product--launcher.XXMaxPermSize256M-vmargs-Dosgi.requiredJavaVersion=1.5-Xms128m-Xmx512m-Dosgi.splashLocation=e:/MyEclipse 6.0.0M1/eclipse/MyEclipseSplash.bmpnguage=en-XX:PermSize=128M-XX:MaxPermSize=256M在256M下加入-clean,这里注意了,刚刚开始我是加在256M上面,但是重启了几回MyEclipse,就是加不进来,后来加在下面就好了,原因我现在还没弄明白呢,有高手望指教!配置好后,重启MyEclipse,右击项目,如果出现Apache Derby选项就是配置成功了!然后把它加载到项目中,在运行的时候要注意,一定要先启动MyEclipse的tomcat,然后才启动derby数据库,因为MyEclipse比较霸道,总认为自己是第一个启动的连接,所有如果先启动derby数据库,MyEclipse的tomcat就无法启动了!另外,如果是Eclipse的话,配置基本一样,就是在运行项目时,可以不分先后启动derby 数据库和Eclipse的tomcat,因为Eclipse的tomcat不抢占第一连接!尤其是注意一点,非常重要,以上配置好后,点击myeclipse的快捷方式后会弹出java无法创建虚拟机的英文提示,这是不要点它的快捷方式,点击C:/Program Files/MyEclipse 6.0/eclipse下的eclipse的快捷方式即可进入。

小巧数据库ApacheDerby使用攻略

小巧数据库ApacheDerby使用攻略

⼩巧数据库ApacheDerby使⽤攻略1. Derby 介绍将⽬光放在⼩ Derby 的原因是纯绿⾊、轻巧、内存占⽤⼩,分分钟在你机⼦跑起来,⾃⼰做点需要连接数据库的代码实践⾮常⽅便。

虽然 Mysql 也可以,多⼀种选择,不是也挺好么?Apache Derby是⼀个完全⽤ java 编写的数据库,Derby是⼀个Open source的产品。

Apache Derby⾮常⼩巧,核⼼部分derby.jar只有2M,既可以做为单独的数据库服务器使⽤,也可以内嵌在应⽤程序中使⽤。

点击进⼊版本中,注意查看 Build Environment 需要的条件,点击下载 zip 解压到随意⽬录即可。

你也可以不⽤下载,从JDK1.6 开始,derby 已经住进了 JDK 的⼤家庭,安装⽬录下⾯ db ⽂件夹就是⼩ derby,当然官⽹可以更⾃由的选择版本。

2. 稍稍配置下环境变量Derby 基于 Java 编写,你机⼦上需要存在 Java JRE,具体安装和配置就不说了。

下⾯是 Derby 需要配置的的系统环境变量,配置环境变量是想让系统知道去哪找命令对应的执⾏程序。

当然你也可以不配置任何环境变量,直接切换到 derby 的安装⽬录下⾯找到 bin ⽂件夹,双击对应的服务。

Name: DERBY_HOMEValue: E:\Java\derby\db-derby-10.10.1.1-bin在Path加⼊:%DERBY_HOME%\bin在CLASSPATH加⼊:%DERBY_HOME%\lib \derby.jar;%DERBY_HOME%\lib\derbyclient.jar;%DERBY_HOME%\lib\derbytools.jar;%DERBY_HOME%\lib\derbynet.jar 切到 cmd ⿊框下⾯,敲 sysinfo⾄此,⼩巧的 Derby 已经成功安装在你的电脑上,是不是很快?骚年,可以进⾏折腾了。

使用Eclipse进行Java应用程序开发的入门教程

使用Eclipse进行Java应用程序开发的入门教程第一章:介绍Eclipse和Java开发环境Eclipse是一款流行的集成开发环境(IDE),可用于开发各种编程语言,包括Java。

它提供了强大的工具和功能,使Java开发变得更加高效和便捷。

1.1 Eclipse的安装首先,下载适用于Java开发的Eclipse版本。

安装过程相对简单,按照向导提示顺序执行即可完成。

确保选择Java开发工具(Java Development Tools,JDT)的安装。

1.2 JDK的安装和配置Eclipse需要Java Development Kit(JDK)才能编译和运行Java 代码。

确保已安装最新版本的JDK,并将其配置为Eclipse的默认JDK。

1.3 创建Java项目启动Eclipse后,选择"File -> New -> Java Project"来创建一个新的Java项目。

在弹出的对话框中,输入项目名称并选择所需的JDK版本。

点击"Finish"来创建项目。

第二章:Eclipse的基本功能和工作空间2.1 工作空间Eclipse使用工作空间(Workspace)来组织项目和资源文件。

在启动Eclipse时,会要求选择工作空间文件夹。

一个工作空间可以包含多个项目。

2.2 项目资源Eclipse的"Package Explorer"窗口显示项目中的所有文件和文件夹。

你可以在此窗口中创建、删除和管理项目资源。

2.3 编辑器Eclipse的编辑器是主要的编码区域,用于编写和编辑Java代码。

当你双击项目中的Java文件时,它会在编辑器中打开。

2.4 调试器Eclipse提供强大的调试功能,可帮助你发现和修复代码中的错误。

你可以设置断点、单步执行代码、查看变量的值等。

调试器是开发过程中非常有用的工具。

第三章:Java项目的创建和配置3.1 创建包和类在Eclipse的"Package Explorer"窗口中,右键点击项目,选择"New -> Package"来创建一个包。

Apache Derby 构建脱机 Ajax

Apache Derby 构建脱机Ajax人们非常喜爱Ajax应用程序,以至于他们十分乐于使用Ajax应用程序而不想使用等效的桌面程序。

但惟一的问题是出现在网络无法访问的时候怎么办。

这是必须要用脱机功能的场景。

Apache Derby是支持Ajax应用程序实现脱机访问的优秀选择。

了解如何使用Apache Derby作为本地数据库,该数据库可以实现Ajax应用程序的脱机使用。

先决条件和系统要求本文将使用Apache Derby作为客户端数据库。

Derby可以单独下载,但是也被绑定到Java?6中并被称为Java DB。

在本文中,我们将把DerbyV10.4.1.3与Java 5和Java 6结合使用。

我们将利用Java Applet在浏览器中启用Derby并且使用JavaScript访问Applet。

因此,强烈建议熟悉Java Applet和JavaScript。

Derby允许使用普通JDBC和SQL,因此需要熟悉这些内容(请参阅参考资料)。

回页首Apache Derby Apache Derby是任何一个Java应用程序都可以使用的嵌入式数据库。

它是非常有用的工具,因此绑定在Java Platform,StandardEdition(Java SE)V6中。

虽然嵌入式数据库的应用不计其数,但是许多人都不知道用Derby可以实现的一些客户端功能。

我们将通过构建一个简单的地址本应用程序研究其中一些应用。

我们将从利用Apache Derby的Java Applet开始,最终实现一个使用Derby作为缓存的基于Ajax的应用程序。

数据访问对于一篇有关数据库技术的文章,应当首先从数据库代码开始讨论。

首先,让我们定义用于存储联系人的简单的表模式,如下所示:图1.Contact表您可以设想更复杂的联系人模式,如添加多个电话号码、地址等。

但是,对于我们的应用程序来说,使用目前的这种模式刚刚好。

当然,存在一个与Contact表相对应的Java类。

如何在Eclipse和MyEclipse开发工具中创建和调试基于JDBC的应用程序

如何在Eclipse和MyEclipse开发工具中创建和调试基于JDBC的应用程序1.1.1在当前的工作空间中再新建一个Java 工程项目1、工程项目的名称设置为JdbcApp2、在该project中添加一个类(1)启动程序类创建向导(2)设置类名称为JdbcAppMain,程序包名称为examples3、编程该类中的功能实现方法package examples;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.sql.*;public class JdbcAppMain extends JFrame implements WindowListener{String[] columnNames = {};Object [][]dataRows;String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs";String DBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";Connection con=null;Statement stmt=null;PreparedStatement pstmt=null;ResultSet rs=null;ResultSetMetaData metaData;public JdbcAppMain(String windowTitle){super(windowTitle);int FrameWidth=600,FrameHeight=400;this.setSize(FrameWidth,FrameHeight);this.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-FrameWidth)/2, (Toolkit.getDefaultToolkit().getScreenSize().height-FrameHeight)/2);this.addWindowListener(this);JPanel contentPanel=new JPanel();contentPanel.setLayout(new BorderLayout());this.setContentPane(contentPanel);try{Class.forName(DBDriver);}catch(ClassNotFoundException e){System.out.println("不能正确地加载驱动程序");}try{con=DriverManager.getConnection(url,"sa","1234");}catch(SQLException e){System.out.println("用户的口令不正确");}try{/*访问SQLServer2000中内置的pubs数据库中的“authors”表*/stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY ); //step 3rs=stmt.executeQuery("select * from authors");metaData=rs.getMetaData();int numberOfColumns = metaData.getColumnCount();columnNames = new String[numberOfColumns];for(int column = 0; column < numberOfColumns; column++){columnNames[column] = metaData.getColumnLabel(column+1);}//以下为获得记录集中的行数,因为此时的记录指针在第一行之前int rowCounter=0;if(st()){rowCounter=rs.getRow();}rs.beforeFirst(); //将记录指针回退到最开始处,此时必须将Cursor设置为可回滚 dataRows=new Object[rowCounter][numberOfColumns];int loopRowCounter=0;while(rs.next()){for(int column = 0; column < numberOfColumns; column++){dataRows[loopRowCounter][column]=rs.getObject(column+1);}loopRowCounter++;}rs.close();}catch(SQLException e){System.out.println("SQLState:"+e.getSQLState());System.out.println("Messager:"+e.getMessage());System.out.println("Vendor:"+e.getErrorCode());}JTable table=new JTable(dataRows,columnNames);JScrollPane jsp=new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);contentPanel.add(jsp,BorderLayout.CENTER);}public static void main(String args[]){JdbcAppMain mainframe=new JdbcAppMain("利用JDBC SQLServer2000的驱动程序连接SQLServer2000数据库");mainframe.show();}public void windowOpened(WindowEvent parm1) {}public void windowClosing(WindowEvent parm1){try{con.close();}catch(SQLException e){}this.dispose();System.exit(0);}public void windowClosed(WindowEvent parm1) {}public void windowIconified(WindowEvent parm1) {}public void windowDeiconified(WindowEvent parm1) {}public void windowActivated(WindowEvent parm1) {}public void windowDeactivated(WindowEvent parm1) {}}4、在项目中添加支持MS SQLServer数据库系统的JDBC驱动程序到本项目中(1)选择所要添加的JDBC驱动程序文件(2)点击“添加外部Jar”按钮,最后为下面的状态5、查看某个类的定义比如,我们希望查看Frame类的定义,则可以选中该Frame类,然后选择“浏览”菜单中的相应的项目。

MyEclipse6.0中Derby的应用说明

MyEclipse Derby 服务器:自动进行JDBC连接;可配置的启动选项。

是一个内嵌在MyEclipse中的数据库,据说性能优于Mysql和Access。

(Derby是小型数据库)下面介绍使用方法:1.启动数据库服务。

--就是点击启动tomcat服务器的那个“Run/Stop/Restart MyEclipse servers” 按钮。

然后“start” “MyEclipse Derby”。

启动后可以看到:2.打开Database Explorer视图,空白处右键选“new”。

3.配置数据库连接属性:其中,Driver name-随便起吧;Connection URL-test是要建立的数据库名称(不是已有的数据库名称,而是当前要新建的数据库名称);Username-随便写;Password-随便写;Jar包在:MyEclipse6.0安装目录\myeclipse\eclipse\plugins\com.genuitec.eclipse.derby_6.0.0.zmyeclipse60020070820\lib下;4.finish。

5.右键我们新建好的连接,选择“Open Connection...”。

6.右键“Connected to myderby”(myderby是上面我起的Driver name),选择“New Sql Editor”。

7.建表时类似Oracle,要将表建在某个登录权限下,例如:我上面写的用户名是“nanfeng”,我们现在要在nanfeng这个权限下建立一张表,可以这样做:8.下面是测试数据库是否建立成功,给出大家一个简单的测试类:好了,这样就做好了一个Derby的DEMO。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

在Eclipse 中开发Apache Derby 应用程序级别: 初级Gilles Roux, 信息管理软件工程师, IBM2005 年2 月01 日在开发 Apache Derby 应用程序的过程中需要执行很多任务,例如创建和连接数据库,编写Java™ JDBC 客户机应用程序和存储过程,以及将最终得到的软件组件部署到生产环境。

本文旨在发现如何结合使用各种基于 Eclipse 的 Apache Derby 工具来简化这一开发过程。

概述本文的目的是向您展示如何在Eclipse 集成开发环境(IDE)中使用几种不同的工具,例如Java Development Tools、IBM® DB2® plug-ins for Eclipse 和IBM integration plug-in for Derby,以便开发Apache Derby 应用程序。

本文将介绍一个典型Derby 应用程序的整个开发周期,从数据库的创建开始,然后经历JDBC 客户机应用程序的开发,存储过程和函数的开发,最后是解决方案的开发。

本文还将描述必要时如何用DB2 Universal Database (UDB) 数据库替代Apache Derby 数据库。

本文假设您对Apache Derby 数据库、Eclipse 平台和DB2 plug-ins for Eclipse 有基本的理解。

强烈建议您阅读参考资料一节中列出的文章“与Apache Derby 一起使用DB2 plug-ins for Eclipse”的两个部分。

为了阐明应用程序开发中涉及的各种不同任务,作者Gilles Roux 将提供关于如何构建一个示例应用程序的具体例子和逐步说明。

在这个例子中,您需要开发一个命令行应用程序来执行一家书店的库存管理。

书店的数据库存储了这家书店拥有的各种书籍,以及这些书籍的现有数量。

这个示例应用程序将允许您访问这些数据,并允许您更改书籍的数量。

例如,如果从一个供应商那里收到一批书,那么就要使用这个应用程序来添加所收到书籍的数量。

如果书籍的数量超过或者低于某个限制,则需要用电子邮件通知管理员,以便其采取必要的行动。

回页首开发环境工具Java Development Tools(JDT)是一组内建到Eclipse 中的插件,为编辑、编译、调试、执行和部署一般用途的Java 应用程序提供了一种方法。

DB2 plug-ins for Eclipse 提供了连接到各种数据库(包括IBM Cloudscape 和Apache Derby)的一组功能。

这个插件是以下几个插件的组合。

∙Connection Wizard:用于创建和连接 DB2、Cloudscape 或 Derby 数据库。

∙Database Explorer View:用于浏览数据库对象。

∙SQL Scrapbook:用于编辑和执行单独的 SQL 语句。

∙Database Output View:用于对一个表的内容进行抽样或者查看一条 SQL 语句的执行结果。

∙Migration Wizard:用于自动地将一个现有 Derby 数据库迁移到 DB2 UDB。

IBM integration plug-in for Derby 将很多有用的Derby 工具集成到了Eclipse 环境中。

下面是该工具所提供的主要功能:∙Apache Derby Nature:使 Eclipse 项目可以执行 Derby 任务。

∙Network Server:直接从 Eclipse 项目中配置和启动 Derby Network Server。

∙IJ:直接在 Eclipse 控制台中以交互模式或脚本模式启动 Derby 命令行实用程序。

∙Sysinfo:显示与项目相关的 Derby 系统信息。

DB2 plug-ins for Eclipse 和IBM Integration plug-in for Derby 是两个独立的工具,但是它们之间互补性很强,前者提供了一般数据库连接,而后者则提供了访问很多特定于Derby 特性的访问途径。

然而,很多任务都可以通过这些工具中的任意一个来执行,效果是一样的。

本文提到了执行一个给定任务的各种不同方法,从而使每个用户都可以选择他们所喜爱的工作方式。

安装工具首先要下载和安装DB2 plug-ins for Eclipse。

该产品包括DB2 插件,并且是基于Eclipse 3.0 的,后者本身就包括了JDT。

然后从 下载Apache Derby plug-in,并在安装了前面软件的基础上安装此软件。

最后,下载IBM Integration plug-in for Derby并在安装了eclipse 的基础上安装此软件。

该插件包括JCC JDBC 驱动程序和Derby 集成工具。

设置开发环境如前所述,您将使用几种不同的工具来开发应用程序:DB2 plug-ins for Eclipse、IBM Integration plug-in for Derby 和JDT。

这些工具都是基于Eclipse 的,因此它们可以很好地集成到一个单独的开发环境中。

在开发应用程序时,通常要建立一些到数据库的连接:∙使用 Database Explorer 浏览数据库。

∙使用 Derby ij 命令行实用程序执行 SQL 语句。

∙在测试时应用程序自己将连接到数据库。

Derby 数据库引擎可以在多种配置下运行。

最简单的一种是嵌入式配置,但在这里不适合,因为需要通过运行在不同Java 虚拟机上的几种工具建立连接。

而且,在生产环境中,可能需要从多个应用程序中访问数据库。

因此,这里使用Network Server 配置。

IBM Integration plug-in for Derby 提供了一种选择,以便可以很容易地从Eclipse 项目目录中启动本地机器上的Derby Network Server。

接着要配置应用程序和其他工具,以连接到该网络服务器。

下图展示了配置情况。

图 1. 开发环境配置设置环境的第一步是创建项目。

选择“File->New->Project->Java Project”并输入bookstore 作为项目名称。

这样就创建了一个Java 项目,然后切换到Java perspective(透视图)中。

右击该项目并选择“Apache Derby->Add Apache Derby nature”。

这样使您的项目可以使用Apache Derby 特性,然后设置该Java 项目的构建路径,以便应用程序可以访问Derby 数据库和JDBC 驱动程序。

DB2 plug-ins for Eclipse 通常可以从Data perspective 访问,并且无需与某个特定的项目相关联。

为了简化开发过程,避免Java perspective 和Data perspective 之间的切换,需要将DB2 plug-ins 视图,即Database Explorer 视图和DB Output 视图,添加到Java perspective。

这可以通过Show View->Other菜单来完成。

下图展示了开发环境的外观。

图 2. 开发环境布局创建数据库在开始编写实际的应用程序代码之前,需要创建应用程序将要用到的数据库,或者连接到一个已有的数据库。

首先通过右击项目并选择“Apache Derby->Start Derby Network Server”来启动Derby Network Server。

每次重新启动Eclipse 时都需要执行这一步。

这时项目图标上有一个绿色的箭头,表明服务器正在运行。

创建数据库创建一个Derby 数据库非常类似于连接到一个已有的数据库:通过将create=true属性包括在URL 中,可以指示数据库引擎在您第一次连接到数据库时创建该数据库。

这可以通过使用DB2 plug-ins for Eclipse 的Connection Wizard 来完成。

下面的表展示了在这个向导中应该使用的参数。

表 1. 连接参数ConnectionnamebookstoredbDatabaseManagerApache Derby v10.0JDBCDriverIBM DB2 Universal 需要使用这个参数来连接到网络服务器Databasebookstoredb 要创建的数据库的名称Host localhost 网络服务器运行在本地机器上PortNumber1527 默认端口号Class Locatio n 比如: C:\eclipse\plugins\com.ibm.cloudscape.ui_1.0.0\db2jcc_license_c.jar;C:\eclipse\plug ins\com.ibm.cloudscape.ui_1.0.0 \db2jcc.jarCreate databa yes 需要使用这个参数在第一次连接时创建数据库回页首se ifrequiredUser ID bookstore 数据库上的认证没有被启用,因此可以使用任何用户,但是用户名将定义默认模式aaa 数据库上的认证没有被启用,因此可以使用任何密码Password图 3. 使用Connection Wizard 创建数据库完成该向导后,便创建了一个数据库,并且向Database Explorer View 中添加了一个连接。

通过展开连接的节点,就可以浏览这个数据库,但是显然这个时候它是空的。

数据库被创建在Derby 网络服务器的当前目录中,也就是之前创建Eclipse 项目时所在的目录。

可以通过右击项目名并选择Refresh 来刷新该项目,这样将显示一个新的bookstoredb/目录,该目录包含用于数据库的文件。

不要试图修改这些文件,否则数据库会受到损坏。

创建数据库对象接下来的步骤是创建应用程序将要用到的数据库对象。

在这里,只需使用SQL Scrapbook 创建一个表即可。

SQL Scrapbook 可以通过右击连接名并选择“Open SQL Scrapbook”来调用。

这时将打开一个新的编辑器,在这个编辑器中可以输入要发出的SQL 语句:请注意,SQL Scrapbook 只能用于执行单条的SQL 语句。

而且,不要以分号来结束SQL 语句。

然后,可以按下主Eclipse 按钮栏中的“Execute SQL statem ent”按钮。

DB Output 视图应该显示结果是成功的。

还可以刷新连接,以及验证数据库现在是否包含新创建的表。

图 4. 使用 SQL Scrapbook 创建表创建测试数据现在通过执行INSERT 语句向books 表填充一些测试数据。

相关文档
最新文档