一、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 {
- /**
- * @param args
- */
- public static void main(String[] args){
- try {
- //加載HSQLDB數據庫JDBC驅動
- Class.forName("org.hsqldb.jdbcDriver");
- //在內存中建立臨時數據庫score,用戶名爲sa,密碼爲空
- @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 {
- //加載HSQLDB數據庫JDBC驅動
- Class.forName("org.hsqldb.jdbcDriver");
- @SuppressWarnings("unused")
- //鏈接內存中的表score,用戶名爲sa,密碼爲空
- 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;//如果有>0條記錄則置爲真
- 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 {
- //加載HSQLDB數據庫JDBC驅動
- Class.forName("org.hsqldb.jdbcDriver");
- @SuppressWarnings("unused")
- //在內存中建立臨時數據庫score,用戶名爲sa,密碼爲空
- 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,運行結果如下圖: