Biblioteca Java - Blame information for rev 35
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
3 | mihai | 1 | /* |
2 | * BatchUpdate.java | ||
3 | */ | ||
4 | package lab.scd.db.jdb20; | ||
5 | |||
6 | import java.sql.Connection; | ||
7 | import java.sql.DriverManager; | ||
8 | import java.sql.ResultSet; | ||
9 | import java.sql.Statement; | ||
10 | |||
11 | import lab.scd.db.util.DBConfig; | ||
12 | |||
13 | /** | ||
14 | * Class created by @author Mihai HULEA at Mar 13, 2005. | ||
35 | mihai | 15 | * |
3 | mihai | 16 | * This class is part of the laborator4_db project. |
17 | * | ||
35 | mihai | 18 | * JDBC 2.0 a introdus posibilitatea de a trimite catre baza de date un set de |
19 | * mai multe updateuri ca un set. Acest procedeu este mult mai eficient decat | ||
20 | * trimiterea fiecarei comenzi de update separat. | ||
3 | mihai | 21 | */ |
22 | public class BatchUpdate { | ||
23 | |||
24 | public static void main(String[] args) { | ||
35 | mihai | 25 | try { |
26 | |||
27 | //incarcare driver petru baza de date | ||
28 | Class.forName("org.apache.derby.jdbc.ClientDriver"); | ||
29 | |||
3 | mihai | 30 | //conectare la baza de date |
35 | mihai | 31 | Connection conn = DriverManager.getConnection("jdbc:derby://" + DBConfig.HOST + "/" + DBConfig.DATABASE, DBConfig.USER, DBConfig.PWD); |
3 | mihai | 32 | System.out.println("Conexiune la baza de date realizata."); |
35 | mihai | 33 | |
3 | mihai | 34 | //start tranzactie |
35 | mihai | 35 | conn.setAutoCommit(false); |
36 | |||
37 | try { | ||
38 | |||
39 | Statement stmt = conn.createStatement(); | ||
40 | |||
41 | stmt.addBatch("INSERT INTO STOC " | ||
42 | + "VALUES('Amaretto1', 9898)"); | ||
43 | stmt.addBatch("INSERT INTO STOC " | ||
44 | + "VALUES('Hazelnut1', 900)"); | ||
45 | stmt.addBatch("INSERT INTO STOC " | ||
46 | + "VALUES('Amaretto_decaf', 77)"); | ||
47 | stmt.addBatch("INSERT INTO STOC " | ||
48 | + "VALUES('Bygtoac1', 69)"); | ||
49 | |||
50 | int[] updateCounts = stmt.executeBatch(); | ||
3 | mihai | 51 | |
35 | mihai | 52 | conn.commit(); |
53 | |||
54 | } catch (Exception e) { | ||
55 | |||
3 | mihai | 56 | e.printStackTrace(); |
57 | //rollback transaction in caz de eroare in cadrul uneia dintre operatiile de ipdate | ||
35 | mihai | 58 | |
59 | conn.rollback(); | ||
3 | mihai | 60 | } |
35 | mihai | 61 | |
62 | } catch (Exception e) { | ||
63 | |||
3 | mihai | 64 | e.printStackTrace(); |
65 | } | ||
35 | mihai | 66 | } |
3 | mihai | 67 | } |