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: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");

    Statement st = con.createStatement();
    ResultSet res = st.executeQuery("SELECT col_name  FROM mytable  ORDER BY col_name ASC");
    while (res.next()) {
        int col = res.getInt(1);
        System.out.println(col);/*www.ja v a 2  s .  com*/
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int sum = 0;/*from   www  . j a  va2  s  .  c  om*/
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");
    Statement st = con.createStatement();
    ResultSet res = st.executeQuery("SELECT SUM(col) FROM mytable");
    while (res.next()) {
        int c = res.getInt(1);
        sum = sum + c;
    }
    System.out.println("Sum of column = " + sum);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    System.out.println("Oracle driver registered");
    Connection conn = null;//from w w w .j  a va2s  . c om
    conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orclh", "scott", "tiger");

    Statement stmt = conn.createStatement();
    ResultSet r = stmt.executeQuery("Select * from emp");
    while (r.next()) {
        String str = r.getString("ename");
        System.out.println(str);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@//server.local:1521/prod", "scott",
            "tiger");
    conn.setAutoCommit(false);//from w  ww  .j  a v  a 2  s .c  om
    Statement stmt = conn.createStatement();
    ResultSet rset = stmt.executeQuery("select * from employee");
    while (rset.next()) {
        System.out.println(rset.getString(1));
    }
    stmt.close();
}

From source file:PrintResultSet.java

public static void main(String args[]) throws Exception {
    String query = "SELECT Name,Description,Qty,Cost FROM Stock";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Inventory");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(query);

    while (rs.next()) {
        System.out.print(rs.getString("Name") + "\t");
        System.out.print(rs.getString("Description") + "\t");
        System.out.print(rs.getInt("Qty") + "\t");
        System.out.println(rs.getFloat("Cost"));
    }/*from   w  ww .j a  va  2s. co m*/
}

From source file:JdbcDemo.java

public static void main(String args[]) throws Exception {
    String query = "SELECT Name,Description,Qty,Cost FROM Stock";

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Inventory");
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(query);
    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 ww w .j a va 2s .co  m*/
    con.close();
}

From source file:EmployeeShow.java

public static void main(String[] args) throws Exception {
    ImageIcon image;/*from   w  ww .  j a  va2s  .c  o m*/

    Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/c:\\employee");

    Statement s = con.createStatement();
    ResultSet rs = s.executeQuery("select photo from employee where name = 'Duke'");
    if (rs.next()) {
        Blob photo = rs.getBlob(1);
        ObjectInputStream ois = null;
        ois = new ObjectInputStream(photo.getBinaryStream());
        image = (ImageIcon) ois.readObject();
    }
    s.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "root";

    Class.forName(driver).newInstance();
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", user, pass);

    Statement st = con.createStatement();
    ResultSet res = st.executeQuery("SELECT * FROM  emp");
    while (res.next()) {
        int i = res.getInt("ID");
        String s = res.getString("name");
        System.out.println(i + "\t\t" + s);
    }/*w w w .  ja  v  a  2 s .  c o m*/
    con.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "root";

    Class.forName(driver);/*from www . j ava 2 s  . c o  m*/
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", user, pass);

    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM employee6");
    ResultSetMetaData md = rs.getMetaData();
    int col = md.getColumnCount();
    System.out.println("Number of Column : " + col);
    for (int i = 1; i <= col; i++) {
        String col_name = md.getColumnName(i);
        System.out.println(col_name);
    }
}

From source file:TestThinApp.java

public static void main(String args[]) throws ClassNotFoundException, SQLException {

    Class.forName("oracle.jdbc.driver.OracleDriver");
    //  DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");

    Statement stmt = conn.createStatement();
    ResultSet rset = stmt.executeQuery("select 'Hello Thin driver tester '||USER||'!' result from dual");
    while (rset.next())
        System.out.println(rset.getString(1));
    rset.close();/*from w  w  w . ja  va 2  s  .co m*/
    stmt.close();
    conn.close();
}