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 10, 2005. | 14 | * Class created by @author Mihai HULEA at Mar 10, 2005. | |
15 | * | - | ||
- | 15 | * | ||
16 | * This class is part of the laborator4_db project. | 16 | * This class is part of the laborator4_db project. | |
17 | * | - | ||
18 | * Pentru realizarea de modificari asupra bazei de date se foloseste metoda executeUpdate() | - | ||
19 | * din cadrul clasei Statement. | - | ||
20 | * | - | ||
- | 17 | * | ||
- | 18 | * Pentru realizarea de modificari asupra bazei de date se foloseste metoda | ||
- | 19 | * executeUpdate() din cadrul clasei Statement. | ||
- | 20 | * | ||
21 | */ | 21 | */ | |
22 | public class TestModificaTabel { | 22 | public class TestModificaTabel { | |
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 | - | |||
30 | //conectare la baza de date | - | ||
31 | Connection conn = 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 | //contruieste un obiect de tip statement | - | ||
35 | Statement stat = conn.createStatement(); | - | ||
36 | - | |||
37 | String updateString = "UPDATE STOC "+ | - | ||
38 | "SET PROD = 'prod modificat'"+ | - | ||
39 | "WHERE PROD LIKE 'PRODUS A';"; | - | ||
40 | - | |||
41 | //modifica inregistrarile din tabel | - | ||
42 | stat.executeUpdate(updateString); | - | ||
43 | - | |||
44 | //afiseaza datele din tabel modificate | - | ||
45 | ResultSet rs = stat.executeQuery("SELECT * FROM STOC;"); | - | ||
46 | - | |||
47 | while(rs.next()){ | - | ||
48 | String pname = rs.getString("PROD"); | - | ||
49 | int ppret = rs.getInt("PRET"); | - | ||
50 | System.out.println("Produs:"+pname+" Pret:"+ppret); | - | ||
51 | }//.while | - | ||
52 | - | |||
53 | }catch(Exception e){ | - | ||
- | 25 | try { | ||
- | 26 | |||
- | 27 | Class.forName("org.apache.derby.jdbc.ClientDriver"); | ||
- | 28 | |||
- | 29 | //conectare la baza de date | ||
- | 30 | Connection conn = DriverManager.getConnection("jdbc:derby://" + DBConfig.HOST + "/" + DBConfig.DATABASE, DBConfig.USER, DBConfig.PWD); | ||
- | 31 | System.out.println("Conexiune la baza de date realizata."); | ||
- | 32 | |||
- | 33 | //contruieste un obiect de tip statement | ||
- | 34 | Statement stat = conn.createStatement(); | ||
- | 35 | |||
- | 36 | String updateString = "UPDATE STOC " | ||
- | 37 | + "SET PROD = 'prod modificat'" | ||
- | 38 | + "WHERE PROD LIKE 'produs 1'"; | ||
- | 39 | |||
- | 40 | //modifica inregistrarile din tabel | ||
- | 41 | stat.executeUpdate(updateString); | ||
- | 42 | |||
- | 43 | //afiseaza datele din tabel modificate | ||
- | 44 | ResultSet rs = stat.executeQuery("SELECT * FROM STOC"); | ||
- | 45 | |||
- | 46 | while (rs.next()) { | ||
- | 47 | String pname = rs.getString("PROD"); | ||
- | 48 | int ppret = rs.getInt("PRET"); | ||
- | 49 | System.out.println("Produs:" + pname + " Pret:" + ppret); | ||
- | 50 | }//.while | ||
- | 51 | |||
- | 52 | } catch (Exception e) { | ||
54 | e.printStackTrace(); | 53 | e.printStackTrace(); | |
55 | } | 54 | } | |
56 | - | |||
- | 55 | |||
57 | }//.main | 56 | }//.main | |
58 | } | 57 | } |