Example usage for java.sql PreparedStatement executeUpdate

List of usage examples for java.sql PreparedStatement executeUpdate

Introduction

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

Prototype

int executeUpdate() throws SQLException;

Source Link

Document

Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.

Usage

From source file:com.l2jfree.sql.L2DatabaseInstaller.java

private static void insertRevision(double revision) {
    System.out.println("Saving revision '" + revision + "'.");

    Connection con = null;/*from  w w w. j  a  v  a2s  .  com*/
    try {
        con = L2Database.getConnection();

        PreparedStatement ps = con.prepareStatement("INSERT INTO _revision VALUES (?,?)");
        ps.setDouble(1, revision);
        ps.setLong(2, System.currentTimeMillis());
        ps.executeUpdate();
        ps.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        L2Database.close(con);
    }

    System.out.println("Done.");
}

From source file:com.l2jfree.gameserver.model.entity.faction.FactionQuest.java

public static void deleteFactionQuest(L2Player player, int factionQuestId) {
    Connection con = null;/*  ww  w. j a v a  2  s  . c  o m*/
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement;
        statement = con.prepareStatement(
                "DELETE FROM character_faction_quests WHERE char_id=? AND faction_quest_id=?");
        statement.setInt(1, player.getObjectId());
        statement.setInt(2, factionQuestId);
        statement.executeUpdate();
        statement.close();
    } catch (Exception e) {
        _log.warn("could not delete char faction quest:", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:com.l2jfree.gameserver.model.entity.faction.FactionQuest.java

public static void createFactionQuest(L2Player player, int factionQuestId) {
    Connection con = null;/*  w ww  .j a  va 2s .  c  om*/
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement;
        statement = con.prepareStatement(
                "INSERT INTO character_faction_quests (char_id,faction_quest_id) VALUES (?,?)");
        statement.setInt(1, player.getObjectId());
        statement.setInt(2, factionQuestId);
        statement.executeUpdate();
        statement.close();
    } catch (Exception e) {
        _log.warn("could not insert char faction quest:", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:com.l2jfree.gameserver.util.IdFactory.java

private static void removeExpired() {
    int removed = 0;

    Connection con = null;//w ww  .j a va  2 s. co m
    try {
        con = L2Database.getConnection();

        for (String query : REMOVE_EXPIRED_QUERIES) {
            final PreparedStatement ps = con.prepareStatement(query);
            ps.setLong(1, System.currentTimeMillis());

            removed += ps.executeUpdate();

            ps.close();
        }
    } catch (SQLException e) {
        _log.warn("", e);
    } finally {
        L2Database.close(con);
    }

    _log.info("IdFactory: Removed " + removed + " expired entries from database.");
}

From source file:las.DBConnector.java

/**
 * Create Table with(example): tableName: ITEMS  <All in capital letters>
 * details: ITEM_ID,NAME,NUMBER_AVAILABLE
 * <All in capital letters and put a dash instead of space>
 *///from w  w  w  .j  av  a2 s.c o m
public static void createTable(String tableName, String details) throws SQLException {
    DatabaseMetaData dbmd = conn.getMetaData();
    ResultSet rs = dbmd.getTables(null, "LAS", tableName, null);

    if (!rs.next()) {
        String data = "CREATE TABLE " + tableName + "(" + details + ")";
        PreparedStatement pt = conn.prepareStatement(data);
        pt.executeUpdate();
        System.out.println(tableName + " has been created");
    } else {
        System.out.println(tableName + " already exists in LAS Database");
    }
}

From source file:net.codjo.dataprocess.server.treatmenthelper.TreatmentHelper.java

public static void deleteRepository(Connection con, int repositoryId) throws SQLException {
    String sql = "delete PM_REPOSITORY_CONTENT where REPOSITORY_ID = ? "
            + " delete PM_REPOSITORY where REPOSITORY_ID = ?";
    PreparedStatement pStmt = con.prepareStatement(sql);
    try {// w w  w  .  j  ava2 s  .  c  o m
        pStmt.setInt(1, repositoryId);
        pStmt.setInt(2, repositoryId);
        pStmt.executeUpdate();
    } finally {
        pStmt.close();
    }
}

From source file:dataMappers.PictureDataMapper.java

public static void addPictureToReport(DBConnector dbconnector, HttpServletRequest request)
        throws FileUploadException, IOException, SQLException {

    if (!ServletFileUpload.isMultipartContent(request)) {
        System.out.println("Invalid upload request");
        return;//from  ww w . ja  va  2  s.  c  o m
    }

    // Define limits for disk item
    DiskFileItemFactory factory = new DiskFileItemFactory();
    factory.setSizeThreshold(THRESHOLD_SIZE);

    // Define limit for servlet upload
    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setFileSizeMax(MAX_FILE_SIZE);
    upload.setSizeMax(MAX_REQUEST_SIZE);

    FileItem itemFile = null;
    int reportID = 0;

    // Get list of items in request (parameters, files etc.)
    List formItems = upload.parseRequest(request);
    Iterator iter = formItems.iterator();

    // Loop items
    while (iter.hasNext()) {
        FileItem item = (FileItem) iter.next();

        if (!item.isFormField()) {
            itemFile = item; // If not form field, must be item
        } else if (item.getFieldName().equalsIgnoreCase("reportID")) { // else it is a form field
            try {
                System.out.println(item.getString());
                reportID = Integer.parseInt(item.getString());
            } catch (NumberFormatException e) {
                reportID = 0;
            }
        }
    }

    // This will be null if no fields were declared as image/upload.
    // Also, reportID must be > 0
    if (itemFile != null || reportID == 0) {

        try {

            // Create credentials from final vars
            BasicAWSCredentials awsCredentials = new BasicAWSCredentials(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY);

            // Create client with credentials
            AmazonS3 s3client = new AmazonS3Client(awsCredentials);
            // Set region
            s3client.setRegion(Region.getRegion(Regions.EU_WEST_1));

            // Set content length (size) of file
            ObjectMetadata om = new ObjectMetadata();
            om.setContentLength(itemFile.getSize());

            // Get extension for file
            String ext = FilenameUtils.getExtension(itemFile.getName());
            // Generate random filename
            String keyName = UUID.randomUUID().toString() + '.' + ext;

            // This is the actual upload command
            s3client.putObject(new PutObjectRequest(S3_BUCKET_NAME, keyName, itemFile.getInputStream(), om));

            // Picture was uploaded to S3 if we made it this far. Now we insert the row into the database for the report.
            PreparedStatement stmt = dbconnector.getCon()
                    .prepareStatement("INSERT INTO reports_pictures" + "(REPORTID, PICTURE) VALUES (?,?)");

            stmt.setInt(1, reportID);
            stmt.setString(2, keyName);

            stmt.executeUpdate();

            stmt.close();

        } catch (AmazonServiceException ase) {

            System.out.println("Caught an AmazonServiceException, which " + "means your request made it "
                    + "to Amazon S3, but was rejected with an error response" + " for some reason.");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());

        } catch (AmazonClientException ace) {

            System.out.println("Caught an AmazonClientException, which " + "means the client encountered "
                    + "an internal error while trying to " + "communicate with S3, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message: " + ace.getMessage());

        }

    }
}

From source file:com.webbfontaine.valuewebb.gtns.TTGTNSSynchronizer.java

private static void updateTTFlag(TtGen ttGen) {
    Connection conn = null;//from ww w . j a  v  a 2s . co m
    PreparedStatement ps = null;
    try {
        conn = Utils.getSQLConnection();
        conn.setAutoCommit(false);
        ps = conn.prepareStatement(UPDATE_TT_SQL);
        ps.setLong(1, ttGen.getId());
        ps.executeUpdate();
        conn.commit();
    } catch (SQLException e) {
        LOGGER.error("Error in updating GCNet Submission Flag for TT with id {0}, error {1}", ttGen.getId(), e);
    } finally {
        DBUtils.closeResource(ps);
        DBUtils.closeResource(conn);
    }
}

From source file:com.sql.EMail.java

/**
 * Marks an email ready to file by the system. This is in place so a user
 * does not try to docket an email that is currently being processed.
 *
 * @param eml/*  w  w w. j av a2  s . co  m*/
 */
public static void setEmailReadyToFile(EmailMessageModel eml) {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = DBConnection.connectToDB();
        String sql = "UPDATE EMail SET readyToFile = ?, emailBodyFileName = ? WHERE id = ?";
        ps = conn.prepareStatement(sql);
        ps.setInt(1, eml.getReadyToFile());
        ps.setString(2, eml.getEmailBodyFileName());
        ps.setInt(3, eml.getId());
        ps.executeUpdate();
    } catch (SQLException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        DbUtils.closeQuietly(ps);
        DbUtils.closeQuietly(conn);
    }
}

From source file:com.sql.SECExceptions.java

/**
 * Removes old exception based off of a global exception date timeframe
 *///from   w w  w .j  a  va2  s  .  c om
public static void removeOldExceptions() {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = DBConnection.connectToDB();
        String sql = "DELETE FROM SECExceptions WHERE " + "timeOccurred < dateadd("
                + Global.getExceptionTimeFrame() + ",-" + Global.getExceptionTimeAmount() + ", getdate())";
        ps = conn.prepareStatement(sql);
        ps.executeUpdate();
    } catch (SQLException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
    }
}