I have downloaded a hsqldb.jar and I set to project buildpath,then next I wrote the program
Class.forName(\"org.hsqldb.jdbc.JDBCDriver\");
Connection conn
You can dump your HSQL in-memory database.
AbstractTestDao:
public abstract class AbstractTestDao {
/**
* Generic method for persisting entities into database.
*
* @param entity Java object from a (hibernate) class annotated with @Entity
*/
<T> void persistEntity(T entity) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.persist(entity);
session.beginTransaction().commit();
session.close();
}
/**
* Generic method for retrieving entities from database.
*
* @param cls Object class
* @param id Entity unique identifier (Primary Key)
* @return Casted Entity object from db
*/
<T> T getEntityById(Class<T> cls, Long id) {
Session session = HibernateUtil.getSessionFactory().openSession();
T entity = cls.cast(session.load(cls, id));
session.close();
return entity;
}
/**
* Don't forget to close your connection so that the in-mem db can release it resources.
*
* @return Connection object to the in-memory db HyperSQL.
* @throws SQLException
*/
public static Connection getNewConn() throws SQLException {
return DriverManager.getConnection("jdbc:hsqldb:mem:testdb;shutdown=false", "SA", "");
}
/**
* Makes a script from the current in-memory database and place a txt file in a directory.
* The script describes the current in-mem db including constrains.
*
* @param testName A String representing the junit test name used for describing the file.
* @throws SQLException
*/
void createDatabaseScript(final String testName) throws SQLException {
final String filePath = new File("").getAbsolutePath();
final String targetDir = "/target/Temp/Databasedump/";
final String directoryPath = filePath.concat(targetDir);
final File directory = new File(directoryPath);
if (!directory.exists()) {
directory.mkdir();
}
final String fileName = uniqueFileNameGenerator(directoryPath, testName);
final Connection conn = getNewConn();
Statement st = null;
ResultSet rs = null;
try {
st = conn.createStatement();
//Example: st.executeQuery("SCRIPT '../Temp/dump4.txt'")
final String sqlStatement = String.format("SCRIPT '%s%s'", directoryPath, fileName);
rs = st.executeQuery(sqlStatement);
} finally {
if (st != null) {
st.close();
}
if (rs != null) {
rs.close();
}
if (conn != null) {
conn.close();
}
}
}
/**
* Search for a unique filename in a directory with 1 to 2147483647 (Integer.MAX_VALUE) possibilities per date.
* May throw a StackOverflowError if all 2147483646 file names are given away.
*
* @param directoryPath - String representing the directory you want to use.
* @param name - String representing the filename you want to use.
* @return String representing a unique filename
*/
private String uniqueFileNameGenerator(final String directoryPath, final String name) {
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
final String todayDate = dateFormat.format(Calendar.getInstance().getTime());
final Integer randomNumber = (int) (Math.random() * Integer.MAX_VALUE + 1);
final String fileName = name + "_" + todayDate + "_" + randomNumber + ".txt";
File file = new File(directoryPath + fileName);
if (file.exists()) {
return uniqueFileNameGenerator(directoryPath, name);
}
return fileName;
}
}
HSQL DB version:
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.10</version>
</dependency>
You've created database in memory, so there is no persistent file with your tables / data and then you close your application, all your data lost.
If you want to make it persistent fix your connection creation: replace mem to file. Something like this:
Connection conn = DriverManager.getConnection("jdbc:hsqldb:file:mydb", "sa", "");
You can read about different HSQLDB modes here. Also you can specify path, where your database will store file:
jdbc:hsqldb:file:/file/path/to/test"
After first run HSQLDB will create several files. Purpose of each file you can read here.
test.properties Contains the entry 'modified'. If the entry 'modified' is set to 'yes' then the database is either running or was not closed correctly (because the close algorithm sets 'modified' to 'no' at the end).
test.script This file contains the SQL statements that makes up the database up to the last checkpoint - it is in synch with test.backup.
test.data This file contains the (binary) data records for CACHED tables only.
test.backup This is compressed file that contains the complete backup of the old test.data file at the time of last checkpoint.
test.log This file contains the extra SQL statements that have modified the database since the last checkpoint (something like the 'Redo-log' or 'Transaction-log', but just text).
BTW, you can get all your tables using this query:
SELECT * FROM INFORMATION_SCHEMA.SYSTEM_TABLES where TABLE_TYPE='TABLE'
You can execute this query as normal query:
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM INFORMATION_SCHEMA.SYSTEM_TABLES where TABLE_TYPE='TABLE'");
while(rs.next()) {
...
}
Also you can view your database using built-in manager. You can start in using command:
java -cp /path/to/hsqldb.jar org.hsqldb.util.DatabaseManager
And then specify path your database:
jdbc:hsqldb:file:mydb
Or you can use popular tool like SQuirreL.