淘宝商城(天猫)高级技术专家.3年研发+3年性能测试调优/系统测试+4年团队管理与测试架构、研发系统实践. 新舞台新气象, 深化测试基础架构及研发架构,希望能在某个技术领域成为真正的技术大牛。欢迎荐才http://bbs.51testing.com/viewthread.php?tid=120496&extra=&page=1 .邮件: jianzhao.liangjz@alibaba-inc.com,MSN:liangjianzhao@163.com.微博:http://t.sina.com.cn/1674816524

Hsqldb在单元测试中简单应用

上一篇 / 下一篇  2009-04-06 12:25:26 / 个人分类:java性能监控与调优

参考:

http://www.blogjava.net/q5782129100/archive/2008/12/27/248600.html

http://www.javaeye.com/topic/79802

下载:http://hsqldb.org/

Hsqldb4种模式可采用server模式退出时不用shutdown持久存储或者memory only模式作为单元测试数据库,可以保证测试原子性。

ALIBABA B2B研发编写的单元测试代码中,已经有多年生命周期了J

 

1)    server模式启动

D:\hsqldb_1_8_0_10\bin> java -cp ../lib/hsqldb.jar org.hsqldb.Server -database.

0 file:mydb -dbname.0 xdb

 

可以看到bin目录下有3个新文件:mydb.properties,mydb.log,mydb.script

  然后另外开一个控制台执行:

D:\hsqldb_1_8_0_10\bin>  java -cp ../lib/hsqldb.jar org.hsqldb.util.DatabaseMan

agerSwing

Failed to load preferences. Proceeding with defaults:

选择: server模式,以及xdb,密码为空.

 

 

执行创建表以及记录:

 

createtableUSER(

   USER_IDvarchar(25)notnull,

   PASSWORDvarchar(25) notnull,

 

   constraintPK_USERprimarykey(USER_ID)

);

INSERT INTO USER VALUES('j2ee','j2ee');

 

 

      编写测试程序:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

 

/**

 * TODO Comment of HSQLServerTest

 *

 * @author jianzhao.liangjz

 */

public class HSQLServerTest {

    public static void main(String[] args){

       System.out.println("begin...");

       Connection connection = null;

       Statement stmt = null;

       ResultSet rs = null;

       try {

           Class.forName("org.hsqldb.jdbcDriver");

           connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/xdb", "sa", "");

           stmt = connection.createStatement();

           String sql = "select * from user";

           rs = stmt.executeQuery(sql);

           while (rs.next()) {

               System.out.println("user_id=" + rs.getString("USER_ID"));

               System.out.println("password=" + rs.getString("PASSWORD"));

           }

       } catch (Exception e) {

           e.printStackTrace();

       } finally {

           try {

               if (rs != null)

                   rs.close();

               if (stmt != null) {

                   stmt.close();

               }

               if (connection != null) {

                   connection.close();

               }

           } catch (Exception e) {

               e.printStackTrace();

           }

       }

   }

}

 

D:\hsqldb_1_8_0_10\bin>javac -cp %CLASSPATH%;../lib/hsqldb.jar HSQLServerTest.

java

 

D:\hsqldb_1_8_0_10\bin>

D:\hsqldb_1_8_0_10\bin>java -cp %CLASSPATH%;../lib/hsqldb.jar HSQLServerTest

begin...

user_id=j2ee

password=j2ee

 

2)    memory only模式启动以及连接

能创建表以及insert记录.但不能开一个DB连接直接读取数据.

所以MEM的测试在一个进程内完成:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

 

public class HSQLMemTest{

   public static void main(String[] args){

       try {

            Class.forName("org.hsqldb.jdbcDriver" );           

           

            Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:memdb", "sa", "");

            if(c != null){

                System.out.println("Connected db success!");

                String sql = "create table USER (USER_ID varchar(25) not null, PASSWORD varchar(25) not null,constraint PK_USER primary key (USER_ID) );";

                Statement st = c.createStatement();

                st.execute(sql);

                sql = "INSERT INTO USER VALUES('j2ee','j2ee');";

                st.executeUpdate(sql);

                  

                String sql_query = "select * from USER";

               ResultSet rs = st.executeQuery(sql_query);

                         while (rs.next()) {

                            System.out.println("user_id=" + rs.getString("USER_ID"));

                            System.out.println("password=" + rs.getString("PASSWORD"));

                        }

                         if(st != null){

                    st.close();

                }

                c.close();

            }

 

        } catch (Exception e) {

            System.out.println("ERROR: failed to load HSQLDB JDBC driver.");

            e.printStackTrace();

           return;

        }

 

    }

}

D:\hsqldb_1_8_0_10\bin>javac -cp %CLASSPATH%;../lib/hsqldb.jar HSQLMemTest.jav

a

 

D:\hsqldb_1_8_0_10\bin>java -cp %CLASSPATH%;../lib/hsqldb.jar HSQLMemTest

 


TAG: java Memory HSQLDB connection memory

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar