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:TestDebug_MySQL.java

public static int countRows(Connection conn, String tableName) throws SQLException {
    Statement stmt = null;
    ResultSet rs = null;//w  w  w. j  a  va  2  s . co  m
    int rowCount = -1;
    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery("SELECT COUNT(*) FROM " + tableName);
        // get the number of rows from the result set
        rs.next();
        rowCount = rs.getInt(1);
    } finally {
        rs.close();
        stmt.close();
    }

    return rowCount;
}

From source file:CountRows_MySQL.java

public static int countRows(Connection conn, String tableName) throws SQLException {
    // select the number of rows in the table
    Statement stmt = null;
    ResultSet rs = null;/*from w  w w.  java 2s .  c  o  m*/
    int rowCount = -1;
    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery("SELECT COUNT(*) FROM " + tableName);
        // get the number of rows from the result set
        rs.next();
        rowCount = rs.getInt(1);
    } finally {
        rs.close();
        stmt.close();
    }
    return rowCount;
}

From source file:CountRows_Oracle.java

public static int countRows(Connection conn, String tableName) throws SQLException {
    // select the number of rows in the table
    Statement stmt = null;
    ResultSet rs = null;/*from  w  ww  .j a  v a  2  s. c  om*/
    int rowCount = -1;
    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery("SELECT COUNT(*) FROM " + tableName);
        // get the number of rows from the result set
        rs.next();
        rowCount = rs.getInt(1);
    } finally {
        rs.close();
        stmt.close();
    }

    return rowCount;
}

From source file:org.jw.service.factory.StatisticsChartFactory.java

public static CategoryDataset createCategoryDataset(Connection conn, String query) throws SQLException {
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    Statement statement = conn.createStatement();
    ResultSet resultSet = statement.executeQuery(query);

    while (resultSet.next()) {
        Double value = resultSet.getDouble("value");
        String series = resultSet.getString("series");
        String category = resultSet.getString("category");
        dataset.addValue(value, series, category);
    }// w  ww .  j a va  2  s  . c o m

    return dataset;
}

From source file:at.molindo.dbcopy.util.Utils.java

public static <T> T execute(Connection c, String query, ResultSetHandler<T> handler) throws SQLException {
    Statement stmt = c.createStatement();
    try {//from   w  w w . j a  va2  s. c o  m
        ResultSet rs = stmt.executeQuery(query);
        return handle(rs, handler);
    } finally {
        stmt.close();
    }
}

From source file:dbutils.ExampleJDBC.java

/**
 * Unlike some other classes in DbUtils, this class(SqlNullCheckedResultSet) is NOT thread-safe.
 *//* w  w w . j  a v a  2s.c om*/
public static void findUseSqlNullCheckedResultSet() {
    Connection conn = getConnection();
    try {
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT id, name, gender, age, team_id as teamId FROM test_student");
        SqlNullCheckedResultSet wrapper = new SqlNullCheckedResultSet(rs);
        wrapper.setNullString("N/A"); // Set null string
        rs = ProxyFactory.instance().createResultSet(wrapper);

        while (rs.next()) {
            System.out.println("id=" + rs.getInt("id") + " username=" + rs.getString("name") + " gender="
                    + rs.getString("gender"));
        }
        rs.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        DbUtils.closeQuietly(conn);
    }
}

From source file:br.senac.tads.pi3.ghosts.locarsys.dao.Relatorios.java

public static void relatoriosVendas() throws SQLException, ClassNotFoundException {
    String query = "SELECT FL.NOME_FILIAL, COUNT(FL.NOME_FILIAL) AS QUANTIDADE FROM FILIAL FL "
            + "INNER JOIN FUNCIONARIO FUNC ON FUNC.ID_FILIAL = FL.ID_FILIAL "
            + "INNER JOIN ALUGUEL AL ON AL.ID_FUNCIONARIO = FUNC.ID_FUNCIONARIO " + "GROUP BY FL.NOME_FILIAL";
    Connection conn = Conexoes.obterConexao();
    Statement stmt = conn.createStatement();

    ResultSet rs = stmt.executeQuery(query);

    DefaultCategoryDataset ds = new DefaultCategoryDataset();

    while (rs.next()) {
        ds.addValue(rs.getInt("QUANTIDADE"), "Quantidade", rs.getString("NOME_FILIAL"));
    }/*  w w w. jav  a 2s  .c om*/

    JFreeChart grafico = ChartFactory.createBarChart3D("Relatrio de Aluguis", "Filiais",
            "Separadas por filiais", ds, PlotOrientation.VERTICAL, true, true, false);

    try (OutputStream arquivo = new FileOutputStream("ImagensLoCarSys\\vendas.png")) {
        ChartUtilities.writeChartAsPNG(arquivo, grafico, 800, 600);
    } catch (FileNotFoundException ex) {
        System.out.println("" + ex.getMessage());
    } catch (IOException ex) {
        System.out.println("" + ex.getMessage());
    }

    //try (OutputStream arquivo = new FileOutputStream("C:\\Users\\bruno.clopes\\Documents\\NetBeansProjects\\LoCarSys\\target\\LoCarSys-1.0-SNAPSHOT\\ImagensLoCarSys\\vendas.png")) {
    try (OutputStream arquivo = new FileOutputStream(
            "C:\\Users\\temp.cas\\Documents\\NetBeansProjects\\LoCarSys\\target\\LoCarSys-1.0-SNAPSHOT\\ImagensLoCarSys\\vendas.png")) {
        ChartUtilities.writeChartAsPNG(arquivo, grafico, 800, 600);
    } catch (FileNotFoundException ex) {
        System.out.println("" + ex.getMessage());
    } catch (IOException ex) {
        System.out.println("" + ex.getMessage());
    }
}

From source file:br.senac.tads.pi3.ghosts.locarsys.dao.Relatorios.java

public static void relatoriosDisponibilidade() throws SQLException, ClassNotFoundException {
    String query = "SELECT FL.NOME_FILIAL, COUNT(FL.NOME_FILIAL) AS QUANTIDADE FROM CARRO CA "
            + "INNER JOIN FILIAL FL ON FL.ID_FILIAL = CA.ID_FILIAL " + "WHERE CA.DISPONIBILIDADE_CARRO = '1' "
            + "GROUP BY FL.NOME_FILIAL";
    Connection conn = Conexoes.obterConexao();
    Statement stmt = conn.createStatement();

    ResultSet rs = stmt.executeQuery(query);

    DefaultCategoryDataset ds = new DefaultCategoryDataset();

    while (rs.next()) {
        ds.addValue(rs.getInt("QUANTIDADE"), "Quantidade", rs.getString("NOME_FILIAL"));
    }//from  www.  j a v  a 2s  .  c  o  m

    /*File fg = new File("C:\\Users\\bruno.lopes.KRONMED\\Documents\\NetBeansProjects\\LoCarSys\\src\\main\\webapp\\ImagensLoCarSys\\disponibilidade.png");
     fg.delete();*/
    JFreeChart grafico = ChartFactory.createBarChart3D("Relatrio de Disponibilidade", "Filiais",
            "Separados por filiais", ds, PlotOrientation.VERTICAL, true, true, false);

    //try (OutputStream arquivo = new FileOutputStream("C:\\Users\\bruno.clopes\\Documents\\NetBeansProjects\\LoCarSys\\target\\LoCarSys-1.0-SNAPSHOT\\ImagensLoCarSys\\disponibilidade.png")) {
    try (OutputStream arquivo = new FileOutputStream("ImagensLoCarSys\\disponibilidade.png")) {
        ChartUtilities.writeChartAsPNG(arquivo, grafico, 800, 600);
    } catch (FileNotFoundException ex) {
        System.out.println("" + ex.getMessage());
    } catch (IOException ex) {
        System.out.println("" + ex.getMessage());
    }

    //try (OutputStream arquivo = new FileOutputStream("C:\\Users\\bruno.clopes\\Documents\\NetBeansProjects\\LoCarSys\\target\\LoCarSys-1.0-SNAPSHOT\\ImagensLoCarSys\\disponibilidade.png")) {
    try (OutputStream arquivo = new FileOutputStream(
            "C:\\Users\\temp.cas\\Documents\\NetBeansProjects\\LoCarSys\\target\\LoCarSys-1.0-SNAPSHOT\\ImagensLoCarSys\\disponibilidade.png")) {
        ChartUtilities.writeChartAsPNG(arquivo, grafico, 800, 600);
    } catch (FileNotFoundException ex) {
        System.out.println("" + ex.getMessage());
    } catch (IOException ex) {
        System.out.println("" + ex.getMessage());
    }
}

From source file:datatableinputdata.DataTableInputData.java

private static void WriteToDB(Map<String, String> map /*String num, String name, String filename*/)
        throws SQLException {
    CallableStatement cstmt = null;
    try {//from  w ww.  ja v a 2  s  .c  o  m
        if (connection == null)
            DBConnect();

        Statement stmt = connection.createStatement();

        ResultSet r = null;

        r = stmt.executeQuery("truncate table DATA");

        for (Map.Entry<String, String> entry : map.entrySet()) {

            r = stmt.executeQuery(
                    "insert into DATA values('" + entry.getValue() + "','" + entry.getKey() + "')");
            /*System.out.println("Country [code= " + entry.getKey() + " , name="
               + entry.getValue() + "]");*/

        }

        /* String day = "";
         cstmt = connection.prepareCall("{call argen666.TESTPRM(?,?)}");
         cstmt.registerOutParameter(2, java.sql.Types.VARCHAR);
         cstmt.setString(1, "Monday");
         cstmt.executeUpdate();
         day = cstmt.getString(2);
         System.out.println(day);*/

        /*cstmt = connection.prepareCall("{call ? :=parus.get_license()}");
        cstmt.registerOutParameter(1, java.sql.Types.VARCHAR);
        cstmt.executeUpdate();
        day = cstmt.getString(1);
        System.out.println(day);*/

        //parus.P_INORDERS_SET_STATUS(59945, 109795, 0, TO_DATE('25.09.2014', 'DD.MM.YYYY HH24:MI:SS'), NWARNING, SMSG);
        //java.sql.SQLException: ORA-20103:   ? , ..  "Other"  ?.
        /*cstmt = connection.prepareCall("{call parus.P_INORDERS_SET_STATUS(59945, 109795, 0, TO_DATE('25.09.2014', 'DD.MM.YYYY HH24:MI:SS'), ?, ?)}");
         cstmt.registerOutParameter(1, java.sql.Types.NUMERIC);
         cstmt.registerOutParameter(2, java.sql.Types.VARCHAR);
         cstmt.executeUpdate();
         day=cstmt.getString(1);
         System.out.println(day);*/
    } catch (SQLException e) {
        System.out.println(e);
    } finally {
        if (cstmt != null) {
            cstmt.close();
        }
    }
}

From source file:com.data2semantics.yasgui.server.db.ConnectionFactory.java

private static ArrayList<Integer> getDeltas(Connection connect) throws SQLException {
    ArrayList<Integer> deltaIds = new ArrayList<Integer>();
    String sql = "SELECT Id FROM Deltas WHERE 1 ORDER BY Id ASC";
    Statement statement = connect.createStatement();
    ResultSet result = statement.executeQuery(sql);
    while (result.next()) {
        deltaIds.add(result.getInt("Id"));
    }//  ww w  . ja  v a 2  s .c o  m
    return deltaIds;
}