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:com.weibo.datasys.common.db.DBManager.java

/**
 * //from   w  w  w  . j ava 2 s  .  c o m
 * ??
 * 
 * @param conn
 * @param ps
 */
public static void releaseConnection(Connection conn, PreparedStatement ps) {
    if (ps != null) {
        try {
            ps.close();
        } catch (Exception e) {
            logger.error("[ClosePSError] - ", e);
        }
    }
    if (conn != null) {
        try {
            if (!conn.isClosed())
                conn.close();
        } catch (Exception e) {
            logger.error("[CloseConnectionError] - ", e);
        }
    }
}

From source file:at.molindo.dbcopy.util.Utils.java

public static <T> T executePrepared(Connection c, String query, ResultSetHandler<T> handler, Object... params)
        throws SQLException {
    PreparedStatement stmt = c.prepareStatement(query);
    try {//from   w  ww . j a  v a  2 s .c  o  m
        for (int i = 0; i < params.length; i++) {
            stmt.setObject(i + 1, params[i]);
        }
        ResultSet rs = stmt.executeQuery();
        return handle(rs, handler);
    } finally {
        stmt.close();
    }
}

From source file:com.example.querybuilder.server.Jdbc.java

public static void close(PreparedStatement preparedStatement) {
    try {/*from www . jav  a  2 s.  c  om*/
        preparedStatement.close();
    } catch (SQLException e) {
        throw new SqlRuntimeException(e);
    }
}

From source file:$.DeviceTypeUtils.java

public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();/*ww  w.  j  av  a 2 s. c o  m*/
            } catch (SQLException e) {
                log.warn("Error occurred while closing result set", e);
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                log.warn("Error occurred while closing prepared statement", e);
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                log.warn("Error occurred while closing database connection", e);
            }
        }
    }

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  av  a 2  s.  c o m*/
    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:Main.java

public static byte[] getBLOB(int id, Connection conn) throws Exception {
    ResultSet rs = null;//from  w  ww. jav  a 2  s.co  m
    PreparedStatement pstmt = null;
    String query = "SELECT photo FROM MyPictures WHERE id = ?";
    try {
        pstmt = conn.prepareStatement(query);
        pstmt.setInt(1, id);
        rs = pstmt.executeQuery();
        rs.next();
        Blob blob = rs.getBlob(3);
        // materialize BLOB onto client
        return blob.getBytes(1, (int) blob.length());
    } finally {
        rs.close();
        pstmt.close();
        conn.close();
    }
}

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

public static boolean processRequest(Login.UserInfo userinfo, JSONObject requestParams)
        throws ClassNotFoundException, SQLException, IOException {
    String newPassword = requestParams.getString("password");
    String requestToken = requestParams.getString("token");

    /*//from  w  ww . j a  v a 2 s. c  om
     * Determine if the token matches for this user record. We are in the
     * unique situation of having a logged in user, but he doesn't know
     * his password. We also ignore any requests with an expired
     * token.
     */

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

    try {
        /*
         * Delete old requests
         */
        c = Database.get();
        ps = c.prepareStatement("DELETE FROM forgotpassword WHERE expires < LOCALTIMESTAMP");
        ps.execute();

        ps.close();
        ps = null;

        /*
         * Verify the token we passed back was correct
         */
        ps = c.prepareStatement(
                "SELECT token " + "FROM forgotpassword " + "WHERE userid = ? " + "AND token = ?");
        ps.setInt(1, userinfo.getUserID());
        ps.setString(2, requestToken);
        rs = ps.executeQuery();
        if (!rs.next())
            return false; // token does not exist or expired.

        rs.close();
        rs = null;
        ps.close();
        ps = null;

        /*
         * Step 2: Modify the password.
         */

        ps = c.prepareStatement("UPDATE Users SET password = ? WHERE userid = ?");
        ps.setString(1, newPassword);
        ps.setInt(2, userinfo.getUserID());
        ps.execute();

        return true;
    } finally {
        if (rs != null)
            rs.close();
        if (ps != null)
            ps.close();
        if (c != null)
            c.close();
    }
}

From source file:Main.java

public static byte[] getBLOB(int id, Connection conn) throws Exception {
    ResultSet rs = null;//from w  ww  . ja  v a2s.  c  o  m
    PreparedStatement pstmt = null;
    String query = "SELECT photo FROM MyPictures WHERE id = ?";
    try {
        pstmt = conn.prepareStatement(query);
        pstmt.setInt(1, id);
        rs = pstmt.executeQuery();
        rs.next();
        Blob blob = rs.getBlob("photo");
        // materialize BLOB onto client
        return blob.getBytes(1, (int) blob.length());
    } finally {
        rs.close();
        pstmt.close();
        conn.close();
    }
}

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

public static void deleteFactionQuest(L2Player player, int factionQuestId) {
    Connection con = null;/*from w ww .  ja va 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;//from w w w  . j a  v a  2  s .c  o m
    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);
    }
}