Statement: executeQuery(String sql) : Statement « java.sql « Java by API






Statement: executeQuery(String sql)

 
/* 
 * */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;

public class Main {
  public static void main(String[] args) throws Exception {
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
        ResultSet.CONCUR_UPDATABLE);

    String sqlQuery = "SELECT uid, name, duration from EVENTS";

    ResultSet rs = stmt.executeQuery(sqlQuery);

    while (rs.next()) {
      rs.updateString("Name", "new Name");

      rs.updateRow();
    }

    rs.first();
    while (rs.next()) {
      String name = rs.getString(2);
      Timestamp hireDate = rs.getTimestamp(5);
      System.out.println("Name: " + name + " Hire Date: " + hireDate);
    }

    rs.close();

  }

  static Connection conn;

  static Statement st;

  static {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";

      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");

      st = conn.createStatement();

    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}

           
         
  








Related examples in the same category

1.Statement.EXECUTE_FAILED
2.Statement.RETURN_GENERATED_KEYS
3.Statement.SUCCESS_NO_INFO
4.Statement: addBatch(String sql)
5.WebRowSet: execute(Connection conn)
6.Statement: executeBatch()
7.Statement: getFetchSize()
8.Statement: getGeneratedKeys()
9.Statement: getMaxFieldSize()
10.Statement: getMaxRows()
11.Statement: getQueryTimeout()
12.WebRowSet: setCommand(String cmd)
13.Statement: setFetchSize(int rows)