Package us.jyg.freshet

Source Code of us.jyg.freshet.HsqldbTest

package us.jyg.freshet;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import junit.framework.TestCase;

/**
* This test class interacts directly with Hypersonic (HSQLDB), creating
* a databse with one table, putting a few rows in and checking that
* they all things went well
*
* @author jgabler
*
*/
public class HsqldbTest extends TestCase {
  protected final Log log = LogFactory.getLog(getClass());
 
    public void testHsqldb() {
     
     
      try {
        Class.forName("org.hsqldb.jdbcDriver").newInstance();
        Connection c = DriverManager.getConnection("jdbc:hsqldb:mem:library", "sa", "");
        Statement statement = c.createStatement();
       
        // INSERT TEST
        statement.execute(
        "CREATE TABLE category ("+
          "bookId   INT NOT NULL PRIMARY KEY," +
          "name VARCHAR(20)"+
        ")"
        );
        statement = c.createStatement();
        statement.addBatch("INSERT INTO category(bookId,name) VALUES (1,'Theology');");
        statement.addBatch("INSERT INTO category(bookId,name) VALUES (2,'Art History');");
        statement.addBatch("INSERT INTO category(bookId,name) VALUES (3,'Medieval Studies');");
        statement.executeBatch();
       
        // SELECT TEST
        statement = c.createStatement();
        statement.execute("SELECT COUNT(bookId) FROM category;");
        ResultSet resultSet = statement.getResultSet();
        resultSet.next();
        assertTrue(resultSet.getInt(1)==3);

        statement = c.createStatement();
        statement.execute("SELECT * FROM category");
        resultSet = statement.getResultSet();
       
        resultSet.next();
        assertTrue(resultSet.getInt(1)==1);
      assertTrue(resultSet.getString(2).equals("Theology"));
     
      resultSet.next();
      assertTrue(resultSet.getInt(1)==2);
      assertTrue(resultSet.getString(2).equals("Art History"));
     
      resultSet.next();
      assertTrue(resultSet.getInt(1)==3);
      assertTrue(resultSet.getString(2).equals("Medieval Studies"));

      } catch (SQLException sqlE) {
        sqlE.printStackTrace();
      } catch (Exception e) {
        e.printStackTrace();
      }
       
    }
}
TOP

Related Classes of us.jyg.freshet.HsqldbTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.