Eclipse下使用HSQLDB

一、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,文件內容如下:

  1. package testJDBC;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. public class Test {
  9.     /**
  10.      * @param args
  11.      */
  12.     public static void main(String[] args){
  13.         try {
  14.                 //加載HSQLDB數據庫JDBC驅動
  15.                 Class.forName("org.hsqldb.jdbcDriver");
  16.                 //在內存中建立臨時數據庫score,用戶名爲sa,密碼爲空
  17.                 @SuppressWarnings("unused")
  18.                 Connection connect = DriverManager.getConnection("jdbc:hsqldb:mem:score""sa""");
  19.                 System.out.println("Link is OK!");
  20.                 
  21.                 Statement state = connect.createStatement(); 
  22.                 state.executeUpdate("create table Tb1 (ID INTEGER, Name VARCHAR(20))");
  23.                 System.out.println("Create is  OK!");
  24.                 
  25.                 state.executeUpdate("Insert into Tb1 (ID, Name) Values(1, '潘永剛')");
  26.                 state.executeUpdate("Insert into Tb1 (ID, Name) Values(2, '劉德華')");
  27.                 System.out.println("Insert is OK!");
  28.                 
  29.                 PreparedStatement pstmt2   =   connect.prepareStatement("select * from Tb1");   
  30.                 ResultSet rs = pstmt2.executeQuery();   
  31.                 while(rs.next()){  
  32.                     String x;
  33.                     x = rs.getString(1) + "   " + rs.getString(2);                  
  34.                     System.out.println(x);                      
  35.                 }   
  36.                 System.out.println("Select is OK!");
  37.                 pstmt2.close();
  38.                 rs.close();
  39.                 
  40.                 state.close();
  41.                 connect.close();
  42.                 
  43.             } catch (SQLException e){
  44.                 e.printStackTrace();
  45.             } catch (ClassNotFoundException e){
  46.                   e.printStackTrace();
  47.             }              
  48.     }
  49. }

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方法,之後處理個按鈕的事件,雙擊按鈕添加代碼即可。文件內容如下:

  1. package com.hsql;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import org.eclipse.jface.dialogs.MessageDialog;
  8. import org.eclipse.swt.SWT;
  9. import org.eclipse.swt.events.SelectionAdapter;
  10. import org.eclipse.swt.events.SelectionEvent;
  11. import org.eclipse.swt.layout.FormAttachment;
  12. import org.eclipse.swt.layout.FormData;
  13. import org.eclipse.swt.layout.FormLayout;
  14. import org.eclipse.swt.widgets.Button;
  15. import org.eclipse.swt.widgets.Display;
  16. import org.eclipse.swt.widgets.Group;
  17. import org.eclipse.swt.widgets.Label;
  18. import org.eclipse.swt.widgets.Shell;
  19. import org.eclipse.swt.widgets.Text;
  20. public class TestHSql {
  21.     private static Text text_2;
  22.     private static Text text_1;
  23.     private static Text text;
  24.     final static Display display = new Display();
  25.     final static Shell shell = new Shell(display);  
  26.     //主函數程序入口
  27.     public static void main(String[] args) {
  28.         shell.setText("HSqlDb應用");  
  29.         shell.setSize(450, 250);
  30.         shell.setLayout(new FormLayout());
  31.         final Group group = new Group(shell, SWT.NONE);
  32.         group.setBackgroundMode(SWT.INHERIT_DEFAULT);
  33.         final FormData fd_group = new FormData();
  34.         fd_group.right = new FormAttachment(100, -5);
  35.         fd_group.bottom = new FormAttachment(0, 101);
  36.         fd_group.top = new FormAttachment(0, 15);
  37.         group.setLayoutData(fd_group);
  38.         group.setText("數據信息");
  39.         Label label;
  40.         label = new Label(group, SWT.NONE);
  41.         label.setBounds(14, 44, 36, 12);
  42.         label.setText("編號:");
  43.         text = new Text(group, SWT.BORDER);
  44.         text.setBounds(55, 41, 70, 18);
  45.         Label label_1;
  46.         label_1 = new Label(group, SWT.NONE);
  47.         label_1.setBounds(130, 44, 36, 12);
  48.         label_1.setText("姓名:");
  49.         text_1 = new Text(group, SWT.BORDER);
  50.         text_1.setBounds(171, 41, 114, 18);
  51.         Group group_1;
  52.         group_1 = new Group(shell, SWT.NONE);
  53.         fd_group.left = new FormAttachment(group_1, 0, SWT.LEFT);
  54.         //添加按鈕事件
  55.         final Button button = new Button(group, SWT.NONE);
  56.         button.setBounds(291, 39,48, 22);
  57.         button.addSelectionListener(new SelectionAdapter() {
  58.             public void widgetSelected(final SelectionEvent e) {  
  59.                 try {           
  60.                     Class.forName("org.hsqldb.jdbcDriver");         
  61.                     @SuppressWarnings("unused")
  62.                     Connection connect = DriverManager.getConnection(
  63.                             "jdbc:hsqldb:mem:score""sa""");         
  64.                     Statement state = connect.createStatement();
  65.                     String sqlText;
  66.                     //將界面中輸入的數據寫入數據庫表中
  67.                     sqlText = "Insert into Tb1 (ID, Name) Values(" +
  68.                       text.getText() + ",'" + text_1.getText() + "')";
  69.                     state.executeUpdate(sqlText);
  70.                     
  71.                     state.close();
  72.                     connect.close();
  73.                     text.setText("");
  74.                     text_1.setText("");
  75.                 } catch (SQLException a) {
  76.                     a.printStackTrace();
  77.                 } catch (ClassNotFoundException a) {
  78.                     a.printStackTrace();
  79.                 }               
  80.             }
  81.         });
  82.         button.setText("添加");
  83.         final FormData fd_group_1 = new FormData();
  84.         fd_group_1.right = new FormAttachment(100, -5);
  85.         fd_group_1.bottom = new FormAttachment(0, 162);
  86.         fd_group_1.top = new FormAttachment(0, 107);
  87.         fd_group_1.left = new FormAttachment(0, 10);
  88.         group_1.setLayoutData(fd_group_1);
  89.         //查詢按鈕事件
  90.         final Button button_1 = new Button(group_1, SWT.NONE);
  91.         button_1.addSelectionListener(new SelectionAdapter() {
  92.             public void widgetSelected(final SelectionEvent e) { 
  93.                 boolean flag = false;
  94.                 try {   
  95.                     //加載HSQLDB數據庫JDBC驅動
  96.                     Class.forName("org.hsqldb.jdbcDriver");         
  97.                     @SuppressWarnings("unused")                 
  98.                     //鏈接內存中的表score,用戶名爲sa,密碼爲空
  99.                     Connection connect = DriverManager.getConnection(
  100.                             "jdbc:hsqldb:mem:score""sa""");  
  101.                     //查詢表
  102.                     Statement pstmt2 = connect.createStatement();
  103.                     ResultSet rs = pstmt2.executeQuery("select * from Tb1 where Id = " + text_2.getText());
  104.                     while(rs.next()){  
  105.                         flag = true;//如果有>0條記錄則置爲真
  106.                         text.setText(rs.getString(1));
  107.                         text_1.setText(rs.getString(2));
  108.                     }   
  109.                     //如果沒有記錄提示
  110.                     if(!flag)
  111.                         MessageDialog.openError(null"信息提示""沒找到該編號的記錄!");
  112.                     pstmt2.close();
  113.                     rs.close();     
  114.                     
  115.                     connect.close();            
  116.                 } catch (SQLException x) {
  117.                     MessageDialog.openError(null"信息提示""異常錯誤!");
  118.                     x.printStackTrace();
  119.                 } catch (ClassNotFoundException x) {
  120.                     x.printStackTrace();
  121.                 }           
  122.                                         
  123.             }
  124.         });
  125.         button_1.setText("查詢");
  126.         button_1.setBounds(163, 23, 48, 22);
  127.         //關閉按鈕事件
  128.         final Button button_2 = new Button(group_1, SWT.NONE);
  129.         button_2.addSelectionListener(new SelectionAdapter() {
  130.             public void widgetSelected(final SelectionEvent e) { 
  131.                 shell.dispose();
  132.             }
  133.         });
  134.         button_2.setText("關閉");
  135.         button_2.setBounds(290, 23, 48, 22);
  136.         final Label label_2 = new Label(group_1, SWT.NONE);
  137.         label_2.setText("編號:");
  138.         label_2.setBounds(17, 28, 30, 17);
  139.         text_2 = new Text(group_1, SWT.BORDER);
  140.         text_2.setBounds(57, 25, 100, 18);
  141.         //
  142.         try {   
  143.             //加載HSQLDB數據庫JDBC驅動
  144.             Class.forName("org.hsqldb.jdbcDriver");         
  145.             @SuppressWarnings("unused")
  146.             //在內存中建立臨時數據庫score,用戶名爲sa,密碼爲空
  147.             Connection connect = DriverManager.getConnection(
  148.                     "jdbc:hsqldb:mem:score""sa""");         
  149.             //創建表
  150.             Statement state = connect.createStatement();
  151.             state.executeUpdate("create table Tb1 (ID INTEGER, Name VARCHAR(20))");             
  152.             state.close();
  153.             connect.close();            
  154.         } catch (SQLException e) {
  155.             e.printStackTrace();
  156.         } catch (ClassNotFoundException e) {
  157.             e.printStackTrace();
  158.         }           
  159.                 
  160.         shell.open();
  161.         while(!shell.isDisposed()){
  162.             if(!display.readAndDispatch()){
  163.                 display.sleep();
  164.             }
  165.         }
  166.         display.dispose();
  167.     }
  168. }

 

 

HSqlDbTest右鍵選擇run as => java application,運行結果如下圖:

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章