up previous next contents
Next: getXXX methods Up: Databases and JDBC Previous: Connecting to the database   Contents

Statements

  • A Statement object is used to send an SQL statement to the DBMS.
  • Create a Statement object and then execute it, using the proper execute method:
    • For SELECT statements use executeQuery.
    • For statements that create or modify tables use executeUpdate.
  • Example:
      Statement stmt = conn.createStatement();
      String query = "SELECT username, password FROM user";
      ResultSet rs = stmt.executeQuery(query);
      while ( rs.next() ) 
      {
        String user = rs.getString("username");
        String password = rs.getString("password");
        System.out.println( "Username: " + user );
        System.out.println( "Password: " + password );
      }
    


up previous next contents
Next: getXXX methods Up: Databases and JDBC Previous: Connecting to the database   Contents