Example usage for java.sql PreparedStatement close

List of usage examples for java.sql PreparedStatement close

Introduction

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

Prototype

void close() throws SQLException;

Source Link

Document

Releases this Statement object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.

Usage

From source file:at.becast.youploader.account.Account.java

public static Account read(int id) throws IOException {
    PreparedStatement stmt;
    try {/* ww  w. j  a v  a2  s.  c  om*/
        stmt = c.prepareStatement("SELECT * FROM `accounts` WHERE `id`=? LIMIT 1");
        stmt.setInt(1, id);
        ResultSet rs = stmt.executeQuery();
        ObjectMapper mapper = new ObjectMapper();
        List<Cookie> c = mapper.readValue(rs.getString("cookie"), new TypeReference<List<Cookie>>() {
        });
        String name = rs.getString("name");
        String token = rs.getString("refresh_token");
        stmt.close();
        rs.close();
        return new Account(id, name, token, c);
    } catch (SQLException e) {
        LOG.error("Account read error!", e);
        return null;
    }
}

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

public static void updateDateRepositoryImport(Connection con, String date) throws SQLException {
    PreparedStatement pStmt = con.prepareStatement(
            "delete from PM_DP_CONFIG where CLE = ? " + "insert into PM_DP_CONFIG (CLE, VALEUR) values (?, ?)");
    try {/*from  ww w  .  j a v  a  2 s  .  co m*/
        pStmt.setString(1, DataProcessConstants.KEY_DATE_LAST_IMPORT_REPOSITORY);
        pStmt.setString(2, DataProcessConstants.KEY_DATE_LAST_IMPORT_REPOSITORY);
        pStmt.setString(3, date);
        pStmt.executeUpdate();
    } finally {
        pStmt.close();
    }
}

From source file:at.becast.youploader.database.SQLite.java

public static void setPlaylistHidden(int id, String hidden) throws SQLException {
    PreparedStatement prest = null;
    String sql = "UPDATE `playlists` SET `shown`=? WHERE `id`=?";
    prest = c.prepareStatement(sql);//from   ww  w  .j a  v  a  2 s.c om
    prest.setString(1, hidden);
    prest.setInt(2, id);
    prest.execute();
    prest.close();
}

From source file:at.becast.youploader.database.SQLite.java

public static Boolean deleteTemplate(int id) {
    PreparedStatement prest = null;
    String sql = "DELETE FROM `templates` WHERE `id`=?";
    try {// w  ww.j av a 2s  .c o m
        prest = c.prepareStatement(sql);
        prest.setInt(1, id);
        boolean res = prest.execute();
        prest.close();
        return res;
    } catch (SQLException e) {
        LOG.error("Error deleting Template ", e);
        return false;
    }
}

From source file:de.iteratec.iteraplan.db.SqlScriptExecutor.java

public static void executeSqlStatements(final List<String> sqlStrings, Connection connection)
        throws SQLException {
    for (String statement : sqlStrings) {
        PreparedStatement stmt = null;
        try {//from   w  w  w.j a  v a 2 s . com
            stmt = connection.prepareStatement(statement);
            stmt.executeUpdate();
        } catch (SQLException se) {
            // ignore alter table errors because these might be ok, if tables do not exist
            if (!statement.trim().startsWith("alter table")) {
                LOGGER.error("database error when running db statement  '" + statement + "'.");
                throw se;
            }
        } finally {
            if (stmt != null) {
                stmt.close();
            }
        }
    }

    connection.commit();
}

From source file:at.becast.youploader.database.SQLite.java

public static Boolean deleteUpload(int upload_id) {
    PreparedStatement prest = null;
    String sql = "DELETE FROM `uploads` WHERE `id`=?";
    try {// w ww . jav  a2 s . co m
        prest = c.prepareStatement(sql);
        prest.setInt(1, upload_id);
        boolean res = prest.execute();
        prest.close();
        return res;
    } catch (SQLException e) {
        LOG.error("Error deleting upload", e);
        return false;
    }
}

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

/**
 * Return the list of device identifiers associated with this account.
 * @param userinfo//from   w  ww .j a  v  a  2s .c  o m
 * @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:net.sf.l2j.gameserver.model.entity.L2JOneoRusEvents.DM.java

public static void saveData() {
    java.sql.Connection con = null;
    try {//from w ww  .ja v a 2 s.  c  o  m
        con = L2DatabaseFactory.getInstance().getConnection();
        PreparedStatement statement;
        statement = con.prepareStatement("Delete from dm");
        statement.execute();
        statement.close();
        statement = con.prepareStatement(
                "INSERT INTO dm (eventName, eventDesc, joiningLocation, minlvl, maxlvl, npcId, npcX, npcY, npcZ, rewardId, rewardAmount, color, playerX, playerY, playerZ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        statement.setString(1, _eventName);
        statement.setString(2, _eventDesc);
        statement.setString(3, _joiningLocationName);
        statement.setInt(4, _minlvl);
        statement.setInt(5, _maxlvl);
        statement.setInt(6, _npcId);
        statement.setInt(7, _npcX);
        statement.setInt(8, _npcY);
        statement.setInt(9, _npcZ);
        statement.setInt(10, _rewardId);
        statement.setInt(11, _rewardAmount);
        statement.setInt(12, _playerColors);
        statement.setInt(13, _playerX);
        statement.setInt(14, _playerY);
        statement.setInt(15, _playerZ);
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.error("Exception: DM.saveData(): " + e.getMessage());
    } finally {
        try {
            con.close();
        } catch (Exception e) {
        }
    }
}

From source file:com.nabla.wapp.server.database.Database.java

public static Integer addRecord(final Connection conn, final String sql, final Object... parameters)
        throws SQLException {
    final PreparedStatement stmt = StatementFormat.prepare(conn, Statement.RETURN_GENERATED_KEYS, sql,
            parameters);// www  . j  a v  a 2s.  c o  m
    try {
        if (stmt.executeUpdate() != 1) {
            if (log.isErrorEnabled())
                log.error("failed to add record");
            return null;
        }
        final ResultSet rsKey = stmt.getGeneratedKeys();
        try {
            rsKey.next();
            return rsKey.getInt(1);
        } finally {
            rsKey.close();
        }
    } finally {
        stmt.close();
    }
}

From source file:genericepayadmin.AddIpBean.java

public static int getPages(Connection con) throws Exception {
    int totalcount = 0;
    PreparedStatement ps = null;
    ResultSet rs = null;// ww w .ja va 2s  .c o m
    try {
        String sql = "select ceil(count(*)/10) as totalpage from webservice_validator";
        ps = con.prepareStatement(sql);
        rs = ps.executeQuery();
        if (rs.next()) {
            totalcount = rs.getInt("totalpage");
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    } finally {
        try {
            if (ps != null)
                ps.close();
            if (rs != null)
                rs.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
    return totalcount;
}