package jag.tests; import static org.junit.Assert.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import org.junit.After; import org.junit.Before; import org.junit.Test; public class MariaDBDateTest { private static final String url = "jdbc:mysql://" + // "jdbc:mariadb://" + System.getProperty("hostname", "localhost") + ":" + System.getProperty("port", "3306") + "/" + System.getProperty("dbname", "database"); private static final String user = System.getProperty("username", "username"); private static final String passwd = System.getProperty("password", "password"); private Connection con; private java.sql.Time aTime = new java.sql.Time(12,0,0); @Before public void setUp() throws Exception { Class.forName("org.mariadb.jdbc.Driver"); // Load in the DriverManager Class // Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(url, user, passwd); createDB(); System.out.println("Setup Complete."); System.out.println("**********"); } @After public void tearDown() throws Exception { con.close(); System.out.println("Teardown Complete."); System.out.println("**********"); } private void createDB() throws Exception { con.setAutoCommit(true); PreparedStatement pstmt_drop = con.prepareStatement("DROP TABLE DATE_TABLE"); try { pstmt_drop.executeUpdate(); } catch (Throwable t) { } PreparedStatement pstmt_create = con.prepareStatement("CREATE TABLE DATE_TABLE (ID INT PRIMARY KEY, SOMETIME TIME)"); pstmt_create.executeUpdate(); PreparedStatement pstmt_add = con.prepareStatement("INSERT INTO DATE_TABLE (ID, SOMETIME) VALUES (?, ?)"); pstmt_add.setInt(1, 1); pstmt_add.setTime(2, aTime); pstmt_add.executeUpdate(); } @Test public void testDateTime() throws Exception { String sql = "SELECT t0.ID, t0.SOMETIME FROM DATE_TABLE t0 WHERE ID = ?"; PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setInt(1, 1); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { assertEquals(1, rs.getInt(1)); assertEquals(aTime, rs.getTime(2)); assertEquals(aTime, rs.getTimestamp(2)); } } }