Biblioteca Java - Blame information for rev 3
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. | ||
15 | * | ||
16 | * This class is part of the laborator4_db project. | ||
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. | ||
21 | */ | ||
22 | public class BatchUpdate { | ||
23 | |||
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 | |||
30 | //conectare la baza de date | ||
31 | Connection con = DriverManager.getConnection("jdbc:mysql://"+DBConfig.HOST+"/"+DBConfig.DATABASE+"?user="+DBConfig.USER+"&password="+DBConfig.PWD); | ||
32 | System.out.println("Conexiune la baza de date realizata."); | ||
33 | |||
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){ | ||
53 | |||
54 | e.printStackTrace(); | ||
55 | //rollback transaction in caz de eroare in cadrul uneia dintre operatiile de ipdate | ||
56 | |||
57 | con.rollback(); | ||
58 | } | ||
59 | |||
60 | }catch(Exception e){ | ||
61 | |||
62 | e.printStackTrace(); | ||
63 | } | ||
64 | } | ||
65 | } |