アットウィキロゴ

jdbc02

import java.sql.*;

public class hello2{
  public static void main (String args[]) {
    Connection db = null; // DB接続オブジェクト
    Statement  st = null; // SQL文オブジェクト
    ResultSet  rs = null; // 問合せ結果オブジェクト

    String url = "jdbc:derby:db;create=true"; // URL
    String usr = "joe"; // ユーザ名
    String pwd = "";    // パスワード

    try {
      System.out.println("Connecting to " + url);
      db = DriverManager.getConnection(url, usr, pwd);
      st = db.createStatement();

      // 表の作成
      st.executeUpdate("create table hellotbl (item varchar(16))");

      // データの登録
      st.executeUpdate("insert into hellotbl values ('Hello world!')");
      st.executeUpdate("insert into hellotbl values ('ようこそ!')");

      // データの検索
      rs = st.executeQuery("select * from hellotbl");
      if (rs != null) {
        while (rs.next()) {
          String item = rs.getString("item");
          System.out.println(item);
        }
      }
      rs.close();

      // 表の削除
      st.executeUpdate("drop table hellotbl");

      // データベース切断
      st.close();
      db.close();

    // エラー処理
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}
最終更新:2011年03月24日 18:25