Example usage for java.sql PreparedStatement executeQuery

List of usage examples for java.sql PreparedStatement executeQuery

Introduction

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

Prototype

ResultSet executeQuery() throws SQLException;

Source Link

Document

Executes the SQL query in this PreparedStatement object and returns the ResultSet object generated by the query.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    int records = 0;

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    String sql = "SELECT COUNT(*) FROM mytable";
    PreparedStatement prest = con.prepareStatement(sql);
    ResultSet rs = prest.executeQuery();
    while (rs.next()) {
        records = rs.getInt(1);/*w  w w  . j  a  v a2 s.c o  m*/
    }
    System.out.println("Number of records: " + records);
    con.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(url, username, password);

    String sql = "SELECT name, description, image FROM pictures ";
    PreparedStatement stmt = conn.prepareStatement(sql);
    ResultSet resultSet = stmt.executeQuery();
    while (resultSet.next()) {
        String name = resultSet.getString(1);
        String description = resultSet.getString(2);
        File image = new File("D:\\java.gif");
        FileOutputStream fos = new FileOutputStream(image);

        byte[] buffer = new byte[1];
        InputStream is = resultSet.getBinaryStream(3);
        while (is.read(buffer) > 0) {
            fos.write(buffer);//from  w  w  w  .  ja  va2s . c  o  m
        }
        fos.close();
    }
    conn.close();
}

From source file:PreparedStmt.java

public static void main(String args[]) throws Exception {

    String query = "SELECT * FROM Stock WHERE Item_Number = ?";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Inventory");
    PreparedStatement pstmt = con.prepareStatement(query);
    pstmt.setInt(1, 2);//from   www.j  av a 2 s.co  m
    ResultSet rs = pstmt.executeQuery();
    while (rs.next()) {
        String name = rs.getString("Name");
        String desc = rs.getString("Description");
        int qty = rs.getInt("Qty");
        float cost = rs.getFloat("Cost");
        System.out.println(name + ", " + desc + "\t: " + qty + "\t@ $" + cost);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection dbConnection = null;
    String myConnectionString = "";
    myConnectionString = "jdbc:mysql://192.168.1.3:3306/mytestdb";
    dbConnection = DriverManager.getConnection(myConnectionString, "root", "whatever");
    PreparedStatement stmt = dbConnection.prepareStatement("SELECT * FROM jdbctest");
    ResultSet rs = stmt.executeQuery();
    int i = 0;/*from w  w w .  ja  va2 s  .  co m*/
    int j = 0;
    String s = "";
    while (rs.next()) {
        i++;
        j = rs.getInt("id");
        s = rs.getString("textcol");
    }
    System.out.println(String.format("Finished reading %d rows.", i));
    rs.close();
    stmt.close();
    dbConnection.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    String sql = "SELECT * FROM product WHERE year_made = ?";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setInt(1, 2002);//  w w  w .j a v a 2s.co m
    ResultSet rs1 = prest.executeQuery();
    while (rs1.next()) {
        String mov_name = rs1.getString(1);
        int mad_year = rs1.getInt(2);
        System.out.println(mov_name + "\t- " + mad_year);
    }
    prest.setInt(1, 2003);
    ResultSet rs2 = prest.executeQuery();
    while (rs2.next()) {
        String mov_name = rs2.getString(1);
        int mad_year = rs2.getInt(2);
        System.out.println(mov_name + "\t- " + mad_year);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(url, username, password);
    PreparedStatement stmt = conn.prepareStatement("SELECT name, description, data FROM documents ");
    ResultSet resultSet = stmt.executeQuery();
    while (resultSet.next()) {
        String name = resultSet.getString(1);
        String description = resultSet.getString(2);
        File data = new File("C:\\a.txt");
        Reader reader = resultSet.getCharacterStream(3);
        FileWriter writer = new FileWriter(data);
        char[] buffer = new char[1];
        while (reader.read(buffer) > 0) {
            writer.write(buffer);//from ww  w .ja va2 s  .  c  o m
        }
        writer.close();
    }
    conn.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int count = 0;
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    String sql = "SELECT title,year_made FROM product WHERE year_made >= ? AND year_made <= ?";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setInt(1, 2000);/*from  www . j av  a  2s .com*/
    prest.setInt(2, 2009);
    ResultSet rs = prest.executeQuery();
    while (rs.next()) {
        String mov_name = rs.getString(1);
        int mov_year = rs.getInt(2);
        count++;
        System.out.println(mov_name + "\t" + "- " + mov_year);
    }
    System.out.println("Number of records: " + count);
    prest.close();
    con.close();
}

From source file:com.khartec.waltz.jobs.ServerHarness.java

public static void main(String[] args) {

    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DIConfiguration.class);
    ServerInformationService serverInfoService = ctx.getBean(ServerInformationService.class);
    ServerInformationDao serverInfoDao = ctx.getBean(ServerInformationDao.class);
    DSLContext dsl = ctx.getBean(DSLContext.class);

    IdSelectionOptions options = ImmutableIdSelectionOptions.builder()
            .entityReference(ImmutableEntityReference.builder().kind(EntityKind.ORG_UNIT).id(10).build())
            .scope(HierarchyQueryScope.CHILDREN).build();

    for (int i = 0; i < 5; i++) {
        HarnessUtilities.time("stats", () -> serverInfoService.findStatsForAppSelector(options));
    }//from  w  w  w  .  j av a2  s.  c  o m

    String sql = "\n" + "select \n" + "  coalesce(\n"
            + "    sum(case [server_information].[is_virtual] when 1 then 1\n"
            + "                                               else 0\n" + "        end), \n"
            + "    0) [virtual_count], \n" + "  coalesce(\n"
            + "    sum(case [server_information].[is_virtual] when 1 then 0\n"
            + "                                               else 1\n" + "        end), \n"
            + "    0) [physical_count]\n" + "from [server_information]\n"
            + "where [server_information].[asset_code] in (\n" + "  select [application].[asset_code]\n"
            + "  from [application]\n" + "  where [application].[id] in (\n" + "    select [application].[id]\n"
            + "    from [application]\n" + "    where [application].[organisational_unit_id] in (\n"
            + "      130, 260, 70, 200, 10, 140, 270, 80, 210, 20, 150, 280, 90, 220, 30, 160, \n"
            + "      290, 100, 230, 40, 170, 300, 110, 240, 50, 180, 120, 250, 60, 190\n" + "    )\n" + "  )\n"
            + ");\n";

    FunctionUtilities.time("raw q", () -> {
        dsl.connection(conn -> {
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getBigDecimal(1) + " - " + rs.getBigDecimal(2));
            }
        });
        return null;
    });

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Connection con = null;/*from  w  ww . ja v a  2 s . c om*/
    PreparedStatement prepstmt = con.prepareStatement("select Name, Addr from Employee where Id = ?");
    prepstmt.setString(1, "1");

    ResultSet rs;
    rs = prepstmt.executeQuery();

    boolean found = rs.next();
    if (found)
        System.out.println(rs.getString(1));
    prepstmt.close();

}

From source file:DynaBeansExampleV2.java

public static void main(String args[]) throws Exception {

    Connection conn = getConnection();
    PreparedStatement ps = conn
            .prepareStatement("SELECT * from movie, person " + "WHERE movie.director = person.Id");
    ResultSet rs = ps.executeQuery();

    ResultSetDynaClass rsdc = new ResultSetDynaClass(rs);

    Iterator itr = rsdc.iterator();
    while (itr.hasNext()) {
        DynaBean bean = (DynaBean) itr.next();
        System.err.println(bean.get("title"));
    }//from   w  ww. j av  a 2  s. co  m

    conn.close();
}