Biblioteca Java - Blame information for rev 3
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
3 | mihai | 1 | /* |
2 | * StocTable.java | ||
3 | */ | ||
4 | package lab.scd.db.tableview; | ||
5 | |||
6 | import java.sql.*; | ||
7 | import java.util.ArrayList; | ||
8 | |||
9 | import javax.swing.table.AbstractTableModel; | ||
10 | |||
11 | import lab.scd.db.util.DBConfig; | ||
12 | |||
13 | /** | ||
14 | * Class created by @author Mihai HULEA at Mar 10, 2005. | ||
15 | * | ||
16 | * This class is part of the laborator4_db project. | ||
17 | * | ||
18 | */ | ||
19 | public class StocTable extends AbstractTableModel{ | ||
20 | |||
21 | Connection conn; | ||
22 | Statement s; | ||
23 | ResultSet rs; | ||
24 | |||
25 | ArrayList columnNames = new ArrayList(); | ||
26 | ArrayList data = new ArrayList(); | ||
27 | |||
28 | StocTable()throws Exception{ | ||
29 | //incarcare driver petru baza de date | ||
30 | Class.forName("com.mysql.jdbc.Driver"); | ||
31 | |||
32 | //conectare la baza de date | ||
33 | Connection conn = DriverManager.getConnection("jdbc:mysql://"+DBConfig.HOST+"/"+DBConfig.DATABASE+"?user="+DBConfig.USER+"&password="+DBConfig.PWD); | ||
34 | |||
35 | System.out.println("Conexiune la baza de date realizata."); | ||
36 | |||
37 | s = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); | ||
38 | |||
39 | updateCursor(); | ||
40 | |||
41 | getTableMetadata(); | ||
42 | } | ||
43 | |||
44 | private void updateCursor() throws SQLException{ | ||
45 | rs = s.executeQuery("SELECT * FROM STOC"); | ||
46 | } | ||
47 | |||
48 | private void getTableMetadata()throws SQLException{ | ||
49 | ResultSetMetaData rsm = rs.getMetaData(); | ||
50 | int col = rsm.getColumnCount(); | ||
51 | for(int i=1; i<=col; i++) { | ||
52 | columnNames.add(rsm.getColumnName(i)); | ||
53 | } | ||
54 | |||
55 | // Read the data | ||
56 | while(rs.next()) { | ||
57 | ArrayList row = new ArrayList(); | ||
58 | data.add(row); | ||
59 | for(int j=1; j<=col; j++) { | ||
60 | |||
61 | Object o = rs.getObject(j); | ||
62 | if(o!=null) | ||
63 | row.add(o.toString()); | ||
64 | else | ||
65 | row.add("NULL"); | ||
66 | } | ||
67 | } | ||
68 | } | ||
69 | |||
70 | |||
71 | public String getColumnName(int c) { | ||
72 | return (String)columnNames.get(c); | ||
73 | } | ||
74 | |||
75 | public int getColumnCount() { | ||
76 | return columnNames.size(); | ||
77 | } | ||
78 | |||
79 | public Object getValueAt(int r,int c) { | ||
80 | ArrayList row = (ArrayList)data.get(r); | ||
81 | return (String)row.get(c); | ||
82 | } | ||
83 | |||
84 | public int getRowCount() { | ||
85 | return data.size(); | ||
86 | } | ||
87 | |||
88 | |||
89 | } |