Example usage for java.sql PreparedStatement setTime

List of usage examples for java.sql PreparedStatement setTime

Introduction

In this page you can find the example usage for java.sql PreparedStatement setTime.

Prototype

void setTime(int parameterIndex, java.sql.Time x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given java.sql.Time value.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Time time = new Time(0);

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    String sql = "INSERT child VALUES(?,?)";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setString(1, "vinod");
    prest.setTime(2, time.valueOf("1:60:60"));
    int row = prest.executeUpdate();
    System.out.println(row + " row(s) affectec)");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (id int,myDate TIME);");
    String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)";

    PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);
    pstmt.setString(1, "1");
    java.sql.Time sqlDate = new java.sql.Time(new java.util.Date().getTime());
    pstmt.setTime(2, sqlDate);

    pstmt.executeUpdate();//from   w  w  w  . j  ava  2s .c  om

    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    outputResultSet(rs);

    rs.close();
    st.close();
    conn.close();
}

From source file:DemoPreparedStatementSetTimeAndTimestamp.java

public static void main(String[] args) throws Exception {
    String id = "0001";
    Connection conn = null;//w ww  .  j  a va 2s. co  m
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        String query = "insert into time_table(id,time_column, timestamp_column) values(?, ?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, id);
        java.sql.Time time = getCurrentJavaSqlTime();
        System.out.println("time=" + time);
        pstmt.setTime(2, time);
        java.sql.Timestamp timestamp = getCurrentJavaSqlTimestamp();
        System.out.println("timestamp=" + timestamp);
        pstmt.setTimestamp(3, timestamp);
        // execute query, and return number of rows created
        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
    } finally {
        pstmt.close();
        conn.close();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);/*from  w ww.ja va 2 s  . co  m*/

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);
    String sql = "INSERT INTO mysql_all_table(" + "col_boolean," + "col_byte," + "col_short," + "col_int,"
            + "col_long," + "col_float," + "col_double," + "col_bigdecimal," + "col_string," + "col_date,"
            + "col_time," + "col_timestamp," + "col_asciistream," + "col_binarystream," + "col_blob) "
            + "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    PreparedStatement pstmt = connection.prepareStatement(sql);

    pstmt.setBoolean(1, true);
    pstmt.setByte(2, (byte) 123);
    pstmt.setShort(3, (short) 123);
    pstmt.setInt(4, 123);
    pstmt.setLong(5, 123L);
    pstmt.setFloat(6, 1.23F);
    pstmt.setDouble(7, 1.23D);
    pstmt.setBigDecimal(8, new BigDecimal(1.23));
    pstmt.setString(9, "a string");
    pstmt.setDate(10, new java.sql.Date(System.currentTimeMillis()));
    pstmt.setTime(11, new Time(System.currentTimeMillis()));
    pstmt.setTimestamp(12, new Timestamp(System.currentTimeMillis()));

    File file = new File("infilename1");
    FileInputStream is = new FileInputStream(file);
    pstmt.setAsciiStream(13, is, (int) file.length());

    file = new File("infilename2");
    is = new FileInputStream(file);
    pstmt.setBinaryStream(14, is, (int) file.length());

    file = new File("infilename3");
    is = new FileInputStream(file);
    pstmt.setBinaryStream(15, is, (int) file.length());

    pstmt.executeUpdate();
}

From source file:InsertDateToOracle.java

public static void main(String args[]) throws Exception {
    String INSERT_RECORD = "insert into TestDates(id, date_column, "
            + "time_column, timestamp_column) values(?, ?, ?, ?)";
    Connection conn = null;// w  ww. j  av  a 2  s.  co  m
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        pstmt = conn.prepareStatement(INSERT_RECORD);
        pstmt.setString(1, "001");

        java.util.Date date = new java.util.Date();
        long t = date.getTime();
        java.sql.Date sqlDate = new java.sql.Date(t);
        java.sql.Time sqlTime = new java.sql.Time(t);
        java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(t);
        System.out.println("sqlDate=" + sqlDate);
        System.out.println("sqlTime=" + sqlTime);
        System.out.println("sqlTimestamp=" + sqlTimestamp);
        pstmt.setDate(2, sqlDate);
        pstmt.setTime(3, sqlTime);
        pstmt.setTimestamp(4, sqlTimestamp);
        pstmt.executeUpdate();
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Failed to insert the record.");
    } finally {
        pstmt.close();
        conn.close();
    }
}

From source file:com.streamsets.pipeline.lib.jdbc.multithread.TableReadContext.java

private static void setParamVal(PreparedStatement ps, int paramIdx, int sqlType, String paramVal)
        throws SQLException, StageException {
    Utils.checkState(OffsetQueryUtil.SQL_TYPE_TO_FIELD_TYPE.containsKey(sqlType),
            Utils.format("Unsupported Partition Offset Type: {}", sqlType));
    //All Date/Time Types are stored as long offsets
    //Parse string to get long.
    switch (sqlType) {
    case Types.TIME:
        ps.setTime(paramIdx, new java.sql.Time(Long.valueOf(paramVal)));
        break;//from w  w  w .  j  a  va2 s . c o  m
    case Types.DATE:
        ps.setDate(paramIdx, new java.sql.Date(Long.valueOf(paramVal)));
        break;
    case Types.TIMESTAMP:
        Timestamp ts = TableContextUtil.getTimestampForOffsetValue(paramVal);
        ps.setTimestamp(paramIdx, ts);
        break;
    default:
        ps.setObject(paramIdx,
                Field.create(OffsetQueryUtil.SQL_TYPE_TO_FIELD_TYPE.get(sqlType), paramVal).getValue());
    }
}

From source file:com.streamsets.pipeline.stage.origin.jdbc.table.BaseTableJdbcSourceIT.java

protected static void setParamsToPreparedStatement(PreparedStatement ps, int paramIdx, int sqlType,
        Object value) throws SQLException {
    switch (sqlType) {
    case Types.DATE:
        ps.setDate(paramIdx, new java.sql.Date(((Date) value).getTime()));
        break;//from  w ww .j av a 2  s .c  om
    case Types.TIME:
        ps.setTime(paramIdx, new java.sql.Time(((Date) value).getTime()));
        break;
    case Types.TIMESTAMP:
        ps.setTimestamp(paramIdx, new java.sql.Timestamp(((Date) value).getTime()));
        break;
    default:
        ps.setObject(paramIdx, value);
    }
}

From source file:com.adaptris.core.services.jdbc.TimeStatementParameter.java

@Override
public void apply(int parameterIndex, PreparedStatement statement, AdaptrisMessage msg)
        throws SQLException, ServiceException {
    statement.setTime(parameterIndex, (java.sql.Time) this.toDate(this.getQueryValue(msg)));
}

From source file:com.wabacus.system.datatype.CTimeType.java

public void setPreparedStatementValue(int iindex, String value, PreparedStatement pstmt, AbsDatabaseType dbtype)
        throws SQLException {
    log.debug("setTime(" + iindex + "," + value + ")");
    pstmt.setTime(iindex, (java.sql.Time) label2value(value));
}

From source file:com.wabacus.system.datatype.CTimeType.java

public void setPreparedStatementValue(int index, Date dateValue, PreparedStatement pstmt) throws SQLException {
    log.debug("setTime(" + index + "," + dateValue + ")");
    if (dateValue == null) {
        pstmt.setTime(index, null);
    } else {/*from w w  w.  j  a  va 2 s. co m*/
        pstmt.setTime(index, new java.sql.Time(dateValue.getTime()));
    }
}