Example usage for java.sql Statement executeQuery

List of usage examples for java.sql Statement executeQuery

Introduction

In this page you can find the example usage for java.sql Statement executeQuery.

Prototype

ResultSet executeQuery(String sql) throws SQLException;

Source Link

Document

Executes the given SQL statement, which returns a single ResultSet object.

Usage

From source file:com.aurel.track.dbase.UpdateDbSchema.java

/**
 * Gets the database version//from   w ww .  ja va  2s.c o  m
 * @param dbConnection
 * @return
 */
public static int getDBVersion(Connection dbConnection) {
    Statement istmt = null;
    ResultSet rs = null;
    try {
        istmt = dbConnection.createStatement();
        rs = istmt.executeQuery("SELECT DBVERSION FROM TSITE");
        if (rs == null || !rs.next()) {
            LOGGER.info("TSITE is empty.");
        } else {
            return rs.getInt(1);
        }
    } catch (Exception e) {
        LOGGER.debug(ExceptionUtils.getStackTrace(e));
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (Exception e) {
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
        try {
            if (istmt != null) {
                istmt.close();
            }
        } catch (Exception e) {
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
        } catch (Exception e) {
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
    }
    return 0;
}

From source file:module.entities.NameFinder.DB.java

public static TreeMap<Integer, String> getDemocracitArticles(int consId) throws SQLException {
    TreeMap<Integer, String> all = new TreeMap<>();
    String sql = "SELECT id, body " + "FROM articles " + "WHERE consultation_id = " + consId
            + " AND id NOT IN (SELECT enhancedentities.article_id FROM enhancedentities);";
    Statement stmt = connection.createStatement();
    ResultSet rs = stmt.executeQuery(sql);
    //        PreparedStatement preparedStatement = connection.prepareStatement(sql);
    //        preparedStatement.setInt(1, consId);
    //        System.out.println(sql);
    //        ResultSet rs = preparedStatement.executeQuery();
    Document doc;//from ww w  .  ja  v a 2 s  . com
    while (rs.next()) {
        int articleID = rs.getInt("id");
        String article_text = rs.getString("body");
        doc = Jsoup.parseBodyFragment(article_text);
        all.put(articleID, doc.text());
    }
    return all;
}

From source file:module.entities.NameFinder.DB.java

public static TreeMap<Integer, String> getDemocracitCompletedConsultationBody() throws SQLException {
    TreeMap<Integer, String> cons_compl_desc = new TreeMap<>();
    String sql = "SELECT id, completed_text " + "FROM consultation "
            + "WHERE completed = 1 AND id NOT IN (SELECT consultations_ner.id FROM consultations_ner);";
    Statement stmt = connection.createStatement();
    ResultSet rs = stmt.executeQuery(sql);
    while (rs.next()) {
        int consID = rs.getInt("id");
        String cons_compl_text = rs.getString("completed_text");
        //            String cons_compl = rs.getString("completed_text");
        //            if (cons_compl != null) {
        //                String cons_text = cons_compl + "\n" + cons_desc;
        //                cons_body.put(consID, cons_text);
        //            } else {
        cons_compl_desc.put(consID, cons_compl_text);
        //            }
    }//from   w  w  w. j a va2s  .  com
    return cons_compl_desc;
}

From source file:com.googlecode.jtiger.modules.ecside.util.ECSideUtils.java

public static synchronized long getSEQSN(Connection conn, String SEQName) {

    long seqSN = 0;
    long startPoint = 1125983190000L;

    try {//from   w  w  w  .  j  av  a 2  s  . c  o m
        String query = "select " + SEQName + ".nextval from dual";
        Statement stmt = conn.createStatement();
        ResultSet rest = stmt.executeQuery(query);
        if (rest.next()) {
            seqSN = rest.getInt(1);
        }
    } catch (Exception e) {
        long t1 = System.currentTimeMillis();
        while (t1 == System.currentTimeMillis()) {
            // Thread.sleep(1);
        }
        seqSN = System.currentTimeMillis() - startPoint;
    }
    return seqSN;
}

From source file:module.entities.NameFinder.DB.java

public static TreeMap<Integer, String> getDemocracitConsultationBody() throws SQLException {
    TreeMap<Integer, String> cons_body = new TreeMap<>();
    String sql = "SELECT id, short_description, completed_text " + "FROM consultation " + "WHERE id "
    //                + "=3338;";
            + "NOT IN (SELECT consultations_ner.id FROM consultations_ner);";
    Statement stmt = connection.createStatement();
    ResultSet rs = stmt.executeQuery(sql);
    while (rs.next()) {
        int consID = rs.getInt("id");
        String cons_desc = rs.getString("short_description");
        //            String cons_compl = rs.getString("completed_text");
        //            if (cons_compl != null) {
        //                String cons_text = cons_compl + "\n" + cons_desc;
        //                cons_body.put(consID, cons_text);
        //            } else {
        cons_body.put(consID, cons_desc);
        //            }
    }//  w  w w. ja  va  2s .co  m
    return cons_body;
}

From source file:com.ultrapower.eoms.common.plugin.ecside.util.ECSideUtils.java

 public static synchronized long getSEQSN(Connection conn, String SEQName) {

   long seqSN = 0;
   long startPoint = 1125983190000L;

   try {/*from  w w  w.  j a v  a2 s . c  o m*/
      String query = "select " + SEQName + ".nextval from dual";
      Statement stmt = conn.createStatement();
      ResultSet rest = stmt.executeQuery(query);
      if (rest.next()) {
         seqSN = rest.getInt(1);
      }
   } catch (Exception e) {
      long t1 = System.currentTimeMillis();
      while (t1 == System.currentTimeMillis()) {
         // Thread.sleep(1);
      }
      seqSN = System.currentTimeMillis() - startPoint;
   }
   return seqSN;
}

From source file:com.oracle.tutorial.jdbc.CoffeesTable.java

public static void alternateViewTable(Connection con) throws SQLException {
    Statement stmt = null;
    String query = "select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES";
    try {/*w w  w. ja v a  2  s  .c  o m*/
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            String coffeeName = rs.getString(1);
            int supplierID = rs.getInt(2);
            float price = rs.getFloat(3);
            int sales = rs.getInt(4);
            int total = rs.getInt(5);
            System.out.println(coffeeName + ", " + supplierID + ", " + price + ", " + sales + ", " + total);
        }
    } catch (SQLException e) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmt != null) {
            stmt.close();
        }
    }
}

From source file:com.oracle.tutorial.jdbc.CoffeesTable.java

public static void viewTable(Connection con) throws SQLException {
    Statement stmt = null;
    String query = "select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES";
    try {// ww  w.  j a v a  2s.co m
        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {
            String coffeeName = rs.getString("COF_NAME");
            int supplierID = rs.getInt("SUP_ID");
            float price = rs.getFloat("PRICE");
            int sales = rs.getInt("SALES");
            int total = rs.getInt("TOTAL");
            System.out.println(coffeeName + ", " + supplierID + ", " + price + ", " + sales + ", " + total);
        }

    } catch (SQLException e) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmt != null) {
            stmt.close();
        }
    }
}

From source file:com.redhat.rhn.common.db.datasource.test.AdvDataSourceTest.java

protected static void oneTimeSetup() throws Exception {
    Session session = null;/*from   w  w w .j  a  v a  2  s.  c  om*/
    Connection c = null;
    Statement stmt = null;
    try {
        session = HibernateFactory.getSession();
        c = session.connection();
        stmt = c.createStatement();
        stmt.executeQuery("select 1 from adv_datasource");
    } catch (SQLException e) {
        // Couldn't select 1, so the table didn't exist, create it
        stmt.execute("create table adv_datasource " + "( " + "  foobar VarChar2(32),"
                + "  test_column VarChar2(25)," + "  pin    number, " + "  id     number"
                + "         constraint adv_datasource_pk primary key" + ")");
        stmt.execute("insert into adv_datasource(foobar, id) " + "values ('Blarg', 1)");
        c.commit();
    } finally {
        HibernateHelper.cleanupDB(stmt);
    }
}

From source file:gridool.util.jdbc.JDBCUtils.java

/**
 * Execute an SQL SELECT query without any replacement parameters.  The
 * caller is responsible for closing the connection.
 * //from  ww w .ja va2 s. c o  m
 * @param conn The connection to execute the query in.
 * @param sql The query to execute.
 * @return The object represents ResultSet.
 */
public static ResultSet fetch(Connection conn, String sql) throws SQLException {
    ResultSet rs = null;
    try {
        Statement stmt = conn.createStatement();
        verboseQuery(sql, (Object[]) null);
        rs = stmt.executeQuery(sql);
    } catch (SQLException e) {
        rethrow(e, sql, (Object[]) null);
    }
    return rs;
}