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