| Apdesigner プログラマーズガイド |
目次
索引
![]()
|
| 第3部 Apdesigner利用編 | > 第12章 データベース処理を作成する |
JDBCは、Javaが実装する標準のデータベースアクセスインタフェースです。データベース管理システム(DBMS)に対する基本的な機能を提供します。
詳細については、"Java Development Kit オンラインマニュアル"を参照してください。
JDBCを使用した記述例を以下に示します。
このアプリケーションには以下の機能があります。

private Connection connection;
try
{
// 使用するJDBCドライバをロードする
Class.forName("com.fujitsu.symfoware.jdbc.SYMDriver");
// データベースに接続する
connection = DriverManager.getConnection("jdbc:symford://HOST01:2050/DBSPACE01","USER01","PASSWORD01");
}
catch (SQLException ex)
{
ex.printStackTrace();
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
try
{
// データベースとの接続解除
connection.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
try
{
PreparedStatement pstmtForSelect;
String SELECT = "SELECT * FROM CHIEF.MEMBER WHERE ID = ?";
ResultSet selectCursor;
// PreparedStatementオブジェクトの作成
pstmtForSelect = connection.prepareStatement(SELECT);
// パラメタ(検索条件)の設定
pstmtForSelect.setInt(1,Integer.parseInt(jTextFieldId.getText()));
// ResultSetオブジェクトの作成
selectCursor = pstmtForSelect.executeQuery();
// 行を読み込む
selectCursor.next();
// データ表示
jTextFieldName.setText(selectCursor.getString("NAME"));
jTextFieldId.setText(selectCursor.getString("ID"));
jTextFieldAge.setText(selectCursor.getString("AGE"));
jTextFieldSalary.setText(selectCursor.getString("SALARY"));
// ResultSetオブジェクトのカーソルをクローズする
selectCursor.close();
pstmtForSelect.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
try
{
PreparedStatement pstmtForUpdate;
String UPDATE = "UPDATE CHIEF.MEMBER SET NAME=?,AGE=?,SALARY=? WHERE ID=?";
// PreparedStatementオブジェクトの作成
pstmtForUpdate = connection.prepareStatement(UPDATE);
// パラメタ(更新値、更新条件)の設定
pstmtForUpdate.setString(1,jTextFieldName.getText());
pstmtForUpdate.setShort(2,Short.parseShort(jTextFieldAge.getText()));
pstmtForUpdate.setLong(3,Long.parseLong(jTextFieldSalary.getText()));
pstmtForUpdate.setInt(4,Integer.parseInt(jTextFieldId.getText()));
// 行の更新
pstmtForUpdate.executeUpdate();
pstmtForUpdate.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
try
{
PreparedStatement pstmtForDelete;
String DELETE = "DELETE FROM CHIEF.MEMBER WHERE ID=?";
// PreparedStatementオブジェクトの作成
pstmtForDelete = connection.prepareStatement(DELETE);
// パラメタ(削除条件)の設定
pstmtForDelete.setInt(1,Integer.parseInt(jTextFieldId.getText()));
// 行の削除
pstmtForDelete.executeUpdate();
pstmtForDelete.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
try
{
PreparedStatement pstmtForInsert;
String INSERT = "INSERT INTO CHIEF.MEMBER VALUES(?,?,?,?)";
// PreparedStatementオブジェクトの作成
pstmtForInsert = connection.prepareStatement(INSERT);
// パラメタ(挿入値)の設定
pstmtForInsert.setString(1,jTextFieldName.getText());
pstmtForInsert.setInt(2,Integer.parseInt(jTextFieldId.getText()));
pstmtForInsert.setShort(3,Short.parseShort(jTextFieldAge.getText()));
pstmtForInsert.setLong(4,Long.parseLong(jTextFieldSalary.getText()));
// 行の挿入
pstmtForInsert.executeUpdate();
pstmtForInsert.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
DBアクセスクラス生成ウィザードを使うことにより、JDBCによるDBアクセス処理をクラスとして自動生成することができます。
DBアクセスクラス生成ウィザードは、メニューバーから[ファイル] > [新規] > [その他]を選択し、[Java] > [ソース]から[DBアクセスソース]を選択します。

Apdesignerのウィザードでは、Pure JavaのJDBCドライバ(タイプ4)の使用を推奨しています。そのため、実行時にRDB2_TCP連携で接続する場合には、生成されたソースのURL記述を変更する必要があります。
DBアクセスクラスでは、ウィザードで指定された情報にもとづいて検索、挿入、削除、更新の処理がメソッドとして、処理対象となるデータベースのカラムはクラスのフィールドとして展開されます。そのため、DBアクセスクラスを使う場合には、クラスのフィールドに対して値の設定、取得を行いながらメソッドを呼び出すことでDBアクセス処理を行うことになります。

そのほかの特徴には以下があります。
目次
索引
![]()
|