List of usage examples for java.sql Time Time
public Time(long time)
Time
object using a milliseconds time value. From source file:org.apache.phoenix.end2end.DateTimeIT.java
@Test public void testProjectedTimeUnsignedTimestampCompare() throws Exception { String tableName = generateUniqueName(); String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " (k1 INTEGER PRIMARY KEY, times TIME, timestamps UNSIGNED_TIMESTAMP)"; conn.createStatement().execute(ddl); // Differ by date String dml = "UPSERT INTO " + tableName + " VALUES (1," + "TO_TIME('2004-02-04 00:10:10')," + "TO_TIMESTAMP('2006-04-12 00:10:10'))"; conn.createStatement().execute(dml); // Differ by time dml = "UPSERT INTO " + tableName + " VALUES (2," + "TO_TIME('2004-02-04 00:10:10'), " + "TO_TIMESTAMP('2004-02-04 15:10:20'))"; conn.createStatement().execute(dml); // Differ by nanoseconds PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?, ?, ?)"); stmt.setInt(1, 3);// ww w .j a va 2 s. co m stmt.setTime(2, new Time(1000)); Timestamp ts = new Timestamp(1000); ts.setNanos(100); stmt.setTimestamp(3, ts); stmt.execute(); // Equality dml = "UPSERT INTO " + tableName + " VALUES (4," + "TO_TIME('2004-02-04 00:10:10'), " + "TO_TIMESTAMP('2004-02-04 00:10:10'))"; conn.createStatement().execute(dml); conn.commit(); ResultSet rs = conn.createStatement().executeQuery("SELECT times = timestamps FROM " + tableName); assertTrue(rs.next()); assertEquals(false, rs.getBoolean(1)); assertTrue(rs.next()); assertEquals(false, rs.getBoolean(1)); assertTrue(rs.next()); assertEquals(false, rs.getBoolean(1)); assertTrue(rs.next()); assertEquals(true, rs.getBoolean(1)); assertFalse(rs.next()); }
From source file:org.dspace.storage.rdbms.MockDatabaseManager.java
@Mock private static void loadParameters(PreparedStatement statement, Collection<ColumnInfo> columns, TableRow row) throws SQLException { int count = 0; for (ColumnInfo info : columns) { count++;/* w w w . j av a 2 s. com*/ String column = info.getName(); int jdbctype = info.getType(); if (row.isColumnNull(column)) { statement.setNull(count, jdbctype); } else { switch (jdbctype) { case Types.BIT: case Types.BOOLEAN: statement.setBoolean(count, row.getBooleanColumn(column)); break; case Types.INTEGER: if (isOracle) { statement.setLong(count, row.getLongColumn(column)); } else { statement.setInt(count, row.getIntColumn(column)); } break; case Types.NUMERIC: case Types.DECIMAL: statement.setLong(count, row.getLongColumn(column)); // FIXME should be BigDecimal if TableRow supported that break; case Types.BIGINT: statement.setLong(count, row.getLongColumn(column)); break; case Types.CLOB: if (isOracle) { // Support CLOBs in place of TEXT columns in Oracle statement.setString(count, row.getStringColumn(column)); } else { throw new IllegalArgumentException("Unsupported JDBC type: " + jdbctype); } break; case Types.VARCHAR: statement.setString(count, row.getStringColumn(column)); break; case Types.DATE: statement.setDate(count, new java.sql.Date(row.getDateColumn(column).getTime())); break; case Types.TIME: statement.setTime(count, new Time(row.getDateColumn(column).getTime())); break; case Types.TIMESTAMP: statement.setTimestamp(count, new Timestamp(row.getDateColumn(column).getTime())); break; default: throw new IllegalArgumentException("Unsupported JDBC type: " + jdbctype); } } } }
From source file:org.apache.phoenix.end2end.DateTimeIT.java
@Test public void testProjectedUnsignedTimeTimestampCompare() throws Exception { String tableName = generateUniqueName(); String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " (k1 INTEGER PRIMARY KEY, times UNSIGNED_TIME, timestamps TIMESTAMP)"; conn.createStatement().execute(ddl); // Differ by date String dml = "UPSERT INTO " + tableName + " VALUES (1," + "TO_TIME('2004-02-04 00:10:10')," + "TO_TIMESTAMP('2006-04-12 00:10:10'))"; conn.createStatement().execute(dml); // Differ by time dml = "UPSERT INTO " + tableName + " VALUES (2," + "TO_TIME('2004-02-04 00:10:10'), " + "TO_TIMESTAMP('2004-02-04 15:10:20'))"; conn.createStatement().execute(dml); // Differ by nanoseconds PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?, ?, ?)"); stmt.setInt(1, 3);// w w w .ja va2 s .co m stmt.setTime(2, new Time(1000)); Timestamp ts = new Timestamp(1000); ts.setNanos(100); stmt.setTimestamp(3, ts); stmt.execute(); // Equality dml = "UPSERT INTO " + tableName + " VALUES (4," + "TO_TIME('2004-02-04 00:10:10'), " + "TO_TIMESTAMP('2004-02-04 00:10:10'))"; conn.createStatement().execute(dml); conn.commit(); ResultSet rs = conn.createStatement().executeQuery("SELECT times = timestamps FROM " + tableName); assertTrue(rs.next()); assertEquals(false, rs.getBoolean(1)); assertTrue(rs.next()); assertEquals(false, rs.getBoolean(1)); assertTrue(rs.next()); assertEquals(false, rs.getBoolean(1)); assertTrue(rs.next()); assertEquals(true, rs.getBoolean(1)); assertFalse(rs.next()); }
From source file:org.apache.phoenix.end2end.DateTimeIT.java
@Test public void testProjectedUnsignedTimeUnsignedTimestampCompare() throws Exception { String tableName = generateUniqueName(); String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " (k1 INTEGER PRIMARY KEY, times UNSIGNED_TIME, timestamps UNSIGNED_TIMESTAMP)"; conn.createStatement().execute(ddl); // Differ by date String dml = "UPSERT INTO " + tableName + " VALUES (1," + "TO_TIME('2004-02-04 00:10:10')," + "TO_TIMESTAMP('2006-04-12 00:10:10'))"; conn.createStatement().execute(dml); // Differ by time dml = "UPSERT INTO " + tableName + " VALUES (2," + "TO_TIME('2004-02-04 00:10:10'), " + "TO_TIMESTAMP('2004-02-04 15:10:20'))"; conn.createStatement().execute(dml); // Differ by nanoseconds PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + tableName + " VALUES (?, ?, ?)"); stmt.setInt(1, 3);/*from ww w . j av a 2 s . co m*/ stmt.setTime(2, new Time(1000)); Timestamp ts = new Timestamp(1000); ts.setNanos(100); stmt.setTimestamp(3, ts); stmt.execute(); // Equality dml = "UPSERT INTO " + tableName + " VALUES (4," + "TO_TIME('2004-02-04 00:10:10'), " + "TO_TIMESTAMP('2004-02-04 00:10:10'))"; conn.createStatement().execute(dml); conn.commit(); ResultSet rs = conn.createStatement().executeQuery("SELECT times = timestamps FROM " + tableName); assertTrue(rs.next()); assertEquals(false, rs.getBoolean(1)); assertTrue(rs.next()); assertEquals(false, rs.getBoolean(1)); assertTrue(rs.next()); assertEquals(false, rs.getBoolean(1)); assertTrue(rs.next()); assertEquals(true, rs.getBoolean(1)); assertFalse(rs.next()); }
From source file:fr.certu.chouette.command.Command.java
protected Object toObject(Class<?> type, String value) throws Exception { if (value == null) return null; String name = type.getSimpleName(); if (name.equals("String")) return value; if (name.equals("Long")) return Long.valueOf(value); if (name.equals("Boolean")) return Boolean.valueOf(value); if (name.equals("Integer")) return Integer.valueOf(value); if (name.equals("Float")) return Float.valueOf(value); if (name.equals("Double")) return Double.valueOf(value); if (name.equals("BigDecimal")) return BigDecimal.valueOf(Double.parseDouble(value)); if (name.equals("Date")) { DateFormat dateFormat = null; if (value.contains("-") && value.contains(":")) { dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss"); } else if (value.contains("-")) { dateFormat = new SimpleDateFormat("yyyy-MM-dd"); } else if (value.contains(":")) { dateFormat = new SimpleDateFormat("HH:mm:ss"); } else {//from w w w . j a v a 2 s.c o m throw new Exception("unable to convert " + value + " to Date"); } Date date = dateFormat.parse(value); return date; } if (name.equals("Time")) { DateFormat dateFormat = null; if (value.contains(":")) { dateFormat = new SimpleDateFormat("H:m:s"); } else { throw new Exception("unable to convert " + value + " to Time"); } Date date = dateFormat.parse(value); Time time = new Time(date.getTime()); return time; } throw new Exception("unable to convert String to " + type.getCanonicalName()); }
From source file:org.apache.phoenix.end2end.DateTimeIT.java
@Test public void testTimestamp() throws Exception { String updateStmt = "upsert into " + tableName + " (" + " ORGANIZATION_ID, " + " ENTITY_ID, " + " A_TIMESTAMP) " + "VALUES (?, ?, ?)"; // Override value that was set at creation time Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); Connection upsertConn = DriverManager.getConnection(url, props); upsertConn.setAutoCommit(true); // Test auto commit PreparedStatement stmt = upsertConn.prepareStatement(updateStmt); stmt.setString(1, tenantId);/*from w w w . ja va 2s . co m*/ stmt.setString(2, ROW4); Timestamp tsValue1 = new Timestamp(5000); byte[] ts1 = PTimestamp.INSTANCE.toBytes(tsValue1); stmt.setTimestamp(3, tsValue1); stmt.execute(); Connection conn1 = DriverManager.getConnection(url, props); TestUtil.analyzeTable(conn1, tableName); conn1.close(); updateStmt = "upsert into " + tableName + " (" + " ORGANIZATION_ID, " + " ENTITY_ID, " + " A_TIMESTAMP," + " A_TIME) " + "VALUES (?, ?, ?, ?)"; stmt = upsertConn.prepareStatement(updateStmt); stmt.setString(1, tenantId); stmt.setString(2, ROW5); Timestamp tsValue2 = new Timestamp(5000); tsValue2.setNanos(200); byte[] ts2 = PTimestamp.INSTANCE.toBytes(tsValue2); stmt.setTimestamp(3, tsValue2); stmt.setTime(4, new Time(tsValue2.getTime())); stmt.execute(); upsertConn.close(); assertTrue(TestUtil.compare(CompareOp.GREATER, new ImmutableBytesWritable(ts2), new ImmutableBytesWritable(ts1))); assertFalse(TestUtil.compare(CompareOp.GREATER, new ImmutableBytesWritable(ts1), new ImmutableBytesWritable(ts1))); String query = "SELECT entity_id, a_timestamp, a_time FROM " + tableName + " WHERE organization_id=? and a_timestamp > ?"; Connection conn = DriverManager.getConnection(url, props); try { PreparedStatement statement = conn.prepareStatement(query); statement.setString(1, tenantId); statement.setTimestamp(2, new Timestamp(5000)); ResultSet rs = statement.executeQuery(); assertTrue(rs.next()); assertEquals(rs.getString(1), ROW5); assertEquals(rs.getTimestamp("A_TIMESTAMP"), tsValue2); assertEquals(rs.getTime("A_TIME"), new Time(tsValue2.getTime())); assertFalse(rs.next()); } finally { conn.close(); } }
From source file:org.apache.phoenix.end2end.DateTimeIT.java
public void testDateFormatTimeZone(String timeZoneId) throws Exception { Properties props = new Properties(); props.setProperty("phoenix.query.dateFormatTimeZone", timeZoneId); Connection conn1 = DriverManager.getConnection(getUrl(), props); String tableName = generateUniqueName(); String ddl = "CREATE TABLE IF NOT EXISTS " + tableName + " (k1 INTEGER PRIMARY KEY," + " v_date DATE," + " v_time TIME," + " v_timestamp TIMESTAMP)"; try {/*from www. ja v a 2s . c o m*/ conn1.createStatement().execute(ddl); PhoenixConnection pConn = conn1.unwrap(PhoenixConnection.class); verifyTimeZoneIDWithConn(pConn, PDate.INSTANCE, timeZoneId); verifyTimeZoneIDWithConn(pConn, PTime.INSTANCE, timeZoneId); verifyTimeZoneIDWithConn(pConn, PTimestamp.INSTANCE, timeZoneId); Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(timeZoneId)); cal.setTime(date); String dateStr = DateUtil.getDateFormatter(DateUtil.DEFAULT_MS_DATE_FORMAT).format(date); String dml = "UPSERT INTO " + tableName + " VALUES (" + "1," + "'" + dateStr + "'," + "'" + dateStr + "'," + "'" + dateStr + "'" + ")"; conn1.createStatement().execute(dml); conn1.commit(); PhoenixStatement stmt = conn1.createStatement().unwrap(PhoenixStatement.class); ResultSet rs = stmt.executeQuery("SELECT v_date, v_time, v_timestamp FROM " + tableName); assertTrue(rs.next()); assertEquals(rs.getDate(1).toString(), new Date(cal.getTimeInMillis()).toString()); assertEquals(rs.getTime(2).toString(), new Time(cal.getTimeInMillis()).toString()); assertEquals(rs.getTimestamp(3).getTime(), cal.getTimeInMillis()); assertFalse(rs.next()); StatementContext stmtContext = stmt.getQueryPlan().getContext(); verifyTimeZoneIDWithFormatter(stmtContext.getDateFormatter(), timeZoneId); verifyTimeZoneIDWithFormatter(stmtContext.getTimeFormatter(), timeZoneId); verifyTimeZoneIDWithFormatter(stmtContext.getTimestampFormatter(), timeZoneId); stmt.close(); } finally { conn1.close(); } }
From source file:fll.db.Queries.java
/** * Convert {@link java.util.Date} to {@link java.sql.Time}. *//*from w w w .j a v a2s . c o m*/ public static Time dateToTime(final Date date) { if (null == date) { return null; } else { return new Time(date.getTime()); } }
From source file:lasige.steeldb.jdbc.BFTRowSet.java
/** * Converts the given <code>Object</code> in the Java programming language * to the standard object mapping for the specified SQL target data type. * The conversion must be to a string or temporal type, and there are also * restrictions on the type to be converted. * <P>/*from w ww.ja v a2s . c o m*/ * <TABLE ALIGN="CENTER" BORDER CELLPADDING=10 BORDERCOLOR="#0000FF" * <CAPTION ALIGN="CENTER"><B>Parameters and Return Values</B></CAPTION> * <TR> * <TD><B>Source SQL Type</B> * <TD><B>Target SQL Type</B> * <TD><B>Object Returned</B> * </TR> * <TR> * <TD><code>TIMESTAMP</code> * <TD><code>DATE</code> * <TD><code>java.sql.Date</code> * </TR> * <TR> * <TD><code>TIMESTAMP</code> * <TD><code>TIME</code> * <TD><code>java.sql.Time</code> * </TR> * <TR> * <TD><code>TIME</code> * <TD><code>TIMESTAMP</code> * <TD><code>java.sql.Timestamp</code> * </TR> * <TR> * <TD><code>DATE</code>, <code>TIME</code>, or <code>TIMESTAMP</code> * <TD><code>CHAR</code>, <code>VARCHAR</code>, or <code>LONGVARCHAR</code> * <TD><code>java.lang.String</code> * </TR> * </TABLE> * <P> * If the source type and target type are the same, * the given object is simply returned. * * @param srcObj the <code>Object</code> in the Java programming language * that is to be converted to the target type * @param srcType the data type that is the standard mapping in SQL of the * object to be converted; must be one of the constants in * <code>java.sql.Types</code> * @param trgType the SQL data type to which to convert the given object; * must be one of the following constants in * <code>java.sql.Types</code>: <code>DATE</code>, * <code>TIME</code>, <code>TIMESTAMP</code>, <code>CHAR</code>, * <code>VARCHAR</code>, or <code>LONGVARCHAR</code> * @return an <code>Object</code> value.that is * the standard object mapping for the target SQL type * @throws SQLException if the given target type is not one of the string or * temporal types in <code>java.sql.Types</code> */ private Object convertTemporal(Object srcObj, int srcType, int trgType) throws SQLException { if (srcType == trgType) { return srcObj; } if (isNumeric(trgType) == true || (isString(trgType) == false && isTemporal(trgType) == false)) { throw new SQLException(resBundle.handleGetObject("cachedrowsetimpl.dtypemismt").toString()); } try { switch (trgType) { case java.sql.Types.DATE: if (srcType == java.sql.Types.TIMESTAMP) { return new java.sql.Date(((java.sql.Timestamp) srcObj).getTime()); } else { throw new SQLException(resBundle.handleGetObject("cachedrowsetimpl.dtypemismt").toString()); } case java.sql.Types.TIMESTAMP: if (srcType == java.sql.Types.TIME) { return new Timestamp(((java.sql.Time) srcObj).getTime()); } else { return new Timestamp(((java.sql.Date) srcObj).getTime()); } case java.sql.Types.TIME: if (srcType == java.sql.Types.TIMESTAMP) { return new Time(((java.sql.Timestamp) srcObj).getTime()); } else { throw new SQLException(resBundle.handleGetObject("cachedrowsetimpl.dtypemismt").toString()); } case java.sql.Types.CHAR: case java.sql.Types.VARCHAR: case java.sql.Types.LONGVARCHAR: return new String(srcObj.toString()); default: throw new SQLException(resBundle.handleGetObject("cachedrowsetimpl.dtypemismt").toString()); } } catch (NumberFormatException ex) { throw new SQLException(resBundle.handleGetObject("cachedrowsetimpl.dtypemismt").toString()); } }
From source file:CampusSmartCafe.view.MainFrame2.java
private void IO_orderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_IO_orderActionPerformed // TODO add your handling code here: int[] quantity = new int[6]; if (IO_q1.getText().trim().length() != 0) { quantity[0] = Integer.parseInt(IO_q1.getText().trim()); }// w w w . j av a2 s.co m if (IO_q2.getText().trim().length() != 0) { quantity[1] = Integer.parseInt(IO_q2.getText().trim()); } if (IO_q3.getText().trim().length() != 0) { quantity[2] = Integer.parseInt(IO_q3.getText().trim()); } if (IO_q4.getText().trim().length() != 0) { quantity[3] = Integer.parseInt(IO_q4.getText().trim()); } if (IO_q5.getText().trim().length() != 0) { quantity[4] = Integer.parseInt(IO_q5.getText().trim()); } if (IO_q6.getText().trim().length() != 0) { quantity[5] = Integer.parseInt(IO_q6.getText().trim()); } float[] prices = { 3.25f, 2.25f, 1.75f, 1.5f, 1.25f, 1, 25f }; int[] calories = { 670, 480, 390, 395, 50, 140 }; float total_f = 0; int total_c = 0; for (int i = 0; i < 6; i++) { total_f += quantity[i] * prices[i]; total_c += quantity[i] * calories[i]; } if (total_c > acc.getDietaryProf().getLeftCalorie()) { JOptionPane.showMessageDialog(null, "You don't have enough Calorie today,\nTry tomorrow :)"); } else if (total_f > acc.getExpenseProf().getLeftFunds()) { JOptionPane.showMessageDialog(null, "You don't have enough money :("); } else { AccountDAO.updateLeft(acc, total_f, total_c); JOptionPane.showMessageDialog(null, "You food will be ready at " + (new Time(System.currentTimeMillis() + 12 * 60 * 1000).toString()) + "\nCafe address is:\n" + "3041 Stevens Creek Blvd\n" + "Santa Clara, CA 95050"); IO_q1.setText(" "); IO_q2.setText(" "); IO_q3.setText(" "); IO_q4.setText(" "); IO_q5.setText(" "); IO_q6.setText(" "); IO_totalfunds.setText(" "); IO_totalcalories.setText(" "); } }