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[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/testdb", "root", "");

    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
    ResultSet resultSet = statement.executeQuery("SELECT * FROM products");

    if (resultSet.isAfterLast()) {
        System.out.println("You are at the beginning of the result set.");
    }/*from   w ww.ja v a2  s.  com*/
    connection.close();
}

From source file:Main.java

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

    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
    ResultSet resultSet = statement.executeQuery("SELECT * FROM products");

    if (resultSet.isBeforeFirst()) {
        System.out.println("beginning");
    }//w w w  . ja  v  a2s  . c om
    connection.close();
}

From source file:Main.java

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

    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
    ResultSet resultSet = statement.executeQuery("SELECT * FROM products");

    if (resultSet.isBeforeFirst()) {
        System.out.println("isBeforeFirst.");
    }/*from w ww. ja  v  a 2  s  . c  o  m*/
    connection.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);// w ww.  j av a 2 s  .  com

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);

    // Create a scrollable result set
    Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");

    // Move to the end of the result set
    resultSet.last();

    // Get the row number of the last row which is also the row count
    int rowCount = resultSet.getRow();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);//w w  w.j  av a2s .co  m

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);
    Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");

    // Move cursor to the row to update
    resultSet.first();

    // Update the value of column col_string on that row
    resultSet.updateString("col_string", "new data");

    // Discard the update to the row
    resultSet.cancelRowUpdates();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);/*from  www.  j  av  a 2  s  .c o m*/

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);
    Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");

    // Move cursor to the "insert row"
    resultSet.moveToInsertRow();

    // Set values for the new row.
    resultSet.updateString("col_string", "new data");

    // Insert the row
    resultSet.insertRow();

}

From source file:Main.java

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

    Class.forName(DRIVER);//from w ww . jav  a2s.c  o  m
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    Statement statement = connection.createStatement();
    String query = "SELECT stock_id, name, price FROM stocks";
    ResultSet resultSet = statement.executeQuery(query);

    ResultSetMetaData metadata = resultSet.getMetaData();
    int precision = metadata.getPrecision(3);
    int scale = metadata.getScale(3);

    System.out.println("Precision: " + precision);
    System.out.println("Scale    : " + scale);
}

From source file:Main.java

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

    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
    ResultSet resultSet = statement.executeQuery("SELECT * FROM products");
    while (resultSet.next()) {
        String productCode = resultSet.getString("product_code");

        int row = resultSet.getRow();

        System.out.println(row + ". " + productCode);
    }/*  w  ww .  jav a2  s  .co  m*/
    connection.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getOracleConnection();
    Statement stmt = null;
    ResultSet rs = null;//from ww  w .  j a  v  a2  s . co  m
    stmt = conn.createStatement();
    //only for Oracle
    rs = stmt.executeQuery("select object_name from user_objects where object_type = 'VIEW'");

    while (rs.next()) {
        String tableName = rs.getString(1);
        System.out.println("viewName=" + tableName);
    }

    stmt.close();
    conn.close();
}

From source file:Main.java

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

    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
            ResultSet.CONCUR_READ_ONLY);
    ResultSet resultSet = statement.executeQuery("SELECT * FROM products");
    resultSet.afterLast();//from w  w  w. j  av  a2s.c o m
    while (resultSet.previous()) {
        String productCode = resultSet.getString("product_code");
        String productName = resultSet.getString("product_name");
        int quantity = resultSet.getInt("quantity");
        double price = resultSet.getDouble("price");

        System.out.println(productCode + "\t" + productName + "\t" + quantity + "\t" + price);
    }
    connection.close();
}