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