Biblioteca Java - Diff between revs 3 and 35
Subversion Repositories:
Rev 3 | Rev 35 | |||
---|---|---|---|---|
Line 10... | Line 10... | |||
10 | 10 | |||
11 | import lab.scd.db.util.DBConfig; | 11 | import lab.scd.db.util.DBConfig; | |
12 | 12 | |||
13 | /** | 13 | /** | |
14 | * Class created by @author Mihai HULEA at Mar 13, 2005. | 14 | * Class created by @author Mihai HULEA at Mar 13, 2005. | |
15 | * | - | ||
- | 15 | * | ||
16 | * This class is part of the laborator4_db project. | 16 | * This class is part of the laborator4_db project. | |
17 | * | 17 | * | |
18 | * JDBC 2.0 a introdus posibilitatea de a trimite catre baza de date un set de mai | - | ||
19 | * multe updateuri ca un set. Acest procedeu este mult mai eficient decat trimiterea | - | ||
20 | * fiecarei comenzi de update separat. | - | ||
- | 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. | ||
21 | */ | 21 | */ | |
22 | public class BatchUpdate { | 22 | public class BatchUpdate { | |
23 | 23 | |||
24 | public static void main(String[] args) { | 24 | public static void main(String[] args) { | |
25 | try{ | - | ||
26 | - | |||
27 | // incarcare driver petru baza de date | - | ||
28 | Class.forName("com.mysql.jdbc.Driver"); | - | ||
29 | - | |||
- | 25 | try { | ||
- | 26 | |||
- | 27 | //incarcare driver petru baza de date | ||
- | 28 | Class.forName("org.apache.derby.jdbc.ClientDriver"); | ||
- | 29 | |||
30 | //conectare la baza de date | 30 | //conectare la baza de date | |
31 | Connection con = DriverManager.getConnection("jdbc:mysql://"+DBConfig.HOST+"/"+DBConfig.DATABASE+"?user="+DBConfig.USER+"&password="+DBConfig.PWD); | - | ||
- | 31 | Connection conn = DriverManager.getConnection("jdbc:derby://" + DBConfig.HOST + "/" + DBConfig.DATABASE, DBConfig.USER, DBConfig.PWD); | ||
32 | System.out.println("Conexiune la baza de date realizata."); | 32 | System.out.println("Conexiune la baza de date realizata."); | |
33 | - | |||
- | 33 | |||
34 | //start tranzactie | 34 | //start tranzactie | |
35 | con.setAutoCommit(false); | - | ||
36 | - | |||
37 | try{ | - | ||
38 | - | |||
39 | Statement stmt = con.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(); | - | ||
51 | - | |||
52 | }catch(Exception e){ | - | ||
- | 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(); | ||
53 | 51 | |||
- | 52 | conn.commit(); | ||
- | 53 | |||
- | 54 | } catch (Exception e) { | ||
- | 55 | |||
54 | e.printStackTrace(); | 56 | e.printStackTrace(); | |
55 | //rollback transaction in caz de eroare in cadrul uneia dintre operatiile de ipdate | 57 | //rollback transaction in caz de eroare in cadrul uneia dintre operatiile de ipdate | |
56 | - | |||
57 | con.rollback(); | - | ||
- | 58 | |||
- | 59 | conn.rollback(); | ||
58 | } | 60 | } | |
59 | - | |||
60 | }catch(Exception e){ | - | ||
61 | - | |||
- | 61 | |||
- | 62 | } catch (Exception e) { | ||
- | 63 | |||
62 | e.printStackTrace(); | 64 | e.printStackTrace(); | |
63 | } | 65 | } | |
64 | } | - | ||
- | 66 | } | ||
65 | } | 67 | } |