カスタム検索
このエントリーをはてなブックマークに追加
tomo.gif (1144 ツバツイツト)line.gif (927 ツバツイツト)line.gif (927 ツバツイツト)line.gif (927 ツバツイツト)To previous pageTo home pageMailing to me

HSQLDBの実験

Modified: 1 March 2007


HSQLDBにJDBCで接続してみる (17 March 2007)


 HSQLDBにJDBCで接続してみる 

ダウンロードする

HSQLDBの公式ページ

http://hsqldb.org/

2007年3月11日に、"hsqldb_1_8_0_7.zip" がダウンロードできました。

ダウンロード後、以下のように解凍します。

# unzip hsqldb_1_8_0_7.zip
              :
#

JARファイル(hsqldb.jarとservlet.jar)を、"WEB-INF"の"lib"にコピーします。これで、インストールは完了です。


データベースを起動します

データベース("aaa")を指定して起動します。

# java -cp lib/hsqldb.jar org.hsqldb.Server -database aaa
[Server@e7b241]: [Thread[main,5,main]]: checkRunning(false) entered
[Server@e7b241]: [Thread[main,5,main]]: checkRunning(false) exited
[Server@e7b241]: Startup sequence initiated from main() method
[Server@e7b241]: Loaded properties from [/usr/local/tomcat/webapps/site/WEB-INF/server.properties]
[Server@e7b241]: Initiating startup sequence...
[Server@e7b241]: Server socket opened successfully in 116 ms.
[Server@e7b241]: Database [index=0, id=0, db=file:aaa, alias=] opened sucessfully in 1959 ms.
[Server@e7b241]: Startup sequence completed in 2189 ms.
[Server@e7b241]: 2007-03-11 23:14:54.762 HSQLDB server 1.7.3 is online
[Server@e7b241]: To close normally, connect and execute SHUTDOWN SQL
[Server@e7b241]: From command line, use [Ctrl]+[C] to abort abruptly

接続ポートは、"9001" です。

# netstat -ln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 127.0.0.1:8005              0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:9001                0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:8009                0.0.0.0:*                   LISTEN

               :       


コマンドツールの起動

設定ファイルを、"sqltool.rc" の名前で作成します。

urlid hsqldb
url jdbc:hsqldb:database/aaa
username sa
password 

設定ファイル("sqltool.rc")を指定して、起動します。

# java -jar lib/hsqldb.jar --rcfile sqltool.rc hsqldb
JDBC Connection established to a HSQL Database Engine v. 1.8.0 database as 'SA'.
SqlTool v. 1.55.                        (SqlFile processor v. 1.135)
Distribution is permitted under the terms of the HSQLDB license.
(c) 2004-2005 Blaine Simpson and the HSQLDB Development Group.

    \q    to Quit.
    \?    lists Special Commands.
    :?    lists Buffer/Editing commands.
    *?    lists PL commands (including alias commands).

SPECIAL Commands begin with '\' and execute when you hit ENTER.
BUFFER Commands begin with ':' and execute when you hit ENTER.
COMMENTS begin with '/*' and end with the very next '*/'.
PROCEDURAL LANGUAGE commands begin with '*' and end when you hit ENTER.
All other lines comprise SQL Statements.
  SQL Statements are terminated by either a blank line (which moves the
  statement into the buffer without executing) or a line ending with ';'
  (which executes the statement).
  SQL Statements may begin with '/PLVARNAME' and/or contain *{PLVARNAME}s.

sql> \q

#


接続のテスト

接続してみて、接続が成功したらコネクションをプリントします。

package appsample;

import java.sql.Connection;
import java.sql.DriverManager;


public class TestHsqldb  {

        public static Connection getConnection() {
                try {
                    Class.forName("org.hsqldb.jdbcDriver");
                    Connection con = DriverManager.getConnection(
                                "jdbc:hsqldb:hsql://localhost", "sa", "");

                    return con;
                } catch (Exception e) {
                        throw new IllegalStateException("fall to getConnection");
                }
        }       
        
        public static void main(String[] args) throws Exception {
        Connection con = getConnection();
        System.out.println("con=" + con);
        con.close();
    }

}

"WEB-INF"で、コンパイルします。

export CLASSPATH=/usr/local/tomcat4/common/lib/servlet.jar 
javac src/appsample/TestHsqldb.java -d classes/

実行します。

以下が成功時の表示です。

# java -cp classes:lib/hsqldb.jar appsample.TestHsqldb
con=org.hsqldb.jdbc.jdbcConnection@167d940
# 

DBの接続に失敗したら以下のメッセージがでます。

# java -cp classes:lib/hsqldb.jar appsample.TestHsqldb
Exception in thread "main" java.lang.IllegalStateException: fall to getConnection
        at appsample.egoMysqlDB.getConnection(egoMysqlDB.java:17)
        at appsample.egoMysqlDB.main(egoMysqlDB.java:23)
#

To previous pageTo home pageMailing to meJump to Top of pageline.gif (927 ツバツイツト)line.gif (927 ツバツイツト)tomo.gif (1144 ツバツイツト)
カスタム検索



このエントリーをはてなブックマークに追加