Biblioteca Java - Blame information for rev 35
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
3 | mihai | 1 | /* |
2 | * TestModificaTabelPS.java | ||
3 | */ | ||
4 | package lab.scd.db.jdbc10; | ||
5 | |||
6 | import java.sql.Connection; | ||
7 | import java.sql.DriverManager; | ||
8 | import java.sql.PreparedStatement; | ||
9 | import java.sql.ResultSet; | ||
10 | import java.sql.Statement; | ||
11 | |||
12 | import lab.scd.db.util.DBConfig; | ||
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 | * |
19 | * Ilustreaza modificarea inregistrarilor unui tabel utilizand clasa | ||
20 | * PreparedStatement. Desi aceasta metoda de realizarea a update-ului asupra | ||
21 | * unui tabel presupune un numar mai mare de instructiuni decat metoda | ||
22 | * prezentata in aplicatia TestModificaTabel, aceasta poate fi de ajutor in | ||
23 | * situatiile in care se relizeaza mai multe modificari asupra tabelelor, | ||
24 | * folosindu-se bucle for sau while. | ||
25 | * | ||
26 | * 1.Modificati aplicatia astfel incat numele produsului ce se doreste a fi | ||
27 | * modificat si valoare noului nume sa fie citite de la tastatura. | ||
3 | mihai | 28 | */ |
29 | public class TestModificaTabelPS { | ||
30 | |||
31 | public static void main(String[] args) { | ||
35 | mihai | 32 | try { |
33 | |||
34 | Class.forName("org.apache.derby.jdbc.ClientDriver"); | ||
35 | |||
3 | mihai | 36 | //conectare la baza de date |
35 | mihai | 37 | Connection conn = DriverManager.getConnection("jdbc:derby://" + DBConfig.HOST + "/" + DBConfig.DATABASE, DBConfig.USER, DBConfig.PWD); |
38 | |||
3 | mihai | 39 | System.out.println("Conexiune la baza de date realizata."); |
35 | mihai | 40 | |
3 | mihai | 41 | //contruieste un obiect de tip prepared statement |
35 | mihai | 42 | String updateString = "UPDATE STOC " |
43 | + "SET PROD = ?" | ||
44 | + "WHERE PROD LIKE ?"; | ||
3 | mihai | 45 | PreparedStatement stat = conn.prepareStatement(updateString); |
35 | mihai | 46 | |
47 | stat.setString(1, "produs modifcat PS"); | ||
48 | stat.setString(2, "produs 2"); | ||
49 | |||
3 | mihai | 50 | stat.executeUpdate(); |
35 | mihai | 51 | |
3 | mihai | 52 | //afiseaza datele din tabel modificate |
35 | mihai | 53 | Statement s = conn.createStatement(); |
54 | ResultSet rs = s.executeQuery("SELECT * FROM STOC"); | ||
55 | |||
56 | while (rs.next()) { | ||
3 | mihai | 57 | String pname = rs.getString("PROD"); |
58 | int ppret = rs.getInt("PRET"); | ||
35 | mihai | 59 | System.out.println("Produs:" + pname + " Pret:" + ppret); |
3 | mihai | 60 | }//.while |
35 | mihai | 61 | |
62 | } catch (Exception e) { | ||
63 | e.printStackTrace(); | ||
64 | } | ||
3 | mihai | 65 | } |
66 | } |