|
import java.sql.*;
public class Update2 {
public static void main(String[] args) throws SQLException {
Connection con = null;
PreparedStatement stm = null;
try {
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
con = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:ORCL","test","test");
// 自動コミットを無効にする。
con.setAutoCommit(false);
// PrepareStatement取得
stm = con.prepareStatement(
"update emp set name = 'なまえ5' where emp_id = ?");
// 列「emp_id」の値を設定
stm.setString(1,"E03");
// SQLの実行
int cnt = stm.executeUpdate();
// 更新件数の表示
System.out.println(cnt);
// コミットする。
con.commit();
} catch (SQLException ex) {
// DBアクセスエラーの場合は、ロールバックする。
con.rollback();
} finally {
if ( stm != null ) {
// Statement クローズ
stm.close();
}
if ( con != null ) {
// コネクションクローズ
con.close();
}
}
}
} |