Example usage for java.sql PreparedStatement setInt

List of usage examples for java.sql PreparedStatement setInt

Introduction

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

Prototype

void setInt(int parameterIndex, int x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java int value.

Usage

From source file:com.zimbra.cs.mailbox.util.MetadataDump.java

private static int getMailboxGroup(DbConnection conn, int mboxId) throws SQLException {
    int gid = 0;/*from  www  . j a v a2 s .co  m*/
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.prepareStatement("SELECT group_id FROM mailbox WHERE id = ?");
        stmt.setInt(1, mboxId);
        rs = stmt.executeQuery();
        if (rs.next())
            gid = rs.getInt(1);
    } finally {
        if (rs != null)
            rs.close();
        if (stmt != null)
            stmt.close();
    }
    return gid;
}

From source file:com.l2jserver.model.template.NPCTemplateConverter.java

private static Skills fillSkillList(final ObjectFactory factory, ResultSet npcRs, int npcId)
        throws SQLException {
    final Connection conn = npcRs.getStatement().getConnection();
    final Skills skills = factory.createNPCTemplateSkills();

    final PreparedStatement st = conn.prepareStatement("SELECT * FROM npcskills WHERE npcid = ?");
    st.setInt(1, npcId);
    st.execute();/*w  w w . ja va2 s .c  o  m*/
    final ResultSet rs = st.getResultSet();
    while (rs.next()) {
        Skills.Skill s = factory.createNPCTemplateSkillsSkill();
        s.setId(new SkillTemplateID(rs.getInt("skillid"), null));
        s.setLevel(rs.getInt("level"));
        skills.getSkill().add(s);
    }
    if (skills.getSkill().size() == 0)
        return null;
    return skills;
}

From source file:com.l2jserver.model.template.NPCTemplateConverter.java

private static Droplist fillDropList(final ObjectFactory factory, ResultSet npcRs, int npcId)
        throws SQLException {
    final Connection conn = npcRs.getStatement().getConnection();
    final Droplist drops = factory.createNPCTemplateDroplist();

    final PreparedStatement st = conn.prepareStatement("SELECT * FROM droplist WHERE mobId = ?");
    st.setInt(1, npcId);
    st.execute();/*w  w  w  . j  a  v a2s  .com*/
    final ResultSet rs = st.getResultSet();
    while (rs.next()) {
        final Droplist.Item item = factory.createNPCTemplateDroplistItem();
        item.setId(new ItemTemplateID(rs.getInt("itemId"), null));
        item.setMin(rs.getInt("min"));
        item.setMax(rs.getInt("max"));
        item.setChance(rs.getInt("chance"));
        item.setCategory(getCategory(rs.getInt("category")));
        drops.getItem().add(item);
    }
    if (drops.getItem().size() == 0)
        return null;
    return drops;
}

From source file:las.DBConnector.java

public static void insertItemIntoTable(Item item) throws SQLException {
    String data = "INSERT INTO Items(title, author, type, amountleft)" + "Values (?,?,?,?)";
    PreparedStatement pt = conn.prepareStatement(data);
    pt.setString(1, item.getTitle());/*  ww  w.j a v a2s .c om*/
    pt.setString(2, item.getAuthor());
    pt.setString(3, item.getType());
    pt.setInt(4, item.getAmountLeft());
    pt.executeUpdate();
}

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

private static void insertClobData(Connection con, String line) throws DDLException {
    /*/*from w ww  . ja  v  a  2  s.  c o m*/
    "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:com.chaosinmotion.securechat.server.commands.GetMessages.java

public static ReturnResult processRequest(Login.UserInfo userinfo, JSONObject requestParams)
        throws ClassNotFoundException, SQLException, IOException {
    String deviceid = requestParams.optString("deviceid");
    MessageReturnResult mrr = new MessageReturnResult();

    /*//from  w ww  .j a  v a  2  s.com
     * Save message to the database.
     */

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

    try {
        /*
         * Get the device ID for this device. Verify it belongs to the
         * user specified
         */
        c = Database.get();
        ps = c.prepareStatement("SELECT deviceid " + "FROM Devices " + "WHERE deviceuuid = ? AND userid = ?");
        ps.setString(1, deviceid);
        ps.setInt(2, userinfo.getUserID());
        rs = ps.executeQuery();

        int deviceID = 0;
        if (rs.next()) {
            deviceID = rs.getInt(1);
        }

        rs.close();
        ps.close();
        if (deviceID == 0) {
            return new ReturnResult(Errors.ERROR_UNKNOWNDEVICE, "Unknown device");
        }

        /*
         * Run query to get messages
         */

        ps = c.prepareStatement("SELECT Messages.messageid, " + "    Messages.senderid, "
                + "    Users.username, " + "    Messages.toflag, " + "    Messages.received, "
                + "    Messages.message " + "FROM Messages, Users " + "WHERE Messages.deviceid = ? "
                + "  AND Messages.senderid = Users.userid");
        ps.setInt(1, deviceID);

        rs = ps.executeQuery();
        while (rs.next()) {
            int messageID = rs.getInt(1);
            int senderID = rs.getInt(2);
            String senderName = rs.getString(3);
            boolean toflag = rs.getBoolean(4);
            Timestamp received = rs.getTimestamp(5);
            byte[] message = rs.getBytes(6);

            mrr.addMessage(messageID, senderID, senderName, toflag, received, message);
        }

        /*
         * Return messages
         */
        return mrr;
    } finally {
        if (rs != null)
            rs.close();
        if (ps != null)
            ps.close();
        if (c != null)
            c.close();
    }
}

From source file:com.wso2telco.dep.reportingservice.dao.ApiManagerDAO.java

/**
 * Gets the consumer key by application.
 *
 * @param applicationId the application id
 * @return the consumer key by application
 * @throws APIMgtUsageQueryServiceClientException the API mgt usage query service client exception
 * @throws SQLException the SQL exception
 *//*  w w  w  .j  a  v a2 s.  co  m*/
public static String getConsumerKeyByApplication(int applicationId)
        throws APIMgtUsageQueryServiceClientException, SQLException {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet results = null;
    String sql = "SELECT CONSUMER_KEY FROM " + ReportingTable.AM_APPLICATION_KEY_MAPPING
            + " WHERE KEY_TYPE = 'PRODUCTION' AND APPLICATION_ID=?";
    String consumerKey = null;

    try {
        conn = DbUtils.getDbConnection(DataSourceNames.WSO2AM_DB);
        ps = conn.prepareStatement(sql);
        ps.setInt(1, applicationId);
        log.debug("getConsumerKeyByApplication");
        results = ps.executeQuery();
        while (results.next()) {
            consumerKey = results.getString("CONSUMER_KEY");
        }
    } catch (Exception e) {
        log.error("Error occured while getting consumer key from the database" + e);
    } finally {
        DbUtils.closeAllConnections(ps, conn, results);
    }

    return consumerKey;
}

From source file:ca.qc.adinfo.rouge.achievement.db.AchievementDb.java

public static boolean createAchievement(DBManager dbManager, String key, String name, int pointValue,
        double total) {

    Connection connection = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;/*  w  w w.  jav a  2  s  .co  m*/
    String sql = null;

    sql = "INSERT INTO rouge_achievements (`key`, `name`, `point_value`, `total`) " + " VALUES (?, ?, ?, ?);";

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

        stmt.setString(1, key);
        stmt.setString(2, name);
        stmt.setInt(3, pointValue);
        stmt.setDouble(4, total);

        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:module.entities.NameFinder.DB.java

public static void saveClusterAnnotatedDocuments(TreeMap<Integer, String> docs) throws SQLException {
    String insertSQL = "INSERT INTO assignments_herc (annotator_id,text_id,json_out) VALUES (-1,?,?)";
    PreparedStatement stmt = connection.prepareStatement(insertSQL);
    for (Map.Entry<Integer, String> pair : docs.entrySet()) {
        stmt.setInt(1, pair.getKey());
        stmt.setString(2, pair.getValue());
        stmt.addBatch();// w w  w.java 2 s. co m
    }
    stmt.executeBatch();
}

From source file:de.is24.infrastructure.gridfs.http.metadata.generation.PrimaryDbGenerator.java

private static int fillStatementForYumPackageFormatEntry(final PreparedStatement ps,
        final YumPackageFormatEntry dependency, int counter) throws SQLException {
    ps.setString(counter++, dependency.getName());
    if (dependency.getFlags() == null) {
        ps.setString(counter++, null);/*from w  w  w . ja  va 2s.c o m*/
        ps.setString(counter++, null);
        ps.setString(counter++, null);
        ps.setString(counter++, null);
    } else {
        ps.setString(counter++, dependency.getFlags());
        ps.setInt(counter++, dependency.getVersion().getEpoch());
        ps.setString(counter++, nullIfBlank(dependency.getVersion().getVer()));
        ps.setString(counter++, nullIfBlank(dependency.getVersion().getRel()));
    }
    return counter;
}