Biblioteca Java - Diff between revs 3 and 35
Subversion Repositories:
Rev 3 | Rev 35 | |||
---|---|---|---|---|
Line 9... | Line 9... | |||
9 | import lab.scd.db.util.DBConfig; | 9 | import lab.scd.db.util.DBConfig; | |
10 | 10 | |||
11 | /* | 11 | /* | |
12 | * TestTranzactii.java | 12 | * TestTranzactii.java | |
13 | */ | 13 | */ | |
14 | - | |||
15 | /** | 14 | /** | |
16 | * Class created by @author Mihai HULEA at Mar 10, 2005. | 15 | * Class created by @author Mihai HULEA at Mar 10, 2005. | |
17 | * | - | ||
- | 16 | * | ||
18 | * This class is part of the laborator4_db project. | 17 | * This class is part of the laborator4_db project. | |
19 | * | - | ||
- | 18 | * | ||
20 | */ | 19 | */ | |
21 | public class TestTranzactii { | 20 | public class TestTranzactii { | |
22 | - | |||
- | 21 | |||
23 | Connection conn; | 22 | Connection conn; | |
24 | Statement stat; | 23 | Statement stat; | |
25 | public TestTranzactii() throws Exception{ | - | ||
26 | Class.forName("com.mysql.jdbc.Driver"); | - | ||
27 | - | |||
- | 24 | |||
- | 25 | public TestTranzactii() throws Exception { | ||
- | 26 | //incarcare driver petru baza de date | ||
- | 27 | Class.forName("org.apache.derby.jdbc.ClientDriver"); | ||
- | 28 | |||
28 | //conectare la baza de date | 29 | //conectare la baza de date | |
29 | Connection conn = DriverManager.getConnection("jdbc:mysql://"+DBConfig.HOST+"/"+DBConfig.DATABASE+"?user="+DBConfig.USER+"&password="+DBConfig.PWD); | - | ||
- | 30 | conn = DriverManager.getConnection("jdbc:derby://" + DBConfig.HOST + "/" + DBConfig.DATABASE, DBConfig.USER, DBConfig.PWD); | ||
30 | System.out.println("Conexiune la baza de date realizata."); | 31 | System.out.println("Conexiune la baza de date realizata."); | |
31 | - | |||
- | 32 | |||
32 | stat = conn.createStatement(); | 33 | stat = conn.createStatement(); | |
33 | } | 34 | } | |
34 | - | |||
- | 35 | |||
35 | public void testTranzactie(boolean simulateError) { | 36 | public void testTranzactie(boolean simulateError) { | |
36 | try{ | - | ||
- | 37 | try { | ||
37 | //start tranzactie | 38 | //start tranzactie | |
38 | conn.setAutoCommit(false); | 39 | conn.setAutoCommit(false); | |
39 | - | |||
- | 40 | |||
40 | stat.executeUpdate("INSERT INTO STOC VALUES ('produs A' , 2500)"); | 41 | stat.executeUpdate("INSERT INTO STOC VALUES ('produs A' , 2500)"); | |
41 | - | |||
- | 42 | |||
42 | //simuleaza aparitia unei probleme | 43 | //simuleaza aparitia unei probleme | |
43 | if(simulateError == true) | - | ||
- | 44 | if (simulateError == true) { | ||
44 | throw new SQLException("Eroare update."); | 45 | throw new SQLException("Eroare update."); | |
45 | - | |||
- | 46 | } | ||
- | 47 | |||
46 | stat.executeUpdate("INSERT INTO STOC VALUES ('produs B' , 7900)"); | 48 | stat.executeUpdate("INSERT INTO STOC VALUES ('produs B' , 7900)"); | |
47 | - | |||
- | 49 | |||
48 | //sfarsit tranzactie | 50 | //sfarsit tranzactie | |
49 | conn.commit(); | 51 | conn.commit(); | |
50 | - | |||
51 | }catch(SQLException ex){ | - | ||
52 | - | |||
- | 52 | |||
- | 53 | } catch (SQLException ex) { | ||
- | 54 | |||
53 | ex.printStackTrace(); | 55 | ex.printStackTrace(); | |
54 | try { | 56 | try { | |
55 | conn.rollback(); | 57 | conn.rollback(); | |
56 | } catch (SQLException e) {e.printStackTrace();} | - | ||
57 | - | |||
- | 58 | } catch (SQLException e) { | ||
- | 59 | e.printStackTrace(); | ||
- | 60 | } | ||
- | 61 | |||
58 | } | 62 | } | |
59 | - | |||
- | 63 | |||
60 | }//. | 64 | }//. | |
61 | - | |||
62 | public void afiseazaTabel() throws SQLException{ | - | ||
63 | ResultSet rs = stat.executeQuery("SELECT * FROM STOC;"); | - | ||
64 | - | |||
65 | while(rs.next()){ | - | ||
- | 65 | |||
- | 66 | public void afiseazaTabel() throws SQLException { | ||
- | 67 | ResultSet rs = stat.executeQuery("SELECT * FROM STOC"); | ||
- | 68 | |||
- | 69 | while (rs.next()) { | ||
66 | String pname = rs.getString("PROD"); | 70 | String pname = rs.getString("PROD"); | |
67 | int ppret = rs.getInt("PRET"); | 71 | int ppret = rs.getInt("PRET"); | |
68 | System.out.println("Produs:"+pname+" Pret:"+ppret); | - | ||
- | 72 | System.out.println("Produs:" + pname + " Pret:" + ppret); | ||
69 | } | 73 | } | |
70 | - | |||
- | 74 | |||
71 | } | 75 | } | |
72 | - | |||
- | 76 | |||
73 | public static void main(String[] args) { | 77 | public static void main(String[] args) { | |
74 | try{ | - | ||
75 | - | |||
- | 78 | try { | ||
- | 79 | |||
76 | TestTranzactii t = new TestTranzactii(); | 80 | TestTranzactii t = new TestTranzactii(); | |
77 | - | |||
- | 81 | |||
78 | t.testTranzactie(true); | 82 | t.testTranzactie(true); | |
- | 83 | |||
- | 84 | t.afiseazaTabel(); | ||
- | 85 | |||
- | 86 | t.testTranzactie(false); | ||
79 | 87 | |||
80 | t.afiseazaTabel(); | 88 | t.afiseazaTabel(); | |
81 | 89 | |||
82 | }catch(Exception e){ | - | ||
- | 90 | } catch (Exception e) { | ||
83 | e.printStackTrace(); | 91 | e.printStackTrace(); | |
84 | } | 92 | } | |
85 | } | 93 | } | |
86 | } | 94 | } |