Skip to main content

This is documentation for Caché & Ensemble. See the InterSystems IRIS version of this content.Opens in a new tab

For information on migrating to InterSystems IRISOpens in a new tab, see Why Migrate to InterSystems IRIS?

クエリの実行

Caché への接続 (java.sql.Connection または com.intersys.objects.Database のいずれかで表される) を構築すると、データベースに対する SQL 文を実行できます。以下のメソッドは、SQL の SELECT 操作を実行します。このクエリでは、データベース内のすべての Contact インスタンスの Name 値と ContactType 値を検索します。このメソッドは、ResultSet を繰り返し処理して、NameContactType の値をそれぞれ表示します。このメソッドでは、java.sql.Connection を使用して、クエリの実行に使用する Statement オブジェクトを生成しています。


public class JDBCExamples {
   public static void printContactNames()
   throws SQLException, ClassNotFoundException{
      Connection conn = JDBCExamples.createConnection();
      Statement stmt = conn.createStatement();
      String query =
         "SELECT Name, ContactType FROM JavaTutorial.Contact";
      ResultSet rs=stmt.executeQuery(query);
      while (rs.next()){
         String name = rs.getString(1);
         String type = rs.getString(2);
         System.out.println("Name: " + name + " Type: " + type);
      }
      rs.close();
   }
}
FeedbackOpens in a new tab