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.taobao.tddl.jdbc.group.integration.CRUDTest.java

    () throws Exception {
   DataSource ds1 = DataSourceFactory.getMySQLDataSource(1);
   DataSource ds2 = DataSourceFactory.getMySQLDataSource(2);
   DataSource ds3 = DataSourceFactory.getMySQLDataSource(3);
      /*from  ww  w .j ava  2s .  c  o m*/
      
   //db3db2db1
   TGroupDataSource ds = new TGroupDataSource();
   DataSourceWrapper dsw1 = new DataSourceWrapper("db1", "r10w", ds1, DBType.MYSQL);
   DataSourceWrapper dsw2 = new DataSourceWrapper("db2", "r20", ds2, DBType.MYSQL);
   DataSourceWrapper dsw3 = new DataSourceWrapper("db3", "r30", ds3, DBType.MYSQL);
   ds.init(dsw1, dsw2, dsw3);

   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(10,'str')";
   PreparedStatement ps = conn.prepareStatement(sql);
   assertEquals(ps.executeUpdate(), 1);
   ps.close();

   sql = "update crud set f2='str2'";
   ps = conn.prepareStatement(sql);
   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:com.att.pirates.controller.GlobalDataController.java

public static boolean isATTEmployeeITUPRoleEmpty(String UUID) {
    ResultSet rs = null;//  w w  w  . ja va 2s .  co m
    Connection conn = null;
    PreparedStatement preparedStatement = null;
    boolean rc = false;

    try {
        conn = DBUtility.getDBConnection();
        // SQL query command
        String SQL = "  select * from ATTEmployeeArtifacts where uuid = ? ";

        preparedStatement = conn.prepareStatement(SQL);
        preparedStatement.setString(1, UUID);
        rs = preparedStatement.executeQuery();

        if (rs.next()) {
            rc = true;
        }
    } catch (SQLException e) {
        logger.error(e.getMessage());
    } catch (Exception e) {
        logger.error(e.getMessage());
    } finally {
        try {
            if (rs != null)
                rs.close();
        } catch (Exception e) {
        }
        ;
        try {
            if (preparedStatement != null)
                preparedStatement.close();
        } catch (Exception e) {
        }
        ;
        try {
            if (conn != null)
                conn.close();
        } catch (Exception e) {
        }
        ;
    }

    return rc;
}

From source file:com.concursive.connect.web.modules.documents.dao.FileFolder.java

/**
 * Description of the Method/* w w w .  j  av a  2 s . co  m*/
 *
 * @param db        Description of the Parameter
 * @param hierarchy Description of the Parameter
 * @param currentId Description of the Parameter
 * @throws SQLException Description of the Exception
 */
public static void buildHierarchy(Connection db, Map hierarchy, int currentId) throws SQLException {
    PreparedStatement pst = db.prepareStatement(
            "SELECT parent_id, subject, display " + "FROM project_folders " + "WHERE folder_id = ? ");
    pst.setInt(1, currentId);
    ResultSet rs = pst.executeQuery();
    int parentId = 0;
    String subject = null;
    int display = -1;
    if (rs.next()) {
        parentId = DatabaseUtils.getInt(rs, "parent_id");
        subject = rs.getString("subject");
        display = DatabaseUtils.getInt(rs, "display");
    }
    rs.close();
    pst.close();
    hierarchy.put(new Integer(currentId), new String[] { subject, String.valueOf(display) });
    if (parentId > -1) {
        FileFolder.buildHierarchy(db, hierarchy, parentId);
    }
}

From source file:com.searchcode.app.util.Helpers.java

public void closeQuietly(PreparedStatement preparedStatement) {
    try {//  w  w  w.  ja v  a  2 s.  c o m
        preparedStatement.close();
    } catch (Exception ex) {
    }
}

From source file:de.iritgo.aktario.jdbc.DeleteUser.java

/**
 * Perform the command./*from w  w  w  . j a  v  a 2 s  . c  o m*/
 */
public void perform() {
    if (properties.get("id") == null) {
        Log.logError("persist", "DeleteUser", "Missing unique id for the user to delete");

        return;
    }

    UserRegistry userRegistry = Server.instance().getUserRegistry();
    long userId = ((Long) properties.get("id")).longValue();
    User user = userRegistry.getUser(userId);

    if (user == null) {
        Log.logError("persist", "DeleteUser", "Unable to find user with id " + userId);

        return;
    }

    JDBCManager jdbcManager = (JDBCManager) Engine.instance().getManager("persist.JDBCManager");
    DataSource dataSource = jdbcManager.getDefaultDataSource();

    Connection connection = null;
    PreparedStatement stmt = null;

    try {
        connection = dataSource.getConnection();

        stmt = connection.prepareStatement("delete from IritgoUser where id=?");
        stmt.setLong(1, userId);
        stmt.execute();
        stmt.close();

        stmt = connection.prepareStatement("delete from IritgoNamedObjects where userId=?");
        stmt.setLong(1, userId);
        stmt.execute();
        stmt.close();

        Log.logVerbose("persist", "DeleteUser", "DELETE USER " + userId);
    } catch (SQLException x) {
        Log.logError("persist", "DeleteUser", "Error while storing user with id " + userId + ": " + x);
    } finally {
        DbUtils.closeQuietly(stmt);
        DbUtils.closeQuietly(connection);
    }
}

From source file:com.rajaram.bookmark.dao.BookmarkDaoImpl.java

@Override
public List<Bookmark> getBookmarks(String userName) {

    String sql = "select * from " + tableName + " where user_name = ?";

    Connection conn = null;//  www . j  a va2  s. co  m
    BookmarkRowMapper rowExtract = new BookmarkRowMapper();
    List<Bookmark> bookmarks = null;

    try {
        conn = dataSource.getConnection();
        PreparedStatement prepareStatement = conn.prepareStatement(sql);
        prepareStatement.setString(1, userName);
        ResultSet rs = prepareStatement.executeQuery();

        bookmarks = rowExtract.mapRow(rs);

        rs.close();
        prepareStatement.close();
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
    }

    return bookmarks;
}

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

private void cleanTables() {
    Connection con = null;/*from   ww  w. j  a  v  a2s .co m*/
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement = con.prepareStatement("TRUNCATE TABLE offline_traders");
        statement.execute();
        statement.close();
        statement = con.prepareStatement("TRUNCATE TABLE offline_traders_items");
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.warn("OfflineTradeManager: Could not clear table: ", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:com.concursive.connect.web.modules.register.beans.RegisterBean.java

/**
 * Description of the Method/*from w  w w  .ja va  2  s  .c o  m*/
 *
 * @param db                Description of the Parameter
 * @param partialUserRecord Description of the Parameter
 * @return Description of the Return Value
 * @throws SQLException Description of the Exception
 */
private static void updateRegisteredStatus(Connection db, User partialUserRecord) throws SQLException {
    // NOTE: Assume the user object isn't complete, so can't load it, etc.
    {
        // Approve the user
        PreparedStatement pst = db
                .prepareStatement("UPDATE users " + "SET first_name = ?, last_name = ?, password = ?, "
                        + "company = ?, registered = ?, enabled = ?, terms = ? " + "WHERE user_id = ? ");
        int i = 0;
        pst.setString(++i, partialUserRecord.getFirstName());
        pst.setString(++i, partialUserRecord.getLastName());
        pst.setString(++i, partialUserRecord.getPassword());
        pst.setString(++i, partialUserRecord.getCompany());
        pst.setBoolean(++i, true);
        pst.setBoolean(++i, true);
        pst.setBoolean(++i, partialUserRecord.getTerms());
        pst.setInt(++i, partialUserRecord.getId());
        pst.executeUpdate();
        pst.close();
        CacheUtils.invalidateValue(Constants.SYSTEM_USER_CACHE, partialUserRecord.getId());
    }

    {
        // Approve the user's profile and update their location
        User user = UserUtils.loadUser(partialUserRecord.getId());
        if (user == null) {
            LOG.warn("updateRegisteredStatus - USER RECORD IS NULL");
        } else {
            Project profile = ProjectUtils.loadProject(user.getProfileProjectId());
            if (profile == null) {
                LOG.warn("updateRegisteredStatus - PROFILE RECORD IS NULL");
            } else {
                profile.setApproved(true);
                profile.setCity(partialUserRecord.getCity());
                profile.setState(partialUserRecord.getState());
                profile.setCountry(partialUserRecord.getCountry());
                profile.setPostalCode(partialUserRecord.getPostalCode());
                profile.update(db);
            }
        }
    }
}

From source file:edu.umass.cs.gnsclient.client.util.keystorage.SimpleKeyStore.java

private void safelyClose(PreparedStatement p) {
    try {/*from   w ww  .  j  ava  2  s  .  c o  m*/
        if (p != null) {
            p.close();
        }
    } catch (SQLException e) {
        DerbyControl.printSQLException(e);
    }
}

From source file:com.appeligo.amazon.ProgramIndexer.java

private void close(PreparedStatement ps) {
    if (ps != null) {
        try {//from   w  ww .j a  v a 2 s.c  o m
            ps.close();
        } catch (SQLException e) {
            log.warn("Cannot close statement.", e);
        }
    }
}