一、Eclipse配置安装HSQLDB(以本人电脑实际版本及路径为例)
将下载的hsqldb_1_8_0_10.zip解压到D:\eclipse3.3.2,其中hsqldb文件夹为解压的全部文件。OK,安装完毕!
二、非图形实例
1、在Eclipse新建Java Project,名称为JDBCTest(不太恰当,当时为练习JDBC使用,后未针对HSQLDB更改),建好之后在项目JDBCTest右键选择Properties,在出现的配置对话框选择Java Build Path =>Libraries=>Add External JARs,将路径D:\eclipse3.3.2\hsqldb\lib下的hsqldb.jar加入,点击OK。
2、新建一Package,起名如com.myhsqldb,之后新建一Class文件Test.java,文件内容如下:
- package testJDBC;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class Test {
-
- public static void main(String[] args){
- try {
-
- Class.forName("org.hsqldb.jdbcDriver");
-
- @SuppressWarnings("unused")
- Connection connect = DriverManager.getConnection("jdbc:hsqldb:mem:score", "sa", "");
- System.out.println("Link is OK!");
-
- Statement state = connect.createStatement();
- state.executeUpdate("create table Tb1 (ID INTEGER, Name VARCHAR(20))");
- System.out.println("Create is OK!");
-
- state.executeUpdate("Insert into Tb1 (ID, Name) Values(1, '潘永刚')");
- state.executeUpdate("Insert into Tb1 (ID, Name) Values(2, '刘德华')");
- System.out.println("Insert is OK!");
-
- PreparedStatement pstmt2 = connect.prepareStatement("select * from Tb1");
- ResultSet rs = pstmt2.executeQuery();
- while(rs.next()){
- String x;
- x = rs.getString(1) + " " + rs.getString(2);
- System.out.println(x);
- }
- System.out.println("Select is OK!");
- pstmt2.close();
- rs.close();
-
- state.close();
- connect.close();
-
- } catch (SQLException e){
- e.printStackTrace();
- } catch (ClassNotFoundException e){
- e.printStackTrace();
- }
- }
- }
JDBCTest右键选择run as => java application,运行结果如下图:
三、图形界面实例
1、在Eclipse新建SWT/JFace Java Project名称为HSqlDbTest,如果直接用Java Project也可以,但需要另外增加配置SWT/JFace库这一步,建好之后在项目JDBCTest右键选择Properties,在出现的配置对话框选择Java Build Path =>Libraries=>Add External JARs,将路径D:\eclipse3.3.2\hsqldb\lib下的hsqldb.jar加入,点击OK。
2、新建一Package,起名如com.hsql,之后新建一Class文件TestHSql.java,切换到图形界面(Design),添加按钮和文本框等,当然你也可以直接用代码写,本人目前对Layout布局使用不精,因此直接在图形界面设计,然后将需要最先处理的代码写在main方法,之后处理个按钮的事件,双击按钮添加代码即可。文件内容如下:
- package com.hsql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import org.eclipse.jface.dialogs.MessageDialog;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.events.SelectionAdapter;
- import org.eclipse.swt.events.SelectionEvent;
- import org.eclipse.swt.layout.FormAttachment;
- import org.eclipse.swt.layout.FormData;
- import org.eclipse.swt.layout.FormLayout;
- import org.eclipse.swt.widgets.Button;
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.Group;
- import org.eclipse.swt.widgets.Label;
- import org.eclipse.swt.widgets.Shell;
- import org.eclipse.swt.widgets.Text;
- public class TestHSql {
- private static Text text_2;
- private static Text text_1;
- private static Text text;
- final static Display display = new Display();
- final static Shell shell = new Shell(display);
-
- public static void main(String[] args) {
- shell.setText("HSqlDb应用");
- shell.setSize(450, 250);
- shell.setLayout(new FormLayout());
- final Group group = new Group(shell, SWT.NONE);
- group.setBackgroundMode(SWT.INHERIT_DEFAULT);
- final FormData fd_group = new FormData();
- fd_group.right = new FormAttachment(100, -5);
- fd_group.bottom = new FormAttachment(0, 101);
- fd_group.top = new FormAttachment(0, 15);
- group.setLayoutData(fd_group);
- group.setText("数据信息");
- Label label;
- label = new Label(group, SWT.NONE);
- label.setBounds(14, 44, 36, 12);
- label.setText("编号:");
- text = new Text(group, SWT.BORDER);
- text.setBounds(55, 41, 70, 18);
- Label label_1;
- label_1 = new Label(group, SWT.NONE);
- label_1.setBounds(130, 44, 36, 12);
- label_1.setText("姓名:");
- text_1 = new Text(group, SWT.BORDER);
- text_1.setBounds(171, 41, 114, 18);
- Group group_1;
- group_1 = new Group(shell, SWT.NONE);
- fd_group.left = new FormAttachment(group_1, 0, SWT.LEFT);
-
- final Button button = new Button(group, SWT.NONE);
- button.setBounds(291, 39,48, 22);
- button.addSelectionListener(new SelectionAdapter() {
- public void widgetSelected(final SelectionEvent e) {
- try {
- Class.forName("org.hsqldb.jdbcDriver");
- @SuppressWarnings("unused")
- Connection connect = DriverManager.getConnection(
- "jdbc:hsqldb:mem:score", "sa", "");
- Statement state = connect.createStatement();
- String sqlText;
-
- sqlText = "Insert into Tb1 (ID, Name) Values(" +
- text.getText() + ",'" + text_1.getText() + "')";
- state.executeUpdate(sqlText);
-
- state.close();
- connect.close();
- text.setText("");
- text_1.setText("");
- } catch (SQLException a) {
- a.printStackTrace();
- } catch (ClassNotFoundException a) {
- a.printStackTrace();
- }
- }
- });
- button.setText("添加");
- final FormData fd_group_1 = new FormData();
- fd_group_1.right = new FormAttachment(100, -5);
- fd_group_1.bottom = new FormAttachment(0, 162);
- fd_group_1.top = new FormAttachment(0, 107);
- fd_group_1.left = new FormAttachment(0, 10);
- group_1.setLayoutData(fd_group_1);
-
- final Button button_1 = new Button(group_1, SWT.NONE);
- button_1.addSelectionListener(new SelectionAdapter() {
- public void widgetSelected(final SelectionEvent e) {
- boolean flag = false;
- try {
-
- Class.forName("org.hsqldb.jdbcDriver");
- @SuppressWarnings("unused")
-
- Connection connect = DriverManager.getConnection(
- "jdbc:hsqldb:mem:score", "sa", "");
-
- Statement pstmt2 = connect.createStatement();
- ResultSet rs = pstmt2.executeQuery("select * from Tb1 where Id = " + text_2.getText());
- while(rs.next()){
- flag = true;
- text.setText(rs.getString(1));
- text_1.setText(rs.getString(2));
- }
-
- if(!flag)
- MessageDialog.openError(null, "信息提示", "没找到该编号的记录!");
- pstmt2.close();
- rs.close();
-
- connect.close();
- } catch (SQLException x) {
- MessageDialog.openError(null, "信息提示", "异常错误!");
- x.printStackTrace();
- } catch (ClassNotFoundException x) {
- x.printStackTrace();
- }
-
- }
- });
- button_1.setText("查询");
- button_1.setBounds(163, 23, 48, 22);
-
- final Button button_2 = new Button(group_1, SWT.NONE);
- button_2.addSelectionListener(new SelectionAdapter() {
- public void widgetSelected(final SelectionEvent e) {
- shell.dispose();
- }
- });
- button_2.setText("关闭");
- button_2.setBounds(290, 23, 48, 22);
- final Label label_2 = new Label(group_1, SWT.NONE);
- label_2.setText("编号:");
- label_2.setBounds(17, 28, 30, 17);
- text_2 = new Text(group_1, SWT.BORDER);
- text_2.setBounds(57, 25, 100, 18);
-
- try {
-
- Class.forName("org.hsqldb.jdbcDriver");
- @SuppressWarnings("unused")
-
- Connection connect = DriverManager.getConnection(
- "jdbc:hsqldb:mem:score", "sa", "");
-
- Statement state = connect.createStatement();
- state.executeUpdate("create table Tb1 (ID INTEGER, Name VARCHAR(20))");
- state.close();
- connect.close();
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
-
- shell.open();
- while(!shell.isDisposed()){
- if(!display.readAndDispatch()){
- display.sleep();
- }
- }
- display.dispose();
- }
- }
HSqlDbTest右键选择run as => java application,运行结果如下图:

发表于 @ 2008年11月13日 16:05:00|评论(loading...)|收藏