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.concursive.connect.web.modules.documents.dao.FileItemList.java

public static void convertTempFiles(Connection db, int linkModuleId, int modifiedBy, int id,
        String attachmentList, boolean setDefault) throws SQLException {
    StringTokenizer items = new StringTokenizer(attachmentList, ",");
    PreparedStatement pst = db.prepareStatement("UPDATE project_files "
            + "SET link_module_id = ?, link_item_id = ? " + (setDefault ? ", default_file = ? " : "")
            + "WHERE link_module_id = ? AND item_id = ? AND enteredby = ? ");
    while (items.hasMoreTokens()) {
        int itemId = Integer.parseInt(items.nextToken().trim());
        int i = 0;
        pst.setInt(++i, linkModuleId);
        pst.setInt(++i, id);//from  ww  w .j av  a  2  s . c  o m
        if (setDefault) {
            pst.setBoolean(++i, true);
        }
        pst.setInt(++i, Constants.TEMP_FILES);
        pst.setInt(++i, itemId);
        pst.setInt(++i, modifiedBy);
        int count = pst.executeUpdate();
    }
    pst.close();
}

From source file:com.l2jfree.gameserver.model.ShortCuts.java

public synchronized void registerShortCut(L2ShortCut shortcut) {
    _shortCuts.put(shortcut.getSlot() + 12 * shortcut.getPage(), shortcut);

    Connection con = null;//  w w  w  .j  a v  a 2  s.c om
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);

        PreparedStatement statement = con.prepareStatement(
                "REPLACE INTO character_shortcuts (charId,slot,page,type,shortcut_id,level,class_index) values(?,?,?,?,?,?,?)");
        statement.setInt(1, _owner.getObjectId());
        statement.setInt(2, shortcut.getSlot());
        statement.setInt(3, shortcut.getPage());
        statement.setInt(4, shortcut.getType());
        statement.setInt(5, shortcut.getId());
        statement.setInt(6, shortcut.getLevel());
        statement.setInt(7, _owner.getClassIndex());
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.warn("", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:dhbw.clippinggorilla.objects.user.UserUtils.java

/**
 * Change the last name of the user./*from  w  w  w . j  a  va  2s  .com*/
 *
 * @param u The User whose last name should be changed
 * @param newLastName The new last name
 * @return true if change was successful, false if dberror occured or
 * requirements are not met.
 */
public static synchronized boolean changeLastName(User u, String newLastName) {
    if (checkName(newLastName).length() == 0) {
        try {
            String sql = "UPDATE " + Tables.USER + " SET " + Columns.LAST_NAME + " = ? WHERE " + Columns.ID
                    + " = ?";
            PreparedStatement statement = Database.getConnection().prepareStatement(sql);
            statement.setString(1, newLastName);
            statement.setInt(2, u.getId());
            statement.executeUpdate();
            u.setLastName(newLastName);
            return true;
        } catch (SQLException ex) {
            Log.warning("Failed to change first name!", ex);
            return false;
        }
    }
    return false;
}

From source file:dhbw.clippinggorilla.objects.user.UserUtils.java

/**
 * Changes the username of a User/*w ww  . j  av a2  s . c  om*/
 *
 * @param u The User whose Username should be changed
 * @param newUsername The new Username
 * @return true if successful, false otherwise
 */
public static synchronized boolean changeUsername(User u, String newUsername) {
    if (checkUsername(newUsername).length() == 0) {
        try {
            String sql = "UPDATE " + Tables.USER + " SET " + Columns.USERNAME + " = ? WHERE " + Columns.ID
                    + " = ?";
            PreparedStatement statement = Database.getConnection().prepareStatement(sql);
            statement.setString(1, newUsername);
            statement.setInt(2, u.getId());
            statement.executeUpdate();
            u.setUsername(newUsername);
            return true;
        } catch (SQLException ex) {
            Log.warning("Failed to change first name!", ex);
            return false;
        }
    }
    return false;
}

From source file:i5.las2peer.services.todolist.Todolist.java

/**
 * //  w  w  w  .  j a v  a 2 s  . c  o  m
 * deleteData
 * 
 * 
 * @return HttpResponse
 * 
 */
@DELETE
@Path("/data/{id}")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
@ApiResponses(value = { @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "responseDeleteData") })
@ApiOperation(value = "deleteData", notes = "")
public HttpResponse deleteData(@PathParam("id") Integer id) {
    Connection conn = null;
    try {

        conn = dbm.getConnection();
        PreparedStatement stmt = conn.prepareStatement("DELETE FROM gamificationCAE.todolist WHERE id = ?");
        stmt.setInt(1, id);
        stmt.executeUpdate();
        stmt = conn.prepareStatement("ALTER TABLE todolist AUTO_INCREMENT = 1;");
        stmt.executeUpdate();
        return new HttpResponse("data number " + id + " is deleted!", HttpURLConnection.HTTP_OK);

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

        return new HttpResponse("Database connection error", HttpURLConnection.HTTP_INTERNAL_ERROR);
    } finally {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:dhbw.clippinggorilla.objects.user.UserUtils.java

/**
 * Change the first name of the user./* w w  w  . j av  a2s.c om*/
 *
 * @param u The User whose first name should be changed
 * @param newFirstName The new first name
 * @return true if change was successful, false if dberror occured or
 * requirements are not met.
 */
public static synchronized boolean changeFirstName(User u, String newFirstName) {
    if (checkName(newFirstName).length() == 0) {
        try {
            String sql = "UPDATE " + Tables.USER + " SET " + Columns.FIRST_NAME + " = ? WHERE " + Columns.ID
                    + " = ?";
            PreparedStatement statement = Database.getConnection().prepareStatement(sql);
            statement.setString(1, newFirstName);
            statement.setInt(2, u.getId());
            statement.executeUpdate();
            u.setFirstName(newFirstName);
            return true;
        } catch (SQLException ex) {
            Log.warning("Failed to change first name!", ex);
            return false;
        }
    }
    return false;
}

From source file:com.nabla.wapp.server.json.SqlToJson.java

public FetchResult serializeSql(final AbstractFetch options, final Connection conn, final String sql,
        Object... parameters) throws SQLException {
    final PreparedStatement stmt = StatementFormat.prepare(conn, createSql(options, sql), parameters);
    try {/*  w  w w  .  j a v a  2s. c  om*/
        int i = (parameters != null) ? parameters.length : 0;
        if (options.isRange()) {
            stmt.setInt(++i, options.getEndRow() - options.getStartRow() + 1);
            stmt.setInt(++i, options.getStartRow());
        }
        return serialize(options, stmt);
    } finally {
        stmt.close();
    }
}

From source file:net.freechoice.model.orm.Map_Post.java

/**
 * for content update see Dao_Post/* ww  w . j a  v a 2  s . co m*/
 */
@Override
public PreparedStatementCreator createUpdate(final FC_Post entity) {

    return new PreparedStatementCreator() {
        @Override
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {

            PreparedStatement ps = con.prepareStatement(" update  FC_Post " + " set   status = ?,"
                    + "     id_author = ?, " + "     name_author = ?, " + "     title = ? " + " where id = ? ");
            ps.setShort(1, entity.status);
            ps.setInt(2, entity.id_author);
            ps.setString(3, entity.name_author);
            ps.setString(4, entity.title);
            ps.setInt(5, entity.id);
            return ps;
        }
    };
}

From source file:com.imagelake.control.CreditsDAOImp.java

@Override
public Credits getCreditDetails(int credit_id) {
    Credits c = new Credits();
    try {/*  w ww .  j a va 2  s .c  om*/
        String sql = "SELECT * FROM credits WHERE credit_id=?";
        Connection con = DBFactory.getConnection();
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setInt(1, credit_id);
        ResultSet rs = ps.executeQuery();
        System.gc();
        while (rs.next()) {
            c = new Credits();
            c.setCredit_id(rs.getInt(1));
            c.setCredits(rs.getInt(2));
            c.setSize(rs.getString(3));
            c.setWidth(rs.getInt(4));
            c.setHeight(rs.getInt(5));
            c.setState(rs.getInt(6));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return c;
}

From source file:dhbw.clippinggorilla.objects.user.UserUtils.java

/**
 * Creates a new User Profile and saves it to the DB
 *
 * @param u The User of the new Interestprofile
 * @param name The name of the Interestprofile
 * @param sources The selected sources//w  w w .j  a  v a  2s  .  co m
 * @param tags The selected tags
 * @param categories The selected categories
 * @return Profile, if sth. went wrong: null
 */
public static InterestProfile createNewProfile(User u, String name, Map<Source, Boolean> sources,
        Set<String> tags, Map<Category, Boolean> categories) {
    //TODO: profile validation here!
    //Valdiation whether profile exists
    if (!InterestProfileUtils.checkNameUnique(u, name)) {
        return null;
    }
    try {
        //Insert profile data to user_profile table 
        String sql = "INSERT INTO " + Tables.USER_PROFILE + " (" + Columns.USER_ID + ", " + Columns.NAME
                + ") VALUES (?, ?)";
        PreparedStatement statement = Database.getConnection().prepareStatement(sql);
        statement.setInt(1, u.getId());
        statement.setString(2, name);
        statement.executeUpdate();

        //Get profile ID from table
        sql = "SELECT " + Columns.ID + " FROM " + Tables.USER_PROFILE + " WHERE " + Columns.USER_ID
                + " = ? AND " + Columns.NAME + " = ?";
        statement = Database.getConnection().prepareStatement(sql);
        statement.setInt(1, u.getId());
        statement.setString(2, name);
        ResultSet result = statement.executeQuery();
        result.next();
        int profileId = result.getInt(Columns.ID);

        //Insert sources
        sources.forEach((lambda_source, bool) -> {
            if (bool == true) {
                String sourceId = lambda_source.getId();
                try {
                    String lambda_sql = "INSERT INTO " + Tables.USER_PROFILE_SOURCE + " (" + Columns.PROFILE_ID
                            + ", " + Columns.SOURCE + ") VALUES (?, ?)";
                    PreparedStatement lambda_statement = Database.getConnection().prepareStatement(lambda_sql);
                    lambda_statement.setInt(1, profileId);
                    lambda_statement.setString(2, sourceId);
                    lambda_statement.executeUpdate();
                } catch (SQLException ex) {
                    Log.warning("Profile source insertion failed", ex);
                }
            }
        });

        //Insert Tags
        tags.forEach((lambda_tags) -> {
            try {
                String lambda_sql = "INSERT INTO " + Tables.USER_PROFILE_TAG + " (" + Columns.PROFILE_ID + ", "
                        + Columns.TAG + ") VALUES (?, ?)";
                PreparedStatement lambda_statement = Database.getConnection().prepareStatement(lambda_sql);
                lambda_statement.setInt(1, profileId);
                lambda_statement.setString(2, lambda_tags);
                lambda_statement.executeUpdate();
            } catch (SQLException ex) {
                Log.warning("Profile source insertion failed", ex);
            }
        });

        //Insert categories
        categories.forEach((category, bool) -> {
            if (bool == true) {
                String categoryId = category.getId();
                try {
                    String lambda_sql = "INSERT INTO " + Tables.USER_PROFILE_CATEGORY + " ("
                            + Columns.PROFILE_ID + ", " + Columns.CATEGORY + ") VALUES (?, ?)";
                    PreparedStatement lambda_statement = Database.getConnection().prepareStatement(lambda_sql);
                    lambda_statement.setInt(1, profileId);
                    lambda_statement.setString(2, categoryId);
                    lambda_statement.executeUpdate();
                } catch (SQLException ex) {
                    Log.warning("Profile source insertion failed", ex);
                }
            }
        });
        return InterestProfileUtils.getInterestProfile(profileId);
    } catch (SQLException ex) {
        Log.warning("Insertion of new profile failed", ex);
        return null;
    }
}