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.l2jfree.gameserver.model.entity.faction.FactionMember.java

public void quitFaction() {
    Connection con = null;/*  ww  w . ja  va 2 s  .com*/
    _factionId = 0;
    _factionPoints = 0;
    _contributions = 0;
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement;

        statement = con.prepareStatement("DELETE FROM faction_members WHERE player_id=?");
        statement.setInt(1, _playerId);
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.warn("Exception: FactionMember.quitFaction(): " + e.getMessage(), e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:hnu.helper.DataBaseConnection.java

/**
 * Create and Execute an SQL PreparedStatement and returns true if
 * everything went ok. Can be used for DML and DDL.
 * Borrows from apache.commons.scaffold.sql.StatementUtils (see
 * jakarta-commons-sandbox/scaffold)/*from  www. ja  v  a 2s  .  c o m*/
 */
public static boolean execute(String sql, Object[] parameters) {
    DataBaseConnection db = new DataBaseConnection();
    boolean returnValue = false;
    Connection conn = db.getDBConnection();
    PreparedStatement pStmt = null;
    Statement stmt = null;

    log.debug("About to execute: " + sql);
    try {
        if (parameters == null || (parameters.length == 0)) {
            stmt = conn.createStatement();
            stmt.executeUpdate(sql);
        } else {
            pStmt = conn.prepareStatement(sql);
            for (int i = 0; i < parameters.length; i++) {
                log.debug("parameter " + i + ": " + parameters[i]);
                pStmt.setObject(i + 1, parameters[i]);
            }
            pStmt.executeUpdate();
        }
        log.debug(".. executed without exception (hope to return 'true') ");
        returnValue = true;
    } catch (SQLException ex) {
        log.error("Error executing: '" + sql + "'", ex);
        returnValue = false;
    } finally {
        try {
            if (stmt != null) {
                stmt.close();
            }
            if (pStmt != null) {
                pStmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (Exception ex) {
            log.error("Couldn't close the statement or connection.", ex);
        }
    }
    return returnValue;
}

From source file:com.l2jfree.gameserver.communitybbs.bb.Topic.java

/**
 *
 *//*w ww  .  j  a va 2  s.  co m*/
public void deleteme(Forum f) {
    TopicBBSManager.getInstance().delTopic(this);
    f.rmTopicByID(getID());
    Connection con = null;
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement = con
                .prepareStatement("DELETE FROM topic WHERE topic_id=? AND topic_forum_id=?");
        statement.setInt(1, getID());
        statement.setInt(2, f.getID());
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.error(e.getMessage(), e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:fr.gael.dhus.server.http.webapp.symmetricDS.SymmetricDSWebapp.java

@Override
public void afterPropertiesSet() throws Exception {
    if (!scalabilityManager.getClearDB()) {
        return;/*  w  ww  . j a  v a  2s  .c o m*/
    }
    PreparedStatement ps = datasource.getConnection().prepareStatement(
            "SELECT TRIGGER_NAME FROM INFORMATION_SCHEMA.TRIGGERS WHERE TRIGGER_NAME LIKE 'SYM_%';",
            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

    ResultSet rs = ps.executeQuery();
    while (rs.next()) {
        PreparedStatement ps2 = datasource.getConnection()
                .prepareStatement("DROP TRIGGER " + rs.getString("TRIGGER_NAME"));
        ps2.execute();
        ps2.close();
    }
    ps.close();

    ps = datasource.getConnection().prepareStatement(
            "SELECT CONSTRAINT_NAME, TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_NAME LIKE 'SYM_%';",
            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    rs = ps.executeQuery();
    while (rs.next()) {
        PreparedStatement ps2 = datasource.getConnection().prepareStatement("ALTER TABLE "
                + rs.getString("TABLE_NAME") + " DROP CONSTRAINT " + rs.getString("CONSTRAINT_NAME"));
        ps2.execute();
        ps2.close();
    }
    ps.close();

    ps = datasource.getConnection().prepareStatement(
            "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'SYM_%';",
            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

    rs = ps.executeQuery();
    while (rs.next()) {
        PreparedStatement ps2 = datasource.getConnection()
                .prepareStatement("DROP TABLE " + rs.getString("TABLE_NAME"));
        ps2.execute();
        ps2.close();
    }
    ps.close();
}

From source file:com.taobao.tddl.jdbc.group.integration.CRUDTest.java

    () throws Exception {
   TGroupDataSource ds = new TGroupDataSource();
   DataSourceWrapper dsw = new DataSourceWrapper("db1", "rw", DataSourceFactory.getMySQLDataSource(), DBType.MYSQL);
   ds.init(dsw);/* w  w  w.j  a  v  a2s.  c  o  m*/
      
   Connection conn = ds.getConnection();

   //Statementcrud
   Statement stmt = conn.createStatement();
   assertEquals(stmt.executeUpdate("insert into crud(f1,f2) values(10,'str')"), 1);
   assertEquals(stmt.executeUpdate("update crud set f2='str2'"), 1);
   ResultSet rs = stmt.executeQuery("select f1,f2 from crud");
   rs.next();
   assertEquals(rs.getInt(1), 10);
   assertEquals(rs.getString(2), "str2");
   assertEquals(stmt.executeUpdate("delete from crud"), 1);
   rs.close();
   stmt.close();

   //PreparedStatementcrud
   String sql = "insert into crud(f1,f2) values(?,?)";
   PreparedStatement ps = conn.prepareStatement(sql);
   ps.setInt(1, 10);
   ps.setString(2, "str");
   assertEquals(ps.executeUpdate(), 1);
   ps.close();

   sql = "update crud set f2=?";
   ps = conn.prepareStatement(sql);
   ps.setString(1, "str2");
   assertEquals(ps.executeUpdate(), 1);
   ps.close();

   sql = "select f1,f2 from crud";
   ps = conn.prepareStatement(sql);
   rs = ps.executeQuery();
   rs.next();
   assertEquals(rs.getInt(1), 10);
   assertEquals(rs.getString(2), "str2");
   rs.close();
   ps.close();

   sql = "delete from crud";
   ps = conn.prepareStatement(sql);
   assertEquals(ps.executeUpdate(), 1);
   ps.close();

   conn.close();
}

From source file:InsertClobToMySqlServlet.java

public void insertCLOB(Connection conn, String id, String name, String fileContent) throws Exception {
    PreparedStatement pstmt = null;
    try {/*from  ww w. j  a  va 2s. c  o  m*/
        pstmt = conn.prepareStatement("insert into datafiles(id, filename, filebody) values (?, ?, ?)");
        pstmt.setString(1, id);
        pstmt.setString(2, name);
        pstmt.setString(3, fileContent);
        pstmt.executeUpdate();
    } finally {
        pstmt.close();
    }
}

From source file:oobbit.orm.LinkConnections.java

public void delete(int sourceId, int destinationId) throws SQLException {
    PreparedStatement statement = getConnection().prepareStatement(
            "DELETE FROM `oobbit`.`connections` WHERE `connections`.`source_link_id` = ? AND `connections`.`destination_link_id` = ?;");
    statement.setInt(1, sourceId);//ww  w .  j  a  v a  2  s.  co m
    statement.setInt(2, destinationId);

    statement.executeUpdate();
    statement.close();
}

From source file:com.taobao.tddl.jdbc.group.integration.CRUDTest.java

    @Test
public void DataSourceWrapper() throws Exception {
   List<DataSourceWrapper> dataSourceWrappers = new ArrayList<DataSourceWrapper>();
   dataSourceWrappers.add(new DataSourceWrapper("dbKey1","rw", DataSourceFactory.getMySQLDataSource(1), DBType.MYSQL));
   dataSourceWrappers.add(new DataSourceWrapper("dbKey2","r", DataSourceFactory.getMySQLDataSource(2), DBType.MYSQL));

   TGroupDataSource ds = new TGroupDataSource();
   ds.setDbGroupKey("myDbGroupKey");
   ds.init(dataSourceWrappers);// www.j  a v a  2  s.  co m

   Connection conn = ds.getConnection();

   //Statementcrud
   Statement stmt = conn.createStatement();
   assertEquals(stmt.executeUpdate("insert into crud(f1,f2) values(10,'str')"), 1);
   assertEquals(stmt.executeUpdate("update crud set f2='str2'"), 1);
   ResultSet rs = stmt.executeQuery("select f1,f2 from crud");
   rs.next();
   assertEquals(rs.getInt(1), 10);
   assertEquals(rs.getString(2), "str2");
   assertEquals(stmt.executeUpdate("delete from crud"), 1);
   rs.close();
   stmt.close();

   //PreparedStatementcrud
   String sql = "insert into crud(f1,f2) values(?,?)";
   PreparedStatement ps = conn.prepareStatement(sql);
   ps.setInt(1, 10);
   ps.setString(2, "str");
   assertEquals(ps.executeUpdate(), 1);
   ps.close();

   sql = "update crud set f2=?";
   ps = conn.prepareStatement(sql);
   ps.setString(1, "str2");
   assertEquals(ps.executeUpdate(), 1);
   ps.close();

   sql = "select f1,f2 from crud";
   ps = conn.prepareStatement(sql);
   rs = ps.executeQuery();
   rs.next();
   assertEquals(rs.getInt(1), 10);
   assertEquals(rs.getString(2), "str2");
   rs.close();
   ps.close();

   sql = "delete from crud";
   ps = conn.prepareStatement(sql);
   assertEquals(ps.executeUpdate(), 1);
   ps.close();

   conn.close();
}

From source file:net.sf.l2j.gameserver.model.entity.ClanHallSiege.java

public long restoreSiegeDate(int ClanHallId) {
    long res = 0;
    Connection con = null;/*  www  .  j a v a 2s . com*/
    try {
        con = L2DatabaseFactory.getInstance().getConnection();
        PreparedStatement statement = con.prepareStatement("SELECT siege_data FROM clanhall_siege WHERE id=?");
        statement.setInt(1, ClanHallId);
        ResultSet rs = statement.executeQuery();
        if (rs.next())
            res = rs.getLong("siege_data");
        rs.close();
        statement.close();
    } catch (Exception e) {
        _log.error("Exception: can't get clanhall siege date: " + e.getMessage(), e);
    } finally {
        try {
            if (con != null)
                con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    return res;
}

From source file:net.sf.l2j.gameserver.model.entity.ClanHallSiege.java

public void setNewSiegeDate(long siegeDate, int ClanHallId, int hour) {
    Calendar tmpDate = Calendar.getInstance();
    if (siegeDate <= System.currentTimeMillis()) {
        tmpDate.setTimeInMillis(System.currentTimeMillis());
        tmpDate.add(Calendar.DAY_OF_MONTH, 3);
        tmpDate.set(Calendar.DAY_OF_WEEK, 6);
        tmpDate.set(Calendar.HOUR_OF_DAY, hour);
        tmpDate.set(Calendar.MINUTE, 0);
        tmpDate.set(Calendar.SECOND, 0);
        setSiegeDate(tmpDate);/*  w ww  . j  av a 2s  . c  om*/
        Connection con = null;
        try {
            con = L2DatabaseFactory.getInstance().getConnection();
            PreparedStatement statement = con
                    .prepareStatement("UPDATE clanhall_siege SET siege_data=? WHERE id = ?");
            statement.setLong(1, getSiegeDate().getTimeInMillis());
            statement.setInt(2, ClanHallId);
            statement.execute();
            statement.close();
        } catch (Exception e) {
            _log.error("Exception: can't save clanhall siege date: " + e.getMessage(), e);
        } finally {
            try {
                if (con != null)
                    con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}