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.seventh_root.ld33.common.player.Player.java

public static Player getByName(Connection databaseConnection, String playerName) throws SQLException {
    if (playersByName.containsKey(playerName))
        return playersByName.get(playerName);
    if (databaseConnection != null) {
        PreparedStatement statement = databaseConnection.prepareStatement(
                "SELECT uuid, name, password_hash, password_salt, resources FROM player WHERE name = ? LIMIT 1");
        statement.setString(1, playerName);
        ResultSet resultSet = statement.executeQuery();
        if (resultSet.next()) {
            return new Player(databaseConnection, UUID.fromString(resultSet.getString("uuid")),
                    resultSet.getString("name"), resultSet.getString("password_hash"),
                    resultSet.getString("password_salt"), resultSet.getInt("resources"));
        }//from www  .  j  a v  a2s .co m
    }
    return null;
}

From source file:com.seventh_root.ld33.common.player.Player.java

public static Player getByUUID(Connection databaseConnection, UUID uuid) throws SQLException {
    if (playersByUUID.containsKey(uuid.toString()))
        return playersByUUID.get(uuid.toString());
    if (databaseConnection != null) {
        PreparedStatement statement = databaseConnection.prepareStatement(
                "SELECT uuid, name, password_hash, password_salt, resources FROM player WHERE uuid = ? LIMIT 1");
        statement.setString(1, uuid.toString());
        ResultSet resultSet = statement.executeQuery();
        if (resultSet.next()) {
            Player player = new Player(databaseConnection, UUID.fromString(resultSet.getString("uuid")),
                    resultSet.getString("name"), resultSet.getString("password_hash"),
                    resultSet.getString("password_salt"), resultSet.getInt("resources"));
            cachePlayer(player);//from w  w w.  j  a va  2s.  c  o  m
            return player;
        }
    }
    return null;
}

From source file:com.concursive.connect.web.modules.upgrade.utils.UpgradeUtils.java

/**
 * Records a database version as being executed
 *
 * @param db      The feature to be added to the Version attribute
 * @param version The feature to be added to the Version attribute
 * @throws SQLException Description of the Exception
 *///from   ww  w .  ja  va2 s. c  o m
public static void addVersion(Connection db, String version) throws SQLException {
    // Add the specified version
    PreparedStatement pst = db.prepareStatement(
            "INSERT INTO database_version " + "(script_filename, script_version) VALUES (?, ?) ");
    pst.setString(1, DatabaseUtils.getTypeName(db) + "_" + version);
    pst.setString(2, version);
    pst.execute();
    pst.close();
}

From source file:com.splicemachine.derby.test.framework.SpliceIndexWatcher.java

/**
 * Use this static method in cases where you want to create an index after creating/loading table.
 * TODO: redirect starting(Description) to call this method
 * @param connection/*www  .j  a v  a  2 s.  c  om*/
 * @param schemaName
 * @param tableName
 * @param indexName
 * @param definition
 * @param unique
 * @throws Exception
 */
public static void createIndex(Connection connection, String schemaName, String tableName, String indexName,
        String definition, boolean unique) throws Exception {
    PreparedStatement statement = null;
    ResultSet rs = null;
    try {
        //            connection = SpliceNetConnection.getConnection();
        statement = connection.prepareStatement(SELECT_SPECIFIC_INDEX);
        statement.setString(1, schemaName);
        statement.setString(2, indexName);
        rs = statement.executeQuery();
        if (rs.next()) {
            SpliceIndexWatcher.executeDrop(connection, schemaName, indexName);
        }
        try (Statement s = connection.createStatement()) {
            System.out.println(String.format("create " + (unique ? "unique" : "") + " index %s.%s on %s.%s %s",
                    schemaName, indexName, schemaName, tableName, definition));
            s.execute(String.format("create " + (unique ? "unique" : "") + " index %s.%s on %s.%s %s",
                    schemaName, indexName, schemaName, tableName, definition));
        }
    } finally {
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(statement);
    }
}

From source file:edu.jhu.pha.vospace.oauth.MySQLOAuthProvider2.java

/**
 * Returns the user the share is made for
 * @param shareId/*w w  w  . j  a va 2  s.  co m*/
 * @return
 */
public static synchronized List<String> getShareUsers(final String shareId) {
    return DbPoolServlet.goSql("Get share user logins",
            "select identity from user_identities JOIN user_groups ON user_groups.user_id = user_identities.user_id JOIN container_shares ON user_groups.group_id = container_shares.group_id AND container_shares.share_id = ?",
            new SqlWorker<List<String>>() {
                @Override
                public List<String> go(Connection conn, PreparedStatement stmt) throws SQLException {
                    stmt.setString(1, shareId);
                    List<String> returnVect = new Vector<String>();
                    ResultSet rs = stmt.executeQuery();
                    while (rs.next()) {
                        returnVect.add(rs.getString(1));
                    }
                    return returnVect;
                }
            });
}

From source file:com.sql.Audit.java

/**
 * Adds an entry to the audit table/*w w  w .j a  v a 2s .  com*/
 * @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:com.concursive.connect.web.modules.upgrade.utils.UpgradeUtils.java

/**
 * Queries the database to see if the script has already been executed
 *
 * @param db      Description of the Parameter
 * @param version Description of the Parameter
 * @return The installed value/*w  w  w. java  2 s.  c  om*/
 * @throws java.sql.SQLException Description of the Exception
 */
public static boolean isInstalled(Connection db, String version) throws SQLException {
    boolean isInstalled = false;
    // Query the installed version
    PreparedStatement pst = db.prepareStatement(
            "SELECT script_version " + "FROM database_version " + "WHERE script_version = ? ");
    pst.setString(1, version);
    ResultSet rs = pst.executeQuery();
    if (rs.next()) {
        isInstalled = true;
    }
    rs.close();
    pst.close();
    return isInstalled;
}

From source file:com.chaosinmotion.securechat.server.commands.Devices.java

/**
 * Return the list of device identifiers associated with this account.
 * @param userinfo/*from  w  w  w . jav a 2  s.  c  om*/
 * @param requestParams
 * @return
 * @throws IOException 
 * @throws SQLException 
 * @throws ClassNotFoundException 
 */
public static ReturnResult processRequest(Login.UserInfo userinfo, JSONObject requestParams)
        throws ClassNotFoundException, SQLException, IOException {
    String username = requestParams.getString("username");

    Connection c = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
        c = Database.get();

        /*
         *    Get user ID
         */

        ps = c.prepareStatement("SELECT userid " + "FROM Users " + "WHERE username = ?");
        ps.setString(1, username);
        rs = ps.executeQuery();

        int userid = 0;
        if (rs.next()) {
            userid = rs.getInt(1);
        } else {
            return new ReturnResult(Errors.ERROR_UNKNOWNUSER, "Unknown user");
        }
        rs.close();
        rs = null;
        ps.close();
        ps = null;

        /*
         * Get devices
         */
        ps = c.prepareStatement("SELECT Devices.deviceuuid, Devices.publickey " + "FROM Devices, Users "
                + "WHERE Users.userid = Devices.userid " + "AND Users.username = ?");
        ps.setString(1, username);
        rs = ps.executeQuery();

        DeviceReturnResult drr = new DeviceReturnResult(userid);
        while (rs.next()) {
            drr.addDeviceUUID(rs.getString(1), rs.getString(2));
        }
        return drr;
    } finally {
        if (rs != null)
            rs.close();
        if (ps != null)
            ps.close();
        if (c != null)
            c.close();
    }
}

From source file:Main.java

public static PreparedStatement createFieldsInsert(Connection conn, int layerId, String name,
        String description, String fieldId, String fieldType, String sid, String sname, String sdesc,
        boolean indb, boolean enabled, boolean namesearch, boolean defaultlayer, boolean intersect,
        boolean layerbranch, boolean analysis, boolean addToMap) throws SQLException {
    // TOOD slightly different statement if sdesc is null...

    PreparedStatement stFieldsInsert = conn.prepareStatement(
            "INSERT INTO fields (name, id, \"desc\", type, spid, sid, sname, sdesc, indb, enabled, last_update, namesearch, defaultlayer, \"intersect\", layerbranch, analysis, addtomap)"
                    + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
    stFieldsInsert.setString(1, name);
    stFieldsInsert.setString(2, fieldId);
    stFieldsInsert.setString(3, description);
    stFieldsInsert.setString(4, fieldType);
    stFieldsInsert.setString(5, Integer.toString(layerId));
    stFieldsInsert.setString(6, sid);//from  w  w w  .  j a v  a2 s.  c om
    stFieldsInsert.setString(7, sname);

    if (sdesc == null || sdesc.isEmpty()) {
        stFieldsInsert.setNull(8, Types.VARCHAR);
    } else {
        stFieldsInsert.setString(8, sdesc);
    }

    stFieldsInsert.setBoolean(9, indb);
    stFieldsInsert.setBoolean(10, enabled);
    stFieldsInsert.setTimestamp(11, new Timestamp(System.currentTimeMillis()));
    stFieldsInsert.setBoolean(12, namesearch);
    stFieldsInsert.setBoolean(13, defaultlayer);
    stFieldsInsert.setBoolean(14, intersect);
    stFieldsInsert.setBoolean(15, layerbranch);
    stFieldsInsert.setBoolean(16, analysis);
    stFieldsInsert.setBoolean(17, addToMap);

    return stFieldsInsert;
}

From source file:com.silverpeas.notation.model.RatingDAO.java

private static void populateRatings(Connection con, Map<String, ContributionRating> ratings,
        String componentInstanceId, String contributionType, Collection<String> contributionIds)
        throws SQLException {
    PreparedStatement prepStmt = con.prepareStatement(
            QUERY_GET_RATINGS.replaceAll("@ids@", "'" + StringUtils.join(contributionIds, "','") + "'"));
    prepStmt.setString(1, componentInstanceId);
    prepStmt.setString(2, contributionType);
    ResultSet rs = null;/*from  w  ww . ja  va  2s .c  o  m*/
    try {
        rs = prepStmt.executeQuery();
        while (rs.next()) {
            RatingRow current = resultSet2RatingRow(rs);
            ContributionRating contributionRating = ratings.get(current.getContributionId());
            if (contributionRating == null) {
                contributionRating = new ContributionRating(new ContributionRatingPK(
                        current.getContributionId(), componentInstanceId, contributionType));
                ratings.put(contributionRating.getContributionId(), contributionRating);
            }
            contributionRating.addRaterRating(current.getRaterId(), current.getRating());
        }
    } finally {
        DBUtil.close(rs, prepStmt);
    }
}