Java活用生活

ログイン

オンライン状況

オンラインユーザー1人
ログインユーザー0人
登録ユーザー2人

カウンタ

COUNTER335459

日誌

MyDoc(備忘録) >> 記事詳細
2022/12/17

PreparedStatement addBatch

固定リンク | by:aramaki
PreparedStatement addBatch
----------------------------------------------
public class AddBatchTest {

private static final String URL = "jdbc:oracle:thin:@192.168.11.123:1521/pdborcl2";
private static final String username = "pdbadmin";
private static final String password = "aramaki3$$";
private static Connection connection = null;

public static void main(String[] args) {
PreparedStatement preparedStatement = null;
try {
createConnection();
connection.setAutoCommit(false);
String sql = "insert into MEMO(memo) values(?)";
preparedStatement = connection.prepareStatement(sql);
int num=1;
for (int i = 0; i < 30; i++) {
preparedStatement.setString(1, "メモ" + num);
preparedStatement.addBatch();
if(num % 10 == 0 || num == 30) {
preparedStatement.executeBatch();
}
num++;
}
connection.commit();
} catch (Exception e) {
try {
connection.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally {
if(preparedStatement!=null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}

public static void createConnection() throws SQLException {
connection = DriverManager.getConnection(URL, username, password);
}

}
23:07 | 投票する | 投票数(0) | コメント(0)
Copyright © Java活用生活 All Rights Reserved .