このトピックでは、DB2eAppl.java および DB2eJavaCLP.java というサンプル・アプリケーションについて説明します。
サンプル 1: DB2eAppl.java
DB2eAppl.java は DB2 Everyplace の JDBC アプリケーションをコーディングする方法を具体的に示します。
DB2eAppl.java アプリケーションの主なステップは次のとおりです。
以下に示す DB2eAppl.java ソース・コードには、上で説明したステップがどこで使用されているのかを示すコメントが入っています。
import java.sql.*; //ステップ 1 public class DB2eAppl { public static void main(String[] args) { String driver = "com.ibm.db2e.jdbc.DB2eDriver"; String url = "jdbc:db2e:mysample"; try { Class.forName(driver); //ステップ 2 Connection con = DriverManager.getConnection(url);//ステップ 3 Statement st = con.createStatement();//ステップ 4 //表 employee の作成 //ステップ 5 st.executeUpdate("CREATE TABLE employee (EMPNO CHAR(6), FIRSTNAME VARCHAR(12))"); System.out.println("*** Created table: employee"); //employee へのレコードの追加 st.executeUpdate("INSERT INTO employee VALUES ('112233','John')"); st.executeUpdate("INSERT INTO employee VALUES ('445566','Mary')"); System.out.println("*** Inserted two records"); //結果の照会と表示 //ステップ 6 ResultSet rs = st.executeQuery("SELECT * FROM employee"); System.out.println("*** Query results:"); while (rs.next()) { System.out.print("EMPNO=" + rs.getString(1) + ", "); System.out.println("FIRSTNAME=" + rs.getString(2)); } //表 employee の削除 //ステップ 7 st.executeUpdate("Drop table employee"); System.out.println("*** Deleted table: employee"); rs.close(); st.close(); con.close(); } catch (SQLException sqlEx) { while(sqlEx != null) { System.out.println("[SQLException] " + "SQLState: " + sqlEx.getSQLState() + ", Message: " + sqlEx.getMessage() + ", Vendor: " + sqlEx.getErrorCode() ); sqlEx = sqlEx.getNextException(); } } catch (Exception ex) { ex.printStackTrace(); } } }
サンプル 2: DB2eJavaCLP.java
DB2eJavaCLP.java は DB2 Everyplace の Java コマンド行プロセッサーです。
関連したタスク
関連した概念