Code example for PreparedStatement

Methods: addBatch, executeBatch, setString, setTimestamp

0
    private void addIntialData(DataSource ds) throws Exception {
        String sql = "INSERT INTO UM_USER (UM_USER_NAME, UM_USER_PASSWORD, UM_CHANGED_TIME) VALUES (?, ?, ?)";
        Connection dbCon = ds.getConnection();
        dbCon.setAutoCommit(false);
        PreparedStatement stmt = dbCon.prepareStatement(sql);
        stmt.setString(1, "system");
        stmt.setString(2, "topsecret");
        stmt.setTimestamp(3, new Timestamp((new Date().getTime())));
        stmt.addBatch();
        stmt.setString(1, "adminx");
        stmt.setString(2, "adminy");
        stmt.setTimestamp(3, new Timestamp((new Date().getTime())));
        stmt.addBatch();
        stmt.setString(1, "anonx");
        stmt.setString(2, "nopassx");
        stmt.setTimestamp(3, new Timestamp((new Date().getTime())));
        stmt.addBatch();
        stmt.setString(1, "saman");
        stmt.setString(2, "pass1");
        stmt.setTimestamp(3, new Timestamp((new Date().getTime())));
        stmt.addBatch();
        stmt.setString(1, "amara");
        stmt.setString(2, "pass2");
        stmt.setTimestamp(3, new Timestamp((new Date().getTime())));
        stmt.addBatch();
        stmt.setString(1, "sunil");
        stmt.setString(2, "pass3");
        stmt.setTimestamp(3, new Timestamp((new Date().getTime())));
        stmt.addBatch();
        int[] count = stmt.executeBatch();
        assertEquals(6, count.length);
         
 
        sql = "INSERT INTO UM_HYBRID_ROLE (UM_ROLE_NAME) VALUES (?)";
        stmt = dbCon.prepareStatement(sql);