hsqldb是個不用安裝也不用啟動的database資料庫,
資料都存在檔案裡,啟動時會把資料載入記憶體(也可用Server start的方式...等等等)
有名的免費office軟體openoffice,裡面的base資料庫也是使用此種方式
只是它把檔案壓縮起來,如果要使用時,需要先解壓縮才能使用
使用的方式如下
1.先下載jdbc driver
http://sourceforge.net/project/showfiles.php?group_id=23316
解壓縮後把資料夾裡的lib/hsqldb.jar放到classpath裡
2.撰寫程式

 

package db;

//import org.hsqldb.jdbcDriver;
import java.sql.*;


public class Ooo
{
  private Connection con = null//Database objects

  private Statement stat = null;
  private ResultSet rs = null;
  private PreparedStatement pst = null;
  
  private String dropdbSQL = "DROP TABLE User ";
  
  private String createdbSQL = "CREATE TABLE User (" +
    "    id     INTEGER " +
    "  , name    VARCHAR " +
    "  , passwd  VARCHAR)";
  
  private String insertdbSQL = "insert into User(id,name,passwd) " +
      "select nvl(max(id),0)+1,?,? FROM User";
  
  private String selectSQL = "select * from User ";
  
  public Ooo()
  {
    try {
      Class.forName("org.hsqldb.jdbcDriver");
      con = DriverManager.getConnection("jdbc:hsqldb:file:c:/database" 
          "SA""");
    }
    catch(ClassNotFoundException e)
    {
      System.out.println("DriverClassNotFound :"+e.toString());
    }
    catch(SQLException x) {
      System.out.println("Exception :"+x.toString());
    }
    
  }
  
  public void createTable() 
  {
    try
    {
      stat = con.createStatement();
      stat.executeUpdate(createdbSQL);
    }
    catch(SQLException e)
    {
      System.out.println("CreateDB Exception :" + e.toString());
    }
    finally
    {
      Close();
    }
  }
  
  public void insertTableString name,String passwd)
  {
    try
    {
      pst = con.prepareStatement(insertdbSQL);
      
      pst.setString(1, name);
      pst.setString(2, passwd);
      pst.executeUpdate();
    }
    catch(SQLException e)
    {
      System.out.println("InsertDB Exception :" + e.toString());
    }
    finally
    {
      Close();
    }
  }
  
  public void dropTable()
  {
    try
    {
      stat = con.createStatement();
      stat.executeUpdate(dropdbSQL);
    }
    catch(SQLException e)
    {
      System.out.println("DropDB Exception :" + e.toString());
    }
    finally
    {
      Close();
    }
  }
  
  public void SelectTable()
  {
    try
    {

文章標籤
全站熱搜
創作者介紹
創作者 白努力電腦日記 的頭像
白努力電腦日記

包包的雲端語意小說

白努力電腦日記 發表在 痞客邦 留言(0) 人氣(385)