Example usage for java.sql PreparedStatement setTimestamp

List of usage examples for java.sql PreparedStatement setTimestamp

Introduction

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

Prototype

void setTimestamp(int parameterIndex, java.sql.Timestamp x) throws SQLException;

Source Link

Document

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

Usage

From source file:com.sql.Audit.java

/**
 * Adds an entry to the audit table// w w  w . ja  v  a  2s .c o m
 * @param action performed action to be stored
 */
public static void addAuditEntry(String action) {
    Connection conn = null;
    PreparedStatement ps = null;
    try {

        conn = DBConnection.connectToDB();

        String sql = "INSERT INTO Audit VALUES" + "(?,?,?)";

        ps = conn.prepareStatement(sql);
        ps.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
        ps.setInt(2, 0);
        ps.setString(3, action == null ? "MISSING ACTION" : StringUtils.left(action, 255));

        ps.executeUpdate();
    } catch (SQLException ex) {
        if (ex.getCause() instanceof SQLServerException) {
            addAuditEntry(action);
        }
    } finally {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
    }
}

From source file:dsd.dao.DAOProvider.java

/**
 * calls the correct method for setting the command parameter depending on
 * parameter type/*  w  ww .  j  a v  a 2 s . c o  m*/
 * 
 * @param command
 * @param object
 * @param parameterIndex
 * @throws SQLException
 */
private static void SetParameter(PreparedStatement command, Object object, int parameterIndex)
        throws SQLException {
    if (object instanceof Timestamp) {
        command.setTimestamp(parameterIndex, (Timestamp) object);
    } else if (object instanceof String) {
        command.setString(parameterIndex, (String) object);
    } else if (object instanceof Long) {
        command.setLong(parameterIndex, (Long) object);
    } else if (object instanceof Integer) {
        command.setInt(parameterIndex, (Integer) object);
    } else if (object instanceof Boolean) {
        command.setBoolean(parameterIndex, (Boolean) object);
    } else if (object instanceof Float) {
        command.setFloat(parameterIndex, (Float) object);
    } else {
        throw new IllegalArgumentException(
                "type needs to be inserted in Set parameter method of DAOProvider class");
    }

}

From source file:module.entities.NameFinder.DB.java

/**
 * Update the activity log//from  ww  w.  j  av a2s  .com
 *
 * @param endTime
 * @param status_id
 * @param regexerId
 * @param obj
 * @throws java.sql.SQLException
 */
public static void UpdateLogRegexFinder(long endTime, int regexerId, JSONObject obj) throws SQLException {
    String updateCrawlerStatusSql = "UPDATE log.activities SET " + "end_date = ?, status_id = ?, message = ?"
            + "WHERE id = ?";
    PreparedStatement prepUpdStatusSt = connection.prepareStatement(updateCrawlerStatusSql);
    prepUpdStatusSt.setTimestamp(1, new java.sql.Timestamp(endTime));
    prepUpdStatusSt.setInt(2, 2);
    prepUpdStatusSt.setString(3, obj.toJSONString());
    prepUpdStatusSt.setInt(4, regexerId);
    prepUpdStatusSt.executeUpdate();
    prepUpdStatusSt.close();
}

From source file:module.entities.NameFinder.DB.java

/**
 * Starts the activity log/* www  .  j a va 2 s .c  om*/
 *
 * @param startTime - The start time of the crawling procedure
 * @return - The activity's log id
 * @throws java.sql.SQLException
 */
public static int LogRegexFinder(long startTime) throws SQLException {
    String insertLogSql = "INSERT INTO log.activities (module_id, start_date, end_date, status_id, message) VALUES (?,?,?,?,?)";
    PreparedStatement prepLogCrawlStatement = connection.prepareStatement(insertLogSql,
            Statement.RETURN_GENERATED_KEYS);
    prepLogCrawlStatement.setInt(1, 4);
    prepLogCrawlStatement.setTimestamp(2, new java.sql.Timestamp(startTime));
    prepLogCrawlStatement.setTimestamp(3, null);
    prepLogCrawlStatement.setInt(4, 1);
    prepLogCrawlStatement.setString(5, null);
    prepLogCrawlStatement.executeUpdate();
    ResultSet rsq = prepLogCrawlStatement.getGeneratedKeys();
    int crawlerId = 0;
    if (rsq.next()) {
        crawlerId = rsq.getInt(1);
    }
    prepLogCrawlStatement.close();
    return crawlerId;
}

From source file:ca.qc.adinfo.rouge.mail.db.MailDb.java

public static boolean sendMail(DBManager dbManager, long fromId, long toId, RougeObject content) {

    Connection connection = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;//from   w  ww  .j a v  a  2 s  . c  o  m
    String sql = null;

    sql = "INSERT INTO rouge_mail (`from`, `to`, `content`, `status`, `time_sent`) " + "VALUES (?, ?, ?, ?, ?)";

    try {
        connection = dbManager.getConnection();
        stmt = connection.prepareStatement(sql);

        stmt.setLong(1, fromId);
        stmt.setLong(2, toId);
        stmt.setString(3, content.toJSON().toString());
        stmt.setString(4, "unr");
        stmt.setTimestamp(5, new Timestamp(System.currentTimeMillis()));

        int ret = stmt.executeUpdate();

        return (ret > 0);

    } catch (SQLException e) {
        log.error(stmt);
        log.error(e);
        return false;

    } finally {

        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(stmt);
        DbUtils.closeQuietly(connection);
    }
}

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  w w.  ja v a 2s .  c  o  m
    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.alibaba.wasp.jdbc.TestPreparedStatement.java

public static void testCoalesce() throws SQLException {
    Statement stat = conn.createStatement();
    stat.executeUpdate("create table test(tm timestamp)");
    stat.executeUpdate("insert into test values(current_timestamp)");
    PreparedStatement prep = conn.prepareStatement("update test set tm = coalesce(?,tm)");
    prep.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
    prep.executeUpdate();/* w ww .  j  a  va 2  s  .c o  m*/
    stat.executeUpdate("drop table test");
}

From source file:com.wso2telco.historylog.DbTracelog.java

/**
 * Log history./*from  w  ww  .j  av a 2 s. co  m*/
 *
 * @param Reqtype         the reqtype
 * @param isauthenticated the isauthenticated
 * @param application     the application
 * @param authUser        the auth user
 * @param authenticators  the authenticators
 * @param ipaddress       the ipaddress
 * @throws LogHistoryException the log history exception
 */
public static void LogHistory(String Reqtype, boolean isauthenticated, String application, String authUser,
        String authenticators, String ipaddress) throws LogHistoryException {
    Connection con = null;
    PreparedStatement pst = null;
    try {
        con = DbTracelog.getMobileDBConnection();

        String sql = "INSERT INTO sp_login_history (reqtype, application_id, authenticated_user, isauthenticated,"
                + " authenticators,ipaddress, created, created_date)" + " VALUES " + "(?, ?, ?, ?, ?, ?, ?, ?)";

        pst = con.prepareStatement(sql);

        pst.setString(1, Reqtype);
        pst.setString(2, application);
        pst.setString(3, authUser);
        pst.setInt(4, (isauthenticated ? 1 : 0));
        pst.setString(5, authenticators);
        pst.setString(6, ipaddress);
        pst.setString(7, "authUser");
        pst.setTimestamp(8, new java.sql.Timestamp(new java.util.Date().getTime()));
        pst.executeUpdate();

    } catch (SQLException e) {
        handleException(
                "Error occured while Login SP LogHistory: " + application + " Service Provider: " + authUser,
                e);
    } finally {
        DbUtil.closeAllConnections(pst, con, null);
    }
}

From source file:com.trackplus.ddl.DataWriter.java

private static void insertClobData(Connection con, String line) throws DDLException {
    /*/*  w w w . j a v a2s. com*/
    "OBJECTID",//Integer not null
    "EXCHANGEDIRECTION",//Integer not null
    "ENTITYID",//Integer not null
    "ENTITYTYPE",//Integer not null
    "FILENAME",//Varchar(255)
    "CHANGEDBY",//Integer
    "LASTEDIT",//Timestamp
    "TPUUID",//Varchar(36)
    "FILECONTENT"//Blob sub_type 1
     */

    String sql = "INSERT INTO TMSPROJECTEXCHANGE(OBJECTID, EXCHANGEDIRECTION, ENTITYID,ENTITYTYPE,FILENAME,CHANGEDBY,LASTEDIT,TPUUID,FILECONTENT) "
            + "VALUES(?,?,?,?,?,?,?,?,?)";
    StringTokenizer st = new StringTokenizer(line, ",");
    Integer objectID = Integer.valueOf(st.nextToken());
    Integer exchangeDirection = Integer.valueOf(st.nextToken());
    Integer entityID = Integer.valueOf(st.nextToken());
    Integer entityType = Integer.valueOf(st.nextToken());
    String fileName = st.nextToken();
    if ("null".equalsIgnoreCase(fileName)) {
        fileName = null;
    }
    Integer changedBy = null;
    try {
        changedBy = Integer.valueOf(st.nextToken());
    } catch (Exception ex) {
        LOGGER.debug(ex);
    }

    Timestamp lastEdit = null;
    String lastEditStr = st.nextToken();
    if (lastEditStr != null) {
        try {
            lastEdit = Timestamp.valueOf(lastEditStr);
        } catch (Exception ex) {
            LOGGER.debug(ex);
        }
    }
    String tpuid = st.nextToken();
    String base64Str = st.nextToken();
    if (base64Str.length() == 1 && " ".equals(base64Str)) {
        base64Str = "";
    }
    byte[] bytes = Base64.decodeBase64(base64Str);
    String fileContent = new String(bytes);

    try {
        PreparedStatement preparedStatement = con.prepareStatement(sql);

        preparedStatement.setInt(1, objectID);
        preparedStatement.setInt(2, exchangeDirection);
        preparedStatement.setInt(3, entityID);
        preparedStatement.setInt(4, entityType);
        preparedStatement.setString(5, fileName);
        preparedStatement.setInt(6, changedBy);
        preparedStatement.setTimestamp(7, lastEdit);
        preparedStatement.setString(8, tpuid);
        preparedStatement.setString(9, fileContent);

        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        throw new DDLException(e.getMessage(), e);
    }

}

From source file:nl.strohalm.cyclos.utils.JDBCWrapper.java

/**
 * Set the given positional parameters on a prepared statement, guessing the argument types
 *///www .j a v a2s. co m
private static void setParameters(final PreparedStatement ps, final Object... parameters) throws SQLException {
    if (ps == null || ArrayUtils.isEmpty(parameters)) {
        return;
    }
    for (int i = 0; i < parameters.length; i++) {
        final Object object = parameters[i];
        final int index = i + 1;
        if (object instanceof Number) {
            ps.setBigDecimal(index, CoercionHelper.coerce(BigDecimal.class, object));
        } else if ((object instanceof Calendar) || (object instanceof Date)) {
            final Calendar cal = CoercionHelper.coerce(Calendar.class, object);
            ps.setTimestamp(index, new Timestamp(cal.getTimeInMillis()));
        } else if (object instanceof Boolean) {
            ps.setBoolean(index, (Boolean) object);
        } else {
            ps.setString(index, CoercionHelper.coerce(String.class, object));
        }
    }
}