Example usage for java.sql PreparedStatement setString

List of usage examples for java.sql PreparedStatement setString

Introduction

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

Prototype

void setString(int parameterIndex, String x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java String value.

Usage

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

/**
 * updates password for admin using auth token
 *///  ww  w.j  ava2s .  co m
public static boolean updatePassword(Auth auth) {
    boolean success = false;

    Connection con = null;
    try {
        con = DBUtils.getConn();

        String prevSalt = getSaltByAuthToken(con, auth.getAuthToken());
        PreparedStatement stmt = con
                .prepareStatement("select * from users where auth_token like ? and password like ?");
        stmt.setString(1, auth.getAuthToken());
        stmt.setString(2, EncryptionUtil.hash(auth.getPrevPassword() + prevSalt));
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {

            String salt = EncryptionUtil.generateSalt();
            stmt = con.prepareStatement("update users set password=?, salt=? where auth_token like ?");
            stmt.setString(1, EncryptionUtil.hash(auth.getPassword() + salt));
            stmt.setString(2, salt);
            stmt.setString(3, auth.getAuthToken());
            stmt.execute();
            success = true;
        }

        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

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

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

/**
 * returns user base on username/*from  w  w w  .ja  v  a  2s . com*/
 *
 * @param con DB connection
 * @param uid username id
 * @return user object
 */
public static User getUserByUID(Connection con, String uid) {

    User user = null;
    try {
        PreparedStatement stmt = con
                .prepareStatement("select * from  users where lower(username) like lower(?) and enabled=true");
        stmt.setString(1, uid);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            user = new User();
            user.setId(rs.getLong("id"));
            user.setEmail(rs.getString("email"));
            user.setUsername(rs.getString("username"));
            user.setUserType(rs.getString("user_type"));
            user.setProfileList(UserProfileDB.getProfilesByUser(con, user.getId()));
        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

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

    return user;
}

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

/**
 * updates existing user/*from   w ww  . jav  a 2s  .  com*/
 * @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  email=?, username=?, user_type=?, password=?, salt=? where id=?");
        stmt.setString(1, user.getEmail());
        stmt.setString(2, user.getUsername());
        stmt.setString(3, user.getUserType());
        stmt.setString(4, EncryptionUtil.hash(user.getPassword() + salt));
        stmt.setString(5, salt);
        stmt.setLong(6, 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.keybox.manage.db.AuthDB.java

/**
 * returns user base on username/*from ww  w.  j  av a 2s . c  om*/
 *
 * @param con DB connection
 * @param uid username id
 * @return user object
 */
public static User getUserByUID(Connection con, String uid) {

    User user = null;
    try {
        PreparedStatement stmt = con
                .prepareStatement("select * from  users where lower(username) like lower(?) and enabled=true");
        stmt.setString(1, uid);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            user = new User();
            user.setId(rs.getLong("id"));
            user.setFirstNm(rs.getString("first_nm"));
            user.setLastNm(rs.getString("last_nm"));
            user.setEmail(rs.getString("email"));
            user.setUsername(rs.getString("username"));
            user.setUserType(rs.getString("user_type"));
            user.setProfileList(UserProfileDB.getProfilesByUser(con, user.getId()));
        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

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

    return user;
}

From source file:com.act.lcms.db.model.CuratedChemical.java

protected static void bindInsertOrUpdateParameters(PreparedStatement stmt, String name, String inchi,
        Double mPlusHPlusMass, Integer expectedCollisionVoltage, String referenceUrl) throws SQLException {
    stmt.setString(DB_FIELD.NAME.getInsertUpdateOffset(), name);
    stmt.setString(DB_FIELD.INCHI.getInsertUpdateOffset(), inchi);
    stmt.setDouble(DB_FIELD.MASS.getInsertUpdateOffset(), mPlusHPlusMass);
    if (expectedCollisionVoltage != null) {
        stmt.setInt(DB_FIELD.EXPECTED_COLLISION_VOLTAGE.getInsertUpdateOffset(), expectedCollisionVoltage);
    } else {/*from   ww  w.j  ava2s .  c om*/
        stmt.setNull(DB_FIELD.EXPECTED_COLLISION_VOLTAGE.getInsertUpdateOffset(), Types.INTEGER);
    }
    stmt.setString(DB_FIELD.REFERENCE_URL.getInsertUpdateOffset(), referenceUrl);
}

From source file:com.wso2telco.mnc.resolver.mncrange.McnRangeDbUtil.java

/**
 * Gets the mcc number ranges./*from ww  w .j ava2 s .c  om*/
 *
 * @param mcc the mcc
 * @return the mcc number ranges
 * @throws MobileNtException the mobile nt exception
 */
public static List<NumberRange> getMccNumberRanges(String mcc) throws MobileNtException {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sql = "SELECT mnccode,brand,rangefrom,rangeto " + "FROM mcc_number_ranges " + "WHERE mcccode = ?";

    List<NumberRange> lstranges = new ArrayList();

    try {
        conn = getAxiataDBConnection();
        ps = conn.prepareStatement(sql);
        ps.setString(1, mcc);
        rs = ps.executeQuery();
        while (rs.next()) {
            lstranges.add(new NumberRange(rs.getLong("rangefrom"), rs.getLong("rangeto"),
                    rs.getString("mnccode"), rs.getString("brand")));
        }
    } catch (SQLException e) {
        handleException("Error occured while getting Number ranges for mcc: " + mcc + " from the database", e);
    } finally {
        McnRangeDbUtil.closeAllConnections(ps, conn, rs);
    }
    return lstranges;
}

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

/**
 * updates the admin table based on auth id
 *
 * @param con  DB connection/*from  ww w .  j  ava  2s  .  c  o 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.dynamobi.network.DynamoNetworkUdr.java

/**
 * Determines status of some jar file./*ww w .  j  a  v a  2  s .c o m*/
 * @return Returns one of DOWNLOADED, INSTALLED, or AVAILABLE.
 */
private static String getStatus(String jarFile) throws SQLException {
    String fn = "${FARRAGO_HOME}/plugin/" + jarFile;
    String outfile = FarragoProperties.instance().expandProperties(fn);
    File f = new File(outfile);
    if (f.exists()) { // Either just downloaded or installed
        String ret = "DOWNLOADED";
        Connection conn = DriverManager.getConnection("jdbc:default:connection");
        String name = jarFile.replaceAll("\\.jar", "");
        String query = "SELECT name FROM localdb.sys_root.dba_jars WHERE " + "name = ? AND url IN (?,?)";
        PreparedStatement ps = conn.prepareStatement(query);
        ps.setString(1, name);
        ps.setString(2, fn);
        ps.setString(3, "file:" + fn);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            ret = "INSTALLED";
        }
        rs.close();
        ps.close();

        return ret;
    } else {
        return "AVAILABLE";
    }
}

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

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

    Connection con = null;
    try {
        con = DBUtils.getConn();
        PreparedStatement stmt = con.prepareStatement(
                "update users set first_nm=?, last_nm=?, email=?, username=?, user_type=? 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.setLong(6, 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.keybox.manage.db.AuthDB.java

/**
 * auth user and return auth token if valid auth
 *
 * @param auth username and password object
 * @return auth token if success/*from  w  ww . jav a  2 s  . c  o m*/
 */
public static String login(Auth auth) {
    //check ldap first
    String authToken = ExternalAuthUtil.login(auth);

    if (StringUtils.isEmpty(authToken)) {

        Connection con = null;

        try {
            con = DBUtils.getConn();

            //get salt for user
            String salt = getSaltByUsername(con, auth.getUsername());
            //login
            PreparedStatement stmt = con
                    .prepareStatement("select * from users where enabled=true and username=? and password=?");
            stmt.setString(1, auth.getUsername());
            stmt.setString(2, EncryptionUtil.hash(auth.getPassword() + salt));
            ResultSet rs = stmt.executeQuery();

            if (rs.next()) {

                auth.setId(rs.getLong("id"));
                authToken = UUID.randomUUID().toString();
                auth.setAuthToken(authToken);
                auth.setAuthType(Auth.AUTH_BASIC);
                updateLogin(con, auth);

            }
            DBUtils.closeRs(rs);
            DBUtils.closeStmt(stmt);

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

        DBUtils.closeConn(con);
    }

    return authToken;

}