List of usage examples for java.sql Statement executeQuery
ResultSet executeQuery(String sql) throws SQLException;
ResultSet
object. From source file:com.p6spy.engine.spy.P6TestUtil.java
public static int queryForInt(Connection con, String sql) throws SQLException { Statement stmt = null; ResultSet rs = null;// ww w. j a va 2s . c o m try { stmt = con.createStatement(); rs = stmt.executeQuery(sql); rs.next(); return rs.getInt(1); } finally { if (rs != null) try { rs.close(); } catch (Exception e) { } if (stmt != null) try { stmt.close(); } catch (Exception e) { } } }
From source file:com.data2semantics.yasgui.server.db.ConnectionFactory.java
/** * Check if database exists/*from ww w.ja va 2 s. co m*/ * @param connect * @param dbName * @return * @throws SQLException */ private static boolean databaseExists(Connection connect, String dbName) throws SQLException { Statement statement = connect.createStatement(); ResultSet resultSet = statement.executeQuery( "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '" + dbName + "'"); boolean exists = resultSet.next(); statement.close(); resultSet.close(); return exists; }
From source file:com.nabla.wapp.server.database.Database.java
/** * Test if a table has any data/* w ww .ja v a 2s.co m*/ * @param conn - database connection * @param tableName - table name * @return success * @throws SQLException */ public static boolean isTableEmpty(final Connection conn, final String tableName) throws SQLException { final Statement stmt = conn.createStatement(); try { final ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM " + tableName + ";"); try { return rs.next() && rs.getInt(1) == 0; } finally { rs.close(); } } finally { stmt.close(); } }
From source file:net.big_oh.common.jdbc.JdbcProxyExerciser.java
private static void exerciseRegularSelect(Connection con) throws SQLException { logger.info(StringUtils.center("exercise regular select", 100, "-")); Statement stmt = null; ResultSet rs = null;/*from w w w . ja v a 2 s.c o m*/ try { stmt = con.createStatement(); rs = stmt.executeQuery("SELECT * FROM TEST_TABLE"); while (rs.next()) { System.out.println(rs.getString("TEST_COLUMN")); } } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(stmt); } }
From source file:CheckJDBCInstallation_Oracle.java
/** * Test Validity of a Connection/* w w w .j av a 2 s .c o m*/ * * @param conn * a JDBC connection object * @param query * a sql query to test against database connection * @return true if a given connection object is a valid one; otherwise return * false. */ public static boolean testConnection(Connection conn, String query) { ResultSet rs = null; Statement stmt = null; try { stmt = conn.createStatement(); if (stmt == null) { return false; } rs = stmt.executeQuery(query); if (rs == null) { return false; } if (rs.next()) { // connection object is valid: we were able to // connect to the database and return something useful. return true; } // there is no hope any more for the validity // of the connection object return false; } catch (Exception e) { return false; } finally { // close database resources try { rs.close(); stmt.close(); } catch (Exception e) { // ignore } } }
From source file:CheckJDBCInstallation.java
/** * Test Validity of a Connection/*w ww . j a v a 2s. c o m*/ * * @param conn * a JDBC connection object * @param query * a sql query to test against db connection * @return true if a given connection object is a valid one; otherwise return * false. */ public static boolean testConnection(Connection conn, String query) { ResultSet rs = null; Statement stmt = null; try { stmt = conn.createStatement(); if (stmt == null) { return false; } rs = stmt.executeQuery(query); if (rs == null) { return false; } // connection object is valid: you were able to // connect to the database and return something useful. if (rs.next()) { return true; } // there is no hope any more for the validity // of the Connection object return false; } catch (Exception e) { // something went wrong: connection is bad return false; } finally { // close database resources try { rs.close(); stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:CheckJDBCInstallation_MySQL.java
/** * Test Validity of a Connection/*from ww w . ja v a 2 s . c o m*/ * * @param conn * a JDBC connection object * @param query * a sql query to test against database connection * @return true if a given connection object is a valid one; otherwise return * false. */ public static boolean testConnection(Connection conn, String query) { ResultSet rs = null; Statement stmt = null; try { stmt = conn.createStatement(); if (stmt == null) { return false; } rs = stmt.executeQuery(query); if (rs == null) { return false; } if (rs.next()) { // connection object is valid: we were able to // connect to the database and return something useful. return true; } // there is no hope any more for the validity // of the connection object return false; } catch (Exception e) { // // something went wrong: connection is bad // return false; } finally { try { rs.close(); stmt.close(); conn.close(); } catch (Exception e) { } } }
From source file:net.tirasa.ilgrosso.resetdb.Main.java
private static void resetPostgreSQL(final Connection conn) throws Exception { final Statement statement = conn.createStatement(); final ResultSet resultSet = statement .executeQuery("SELECT 'DROP TABLE ' || c.relname || ' CASCADE;' FROM pg_catalog.pg_class c " + "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace " + "WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast') " + "AND pg_catalog.pg_table_is_visible(c.oid)"); final List<String> drops = new ArrayList<String>(); while (resultSet.next()) { drops.add(resultSet.getString(1)); }//from w w w. j a v a2 s . c om resultSet.close(); for (String drop : drops) { statement.executeUpdate(drop.substring(0, drop.length() - 1)); } statement.close(); conn.close(); }
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)); }//from w w w . j a va2s. c o 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:net.tirasa.ilgrosso.resetdb.Main.java
private static void resetMySQL(final Connection conn) throws Exception { final Statement statement = conn.createStatement(); ResultSet resultSet = statement .executeQuery("SELECT concat('DROP VIEW IF EXISTS ', table_name, ' CASCADE;')" + "FROM information_schema.views;"); final List<String> drops = new ArrayList<String>(); while (resultSet.next()) { drops.add(resultSet.getString(1)); }//from www .j a v a 2 s.co m resultSet.close(); for (String drop : drops) { statement.executeUpdate(drop.substring(0, drop.length() - 1)); } drops.clear(); drops.add("SET FOREIGN_KEY_CHECKS = 0;"); resultSet = statement.executeQuery("SELECT concat('DROP TABLE IF EXISTS ', table_name, ' CASCADE;')" + "FROM information_schema.tables;"); while (resultSet.next()) { drops.add(resultSet.getString(1)); } resultSet.close(); drops.add("SET FOREIGN_KEY_CHECKS = 1;"); for (String drop : drops) { statement.executeUpdate(drop.substring(0, drop.length() - 1)); } statement.close(); conn.close(); }