Example usage for java.sql ResultSet close

List of usage examples for java.sql ResultSet close

Introduction

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

Prototype

void close() throws SQLException;

Source Link

Document

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

Usage

From source file:net.tirasa.ilgrosso.resetdb.Main.java

private static void resetSQLServer(final Connection conn) throws SQLException {

    Statement statement = conn.createStatement();
    final ResultSet resultSet = statement.executeQuery("SELECT sysobjects.name " + "FROM sysobjects "
            + "JOIN sysusers " + "ON sysobjects.uid = sysusers.uid "
            + "WHERE OBJECTPROPERTY(sysobjects.id, N'IsView') = 1");
    final List<String> drops = new ArrayList<String>();
    while (resultSet.next()) {
        drops.add("DROP VIEW " + resultSet.getString(1));
    }//  w  w w.j  a  v  a2  s . co  m
    resultSet.close();
    statement.close();

    statement = conn.createStatement();
    for (String drop : drops) {
        statement.executeUpdate(drop);
    }
    statement.close();

    statement = conn.createStatement();
    statement.executeUpdate("EXEC sp_MSforeachtable \"ALTER TABLE ? NOCHECK CONSTRAINT all\"");
    statement.close();

    statement = conn.createStatement();
    statement.executeUpdate("EXEC sp_MSforeachtable \"DROP TABLE ?\"");
    statement.close();

    conn.close();
}

From source file:com.baomidou.mybatisplus.toolkit.IOUtils.java

/**
 * Closes a <code>AutoCloseable</code> unconditionally.
 * <p>/*from w ww  .  j a  v  a 2s.  c o  m*/
 * Equivalent to {@link ResultSet#close()}, except any exceptions will be ignored. This is typically used in finally
 * blocks.
 * <p>
 * Example code:
 * <p>
 * <pre>
 * AutoCloseable statement = null;
 * try {
 *    statement = new Connection();
 *    // process close
 *    statement.close();
 * } catch (Exception e) {
 *    // error handling
 * } finally {
 *    IOUtils.closeQuietly(conn);
 * }
 * </pre>
 *
 * @param resultSet the Connection to close, may be null or already closed
 * @since 2.2
 */
public static void closeQuietly(final ResultSet resultSet) {
    if (resultSet != null) {
        try {
            resultSet.close();
        } catch (Exception e) {
            logger.error("error close resultSet", e);
        }
    }
}

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

/**
 * Returns the DB Version for App Update purposes
 * //from w ww .  j  a v a2s . c  o  m
 * @return An Integer with the current DB Version or 0 if there was a Error getting the Version
 * @throws SQLException
 */
public static int getVersion() throws SQLException {
    PreparedStatement prest = null;
    String sql = "PRAGMA `user_version`";
    prest = c.prepareStatement(sql);
    ResultSet rs = prest.executeQuery();
    if (rs.next()) {
        int version = rs.getInt(1);
        rs.close();
        return version;
    } else {
        return 0;
    }
}

From source file:org.red5.webapps.admin.controllers.service.UserDAO.java

public static AdminUserDetails getUser(String username) {
    AdminUserDetails details = null;//from   w  ww  . j  ava 2 s . c  o  m
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = UserDatabase.getConnection();
        //make a statement
        stmt = conn.prepareStatement("SELECT * FROM APPUSER WHERE username = ?");
        stmt.setString(1, username);
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            log.debug("User found");
            details = new AdminUserDetails();
            details.setEnabled("enabled".equals(rs.getString("enabled")));
            details.setPassword(rs.getString("password"));
            details.setUserid(rs.getInt("userid"));
            details.setUsername(rs.getString("username"));
            //
            rs.close();
            //get role            
            stmt = conn.prepareStatement("SELECT authority FROM APPROLE WHERE username = ?");
            stmt.setString(1, username);
            rs = stmt.executeQuery();
            if (rs.next()) {
                GrantedAuthority[] authorities = new GrantedAuthority[1];
                authorities[0] = new GrantedAuthorityImpl(rs.getString("authority"));
                details.setAuthorities(authorities);
                //
                //if (daoAuthenticationProvider != null) {
                //User usr = new User(username, details.getPassword(), true, true, true, true, authorities);
                //daoAuthenticationProvider.getUserCache().putUserInCache(usr);               
                //}
            }
        }
        rs.close();
    } catch (Exception e) {
        log.error("Error connecting to db", e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
            }
        }
        if (conn != null) {
            UserDatabase.recycle(conn);
        }
    }
    return details;
}

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

public static void optimize() {
    Connection con = null;//from   www.j  a  va 2 s.c  o  m
    try {
        con = L2DatabaseFactory.getInstance().getConnection();
        Statement st = con.createStatement();

        final ArrayList<String> tables = new ArrayList<String>();
        {
            ResultSet rs = st.executeQuery("SHOW FULL TABLES");
            while (rs.next()) {
                String tableType = rs.getString(2/*"Table_type"*/);

                if (tableType.equals("VIEW"))
                    continue;

                tables.add(rs.getString(1));
            }
            rs.close();
        }

        {
            ResultSet rs = st.executeQuery("CHECK TABLE " + StringUtils.join(tables, ","));
            while (rs.next()) {
                String table = rs.getString("Table");
                String msgType = rs.getString("Msg_type");
                String msgText = rs.getString("Msg_text");

                if (msgType.equals("status"))
                    if (msgText.equals("OK"))
                        continue;

                _log.warn("TableOptimizer: CHECK TABLE " + table + ": " + msgType + " -> " + msgText);
            }
            rs.close();

            _log.info("TableOptimizer: Database tables have been checked.");
        }

        {
            ResultSet rs = st.executeQuery("ANALYZE TABLE " + StringUtils.join(tables, ","));
            while (rs.next()) {
                String table = rs.getString("Table");
                String msgType = rs.getString("Msg_type");
                String msgText = rs.getString("Msg_text");

                if (msgType.equals("status"))
                    if (msgText.equals("OK") || msgText.equals("Table is already up to date"))
                        continue;

                _log.warn("TableOptimizer: ANALYZE TABLE " + table + ": " + msgType + " -> " + msgText);
            }
            rs.close();

            _log.info("TableOptimizer: Database tables have been analyzed.");
        }

        {
            ResultSet rs = st.executeQuery("OPTIMIZE TABLE " + StringUtils.join(tables, ","));
            while (rs.next()) {
                String table = rs.getString("Table");
                String msgType = rs.getString("Msg_type");
                String msgText = rs.getString("Msg_text");

                if (msgType.equals("status"))
                    if (msgText.equals("OK") || msgText.equals("Table is already up to date"))
                        continue;

                if (msgType.equals("note"))
                    if (msgText.equals("Table does not support optimize, doing recreate + analyze instead"))
                        continue;

                _log.warn("TableOptimizer: OPTIMIZE TABLE " + table + ": " + msgType + " -> " + msgText);
            }
            rs.close();

            _log.info("TableOptimizer: Database tables have been optimized.");
        }
        st.close();
    } catch (Exception e) {
        _log.warn("TableOptimizer: Cannot optimize database tables!", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:com.jaspersoft.studio.data.xvia.querydesigner.TrackViaMetadata.java

public static List<String> readTableTypes(DatabaseMetaData meta) throws SQLException {
    List<String> tableTypes = new ArrayList<String>();
    ResultSet rs = meta.getTableTypes();
    while (rs.next())
        tableTypes.add(rs.getString("TABLE_TYPE")); //$NON-NLS-1$
    rs.close();
    return tableTypes;
}

From source file:gridool.db.helpers.GridDbUtils.java

public static boolean hasParentTable(@Nonnull final Connection conn, @Nullable final String pkTableName)
        throws SQLException {
    DatabaseMetaData metadata = conn.getMetaData();
    String catalog = conn.getCatalog();
    final ResultSet rs = metadata.getExportedKeys(catalog, null, pkTableName);
    try {//from ww  w.j  ava  2 s.  c om
        return rs.next();
    } finally {
        rs.close();
    }
}

From source file:org.red5.server.plugin.admin.dao.UserDAO.java

public static UserDetails getUser(String username) {
    UserDetails details = null;//from   w w w .  j a va2s.  com
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        // JDBC stuff
        DataSource ds = UserDatabase.getDataSource();

        conn = ds.getConnection();
        //make a statement
        stmt = conn.prepareStatement("SELECT * FROM APPUSER WHERE username = ?");
        stmt.setString(1, username);
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            log.debug("User found");
            details = new UserDetails();
            details.setEnabled("enabled".equals(rs.getString("enabled")));
            details.setPassword(rs.getString("password"));
            details.setUserid(rs.getInt("userid"));
            details.setUsername(rs.getString("username"));
            //
            rs.close();
            //get role            
            stmt = conn.prepareStatement("SELECT authority FROM APPROLE WHERE username = ?");
            stmt.setString(1, username);
            rs = stmt.executeQuery();
            if (rs.next()) {
                Collection<? extends GrantedAuthority> authorities;
                //                  authorities.addAll((Collection<?>) new GrantedAuthorityImpl(rs.getString("authority")));
                //                  details.setAuthorities(authorities);
                //
                //if (daoAuthenticationProvider != null) {
                //User usr = new User(username, details.getPassword(), true, true, true, true, authorities);
                //daoAuthenticationProvider.getUserCache().putUserInCache(usr);               
                //}
            }
        }
        rs.close();
    } catch (Exception e) {
        log.error("Error connecting to db", e);
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
    }
    return details;
}

From source file:application.bbdd.pool.java

public static void realizaConsulta2() {
    Connection conexion = null;// ww w. j  av a 2  s .c  om
    Statement sentencia = null;
    ResultSet rs = null;

    try {
        conexion = getConexion();
        sentencia = conexion.createStatement();
        rs = sentencia.executeQuery("select count(*) from db");
        rs.next();
        JOptionPane.showMessageDialog(null, "El numero de bd es: " + rs.getInt(1));
        logStatistics();

    } catch (SQLException e) {
        JOptionPane.showMessageDialog(null, e.toString());
    } finally {
        try {
            rs.close();
            sentencia.close();
            liberaConexion(conexion);
        } catch (Exception fe) {
            JOptionPane.showMessageDialog(null, fe.toString());
        }
    }
}

From source file:com.dynamobi.network.DynamoNetworkUdr.java

/**
 * Determines status of some jar file./*from www .  jav a2 s  .co  m*/
 * @return Returns one of DOWNLOADED, INSTALLED, or AVAILABLE.
 */
private static String getStatus(String jarFile) throws SQLException {
    String fn = "${FARRAGO_HOME}/plugin/" + jarFile;
    String outfile = FarragoProperties.instance().expandProperties(fn);
    File f = new File(outfile);
    if (f.exists()) { // Either just downloaded or installed
        String ret = "DOWNLOADED";
        Connection conn = DriverManager.getConnection("jdbc:default:connection");
        String name = jarFile.replaceAll("\\.jar", "");
        String query = "SELECT name FROM localdb.sys_root.dba_jars WHERE " + "name = ? AND url IN (?,?)";
        PreparedStatement ps = conn.prepareStatement(query);
        ps.setString(1, name);
        ps.setString(2, fn);
        ps.setString(3, "file:" + fn);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            ret = "INSTALLED";
        }
        rs.close();
        ps.close();

        return ret;
    } else {
        return "AVAILABLE";
    }
}