JDK version 1.6.0_15.
H2 version 1.2.125.
Here is the test:
<pre>
import org.h2.tools.DeleteDbFiles;
import java.sql.*;
import static java.io.File.separatorChar;
/** Tests that {...@code Date} type value is eqaul after writing and then
reading it from the
database. */
public class DateH2Test{
public static java.util.Date convertSqlToUtilDate( final
java.sql.Date in_SqlDate ){
return ( null != in_SqlDate ) ? new java.util.Date
( in_SqlDate.getTime() ) : null;
}
public static java.sql.Date convertUtilToSqlDate( final
java.util.Date in_UtilDate ){
return ( null != in_UtilDate ) ? new java.sql.Date
( in_UtilDate.getTime() ) : null;
}
static final String DB_NAME = "test";
static final String DB_FOLDER = System.getProperty( "user.dir" );
static final String DB_FILE_PATH = "jdbc:h2:" + DB_FOLDER +
separatorChar + DB_NAME;
static final String DB_PARAMETERS =
";CACHE_SIZE=8192;AUTOCOMMIT=OFF";
static final String TABLE_NAME = "METADATA";
static final String CREATE_TABLE_STATEMENT = "CREATE TABLE " +
TABLE_NAME
+ "(ID INT IDENTITY(0,1), RATING INT DEFAULT 0,
DATE_VIEWED DATE
DEFAULT NULL, "
+ "VIEWED_COUNT INT DEFAULT 0)";
static final String INSERT_ROW_STATEMENT = "INSERT INTO " +
TABLE_NAME
+ "(ID, RATING, DATE_VIEWED, VIEWED_COUNT) VALUES(?, ?, ?, ?)";
public static void main( String[] args ) throws Exception{
boolean assertsEnabled = false;
assert( assertsEnabled = true ); // intentional side effect
if( assertsEnabled ){
// do nothing
}
else{
throw new RuntimeException( "Assertions must be
enabled!" );
}
// Test conversion methods
final java.util.Date UTIL_DATE = new java.util.Date();
final java.sql.Date WRITE_SQL_DATE = convertUtilToSqlDate
( UTIL_DATE );
assert( UTIL_DATE.equals( WRITE_SQL_DATE ) );
assert( WRITE_SQL_DATE.equals( UTIL_DATE ) );
assert( UTIL_DATE.equals( convertSqlToUtilDate
( WRITE_SQL_DATE ) ) );
// Test thatDate read/write in the database
// .. create db and tables, open it
final Connection connection;
{
Class.forName( "org.h2.Driver" );
connection = DriverManager.getConnection( DB_FILE_PATH +
DB_PARAMETERS );
final Statement statement =
connection.createStatement();
statement.execute( CREATE_TABLE_STATEMENT );
statement.close();
connection.commit();
}
// .. insert one row
final int ID;
{
final PreparedStatement statement =
connection.prepareStatement(
INSERT_ROW_STATEMENT );
ID = 1;
statement.setInt( 1, ID );
statement.setInt( 2, 2 );
statement.setDate( 3, WRITE_SQL_DATE );
statement.setInt( 4, 3 );
statement.executeUpdate();
statement.close();
connection.commit();
}
// .. read inserted row
final Date READ_SQL_DATE;
{
final Statement statement =
connection.createStatement();
final String SELECT_STATEMENT = "SELECT * FROM " +
TABLE_NAME + "
WHERE ID=" + ID;
final ResultSet metaResult = statement.executeQuery
( SELECT_STATEMENT );
metaResult.next();
READ_SQL_DATE = metaResult.getDate( 3 );
statement.close();
}
// .. check results
System.out.println( "Written date is: " + WRITE_SQL_DATE.getTime
() );
System.out.println( "Read date is: " + READ_SQL_DATE.getTime()
);
assert( WRITE_SQL_DATE.equals( READ_SQL_DATE ) );
assert( UTIL_DATE.equals( convertSqlToUtilDate( READ_SQL_DATE )
) );
assert( UTIL_DATE.equals( READ_SQL_DATE ) );
connection.close();
DeleteDbFiles.execute( DB_FOLDER, DB_NAME, true );
// perform same test after closing and then opening the db
}
}
</pre>
--
You received this message because you are subscribed to the Google Groups "H2
Database" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/h2-database?hl=en.