アットウィキロゴ

JAVA DB04

import java.sql.*;

public class hello3{
  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 = ""; // ユーザ名
    String pwd = "";    // パスワード

    try {
      db = DriverManager.getConnection(url, usr, pwd);
      st = db.createStatement();

      st.executeUpdate("create table hello4 (item varchar(16))");
      st.executeUpdate("insert into hello4 values ('Hel wod')");
      st.executeUpdate("insert into hello4 values ('よう')");
      st.executeUpdate("insert into hello4 values ('AA')");

      rs = st.executeQuery("select * from hello4");
      if (rs != null) {
        while (rs.next()) {
          String item = rs.getString("item");
          System.out.println(item);
        }
      }
      rs.close();

      st.executeUpdate("drop table hello4");

      st.close();
      db.close();

    } catch (Exception ex) {ex.printStackTrace();}
  }
}
最終更新:2011年03月24日 18:44