JDBC Driver for Cloudant

Build 24.0.9060

Executing Statements

After Connecting from Code, you can execute SQL statements with the Statement class. See Using Prepared Statements to execute parameterized statements.

Select

To execute SQL statements that return data, use the Statement class' generic execute method or the executeQuery method. To return the results of a query, call the getResultSet method of the Statement.

The following example calls the execute method and iterates over the results returned:

Statement stat = conn.createStatement();
boolean ret = stat.execute("SELECT MovieRuntime, MovieRating FROM Movies");
if (ret) {
  ResultSet rs=stat.getResultSet();
  while(rs.next()) {
    for(int i=1;i<=rs.getMetaData().getColumnCount();i++) {
      System.out.println(rs.getMetaData().getColumnLabel(i) +"="+rs.getString(i));
    }
  }
}

Insert

To execute an INSERT, use the generic execute method or the executeUpdate method of the Statement class. For example:

Statement stat = conn.createStatement();
int count = stat.executeUpdate("INSERT INTO Movies (MovieRuntime, MovieRating) VALUES ('MovieRuntime','MovieRating')");

Update

To execute an update, use the generic execute method or the executeUpdate method of the Statement class. You can call the getUpdateCount method to obtain the count of affected rows. Or, call the executeUpdate method; this method returns the row count. For example:

Statement stat = conn.createStatement();
stat.execute("UPDATE Movies SET MovieRuntime = 'XXX' , MovieRating = 'YYY' WHERE Id = '70f6284d2a395396dbb3a60b4ce3d2d6'");
int count = stat.getUpdateCount();

Delete

To execute a delete, use the generic execute method or the executeUpdate method of the Statement class. You can call the getUpdateCount method to obtain the count of affected rows. Or, call the executeUpdate method; this method returns the row count. For example:

Statement stat = conn.createStatement();
stat.execute("DELETE FROM Movies WHERE Id = '70f6284d2a395396dbb3a60b4ce3d2d6'");
int count = stat.getUpdateCount();

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 24.0.9060