Example usage for java.sql PreparedStatement setLong

List of usage examples for java.sql PreparedStatement setLong

Introduction

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

Prototype

void setLong(int parameterIndex, long x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java long value.

Usage

From source file:ca.qc.adinfo.rouge.variable.db.PersistentVariableDb.java

public static boolean updatePersitentVariable(DBManager dbManager, User user, Variable variable) {

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

    if (variable.getVersion() == 0) {
        sql = "INSERT INTO rouge_persistant_variable (`key`, `value`, `version`, `creator_user_id`) "
                + " VALUES (?, ?, ?, ?);";
    } else {
        sql = "UPDATE rouge_persistant_variable SET `value` = ?, `version` = ? "
                + " WHERE `key` = ? AND `version` = ?";
    }

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

        if (variable.getVersion() == 0) {
            stmt.setString(1, variable.getKey());
            stmt.setString(2, variable.getValue().toJSON().toString());
            stmt.setLong(3, 1);
            stmt.setLong(4, user.getId());

            variable.setVersion(1);
        } else {
            stmt.setString(1, variable.getValue().toJSON().toString());
            stmt.setLong(2, variable.getVersion() + 1);
            stmt.setString(3, variable.getKey());
            stmt.setLong(4, variable.getVersion());

            variable.setVersion(variable.getVersion() + 1);
        }

        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:at.becast.youploader.database.SQLite.java

public static int addUpload(File file, Video data, VideoMetadata metadata, Date startAt)
        throws SQLException, IOException {
    PreparedStatement prest = null;
    ObjectMapper mapper = new ObjectMapper();
    String sql = "INSERT INTO `uploads` (`account`, `file`, `lenght`, `data`,`enddir`, `metadata`, `status`,`starttime`) "
            + "VALUES (?,?,?,?,?,?,?,?)";
    prest = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
    prest.setInt(1, metadata.getAccount());
    prest.setString(2, file.getAbsolutePath());
    prest.setLong(3, file.length());
    prest.setString(4, mapper.writeValueAsString(data));
    prest.setString(5, metadata.getEndDirectory());
    prest.setString(6, mapper.writeValueAsString(metadata));
    prest.setString(7, UploadManager.Status.NOT_STARTED.toString());
    if (startAt == null) {
        prest.setString(8, "");
    } else {//  w w  w. ja v a2s  . c o  m
        prest.setDate(8, new java.sql.Date(startAt.getTime()));
    }
    prest.execute();
    ResultSet rs = prest.getGeneratedKeys();
    prest.close();
    if (rs.next()) {
        int id = rs.getInt(1);
        rs.close();
        return id;
    } else {
        return -1;
    }
}

From source file:com.keybox.manage.db.UserDB.java

/**
 * updates existing user//from   ww  w .j a va  2 s. c o m
 * @param user user object
 */
public static void updateUserCredentials(User user) {

    Connection con = null;
    try {
        con = DBUtils.getConn();
        String salt = EncryptionUtil.generateSalt();
        PreparedStatement stmt = con.prepareStatement(
                "update users set first_nm=?, last_nm=?, email=?, username=?, user_type=?, password=?, salt=? where id=?");
        stmt.setString(1, user.getFirstNm());
        stmt.setString(2, user.getLastNm());
        stmt.setString(3, user.getEmail());
        stmt.setString(4, user.getUsername());
        stmt.setString(5, user.getUserType());
        stmt.setString(6, EncryptionUtil.hash(user.getPassword() + salt));
        stmt.setString(7, salt);
        stmt.setLong(8, user.getId());
        stmt.execute();
        DBUtils.closeStmt(stmt);
        if (User.ADMINISTRATOR.equals(user.getUserType())) {
            PublicKeyDB.deleteUnassignedKeysByUser(con, user.getId());
        }

    } catch (Exception e) {
        log.error(e.toString(), e);
    }
    DBUtils.closeConn(con);

}

From source file:com.flexive.core.security.FxDBAuthentication.java

/**
 * Mark a user as no longer active in the database.
 *
 * @param ticket the ticket of the user/*from w w w.  j a  va  2  s  .  c o m*/
 * @throws javax.security.auth.login.LoginException
 *          if the function failed
 */
public static void logout(UserTicket ticket) throws LoginException {
    PreparedStatement ps = null;
    String curSql;
    Connection con = null;
    FxContext inf = FxContext.get();
    try {

        // Obtain a database connection
        con = Database.getDbConnection();

        // EJBLookup user in the database, combined with a update statement to make sure
        // nothing changes between the lookup/set ISLOGGEDIN flag.
        curSql = "UPDATE " + TBL_ACCOUNT_DETAILS + " SET ISLOGGEDIN=? WHERE ID=? AND APPLICATION=?";
        ps = con.prepareStatement(curSql);
        ps.setBoolean(1, false);
        ps.setLong(2, ticket.getUserId());
        ps.setString(3, inf.getApplicationId());

        // Not more than one row should be affected, or the logout failed
        final int rowCount = ps.executeUpdate();
        if (rowCount > 1) {
            // Logout failed.
            LoginException le = new LoginException("Logout for user [" + ticket.getUserId() + "] failed");
            LOG.error(le);
            throw le;
        }

    } catch (SQLException exc) {
        LoginException le = new LoginException("Database error: " + exc.getMessage());
        LOG.error(le);
        throw le;
    } finally {
        Database.closeObjects(FxDBAuthentication.class, con, ps);
    }
}

From source file:com.keybox.manage.db.SystemDB.java

/**
 * updates host system record/*w  w w .  ja  v  a 2s . co m*/
 *
 * @param hostSystem host system object
 */
public static void updateSystem(HostSystem hostSystem) {

    Connection con = null;

    try {
        con = DBUtils.getConn();

        PreparedStatement stmt = con.prepareStatement(
                "update system set display_nm=?, user=?, host=?, port=?, authorized_keys=?, status_cd=?  where id=?");
        stmt.setString(1, hostSystem.getDisplayNm());
        stmt.setString(2, hostSystem.getUser());
        stmt.setString(3, hostSystem.getHost());
        stmt.setInt(4, hostSystem.getPort());
        stmt.setString(5, hostSystem.getAuthorizedKeys());
        stmt.setString(6, hostSystem.getStatusCd());
        stmt.setLong(7, hostSystem.getId());
        stmt.execute();
        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        log.error(e.toString(), e);
    }
    DBUtils.closeConn(con);

}

From source file:com.keybox.manage.db.AuthDB.java

/**
 * updates the admin table based on auth id
 *
 * @param con  DB connection/*w  w w. jav a 2s  .co m*/
 * @param auth username and password object
 */
public static void updateLogin(Connection con, Auth auth) {

    try {
        PreparedStatement stmt = con.prepareStatement(
                "update users set username=?, auth_type=?, auth_token=?, password=?, salt=? where id=?");
        stmt.setString(1, auth.getUsername());
        stmt.setString(2, auth.getAuthType());
        stmt.setString(3, auth.getAuthToken());
        if (StringUtils.isNotEmpty(auth.getPassword())) {
            String salt = EncryptionUtil.generateSalt();
            stmt.setString(4, EncryptionUtil.hash(auth.getPassword() + salt));
            stmt.setString(5, salt);
        } else {
            stmt.setString(4, null);
            stmt.setString(5, null);
        }
        stmt.setLong(6, auth.getId());
        stmt.execute();

        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        log.error(e.toString(), e);
    }

}

From source file:com.flexive.core.Database.java

/**
 * Store a FxString in a translation table that only consists of one(!) translation column
 *
 * @param string     string to be stored
 * @param con        existing connection
 * @param table      storage table//from www . j a  va  2 s. c  om
 * @param dataColumn name of the data column
 * @param idColumn   name of the id column
 * @param id         id of the given string
 * @throws SQLException if a database error occured
 */
public static void storeFxString(FxString string, Connection con, String table, String dataColumn,
        String idColumn, long id) throws SQLException {
    if (!string.isMultiLanguage()) {
        throw new FxInvalidParameterException("string", LOG, "ex.db.fxString.store.multilang", table)
                .asRuntimeException();
    }
    PreparedStatement ps = null;
    try {
        ps = con.prepareStatement("DELETE FROM " + table + ML + " WHERE " + idColumn + "=?");
        ps.setLong(1, id);
        ps.execute();
        ps.close();
        if (string.getTranslatedLanguages().length > 0) {
            ps = con.prepareStatement("INSERT INTO " + table + ML + " (" + idColumn + ",LANG,DEFLANG,"
                    + dataColumn + ") VALUES (?,?,?,?)");
            ps.setLong(1, id);
            String curr;
            for (long lang : string.getTranslatedLanguages()) {
                curr = string.getTranslation(lang);
                if (curr != null && curr.trim().length() > 0) {
                    ps.setInt(2, (int) lang);
                    ps.setBoolean(3, lang == string.getDefaultLanguage());
                    ps.setString(4, curr);
                    ps.executeUpdate();
                }
            }
        }
    } finally {
        if (ps != null)
            ps.close();
    }
}

From source file:org.openmrs.module.bahmniexports.example.domain.trade.internal.CustomerCreditUpdatePreparedStatementSetter.java

@Override
public void setValues(CustomerCredit customerCredit, PreparedStatement ps) throws SQLException {
    ps.setBigDecimal(1, customerCredit.getCredit().add(FIXED_AMOUNT));
    ps.setLong(2, customerCredit.getId());
}

From source file:org.tibetjungle.demo.dao.ContactDaoImpl.java

public void delete(final Long contactId) {
    getJdbcTemplate().update("delete from contacts where id = ?", new PreparedStatementSetter() {
        public void setValues(PreparedStatement ps) throws SQLException {
            ps.setLong(1, contactId);
        }// ww w . j  a va 2s.co  m
    });
}

From source file:org.tibetjungle.demo.dao.ContactDaoImpl.java

public void create(final Contact contact) {
    getJdbcTemplate().update("insert into contacts values (?, ?, ?)", new PreparedStatementSetter() {
        public void setValues(PreparedStatement ps) throws SQLException {
            ps.setLong(1, contact.getId());
            ps.setString(2, contact.getName());
            ps.setString(3, contact.getEmail());
        }/*from  www .j  a  va 2  s.c om*/
    });
}