1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| import java.sql.*;
import java.util.*;
import javax.swing.table.*;
public class ClientsModelTable extends AbstractTableModel {
private List<ClientObject> mesClients = new ArrayList<ClientObject>();
private List<String> columnName = new ArrayList<String>();
private String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/MaBase.mdb";
private String user = "";
private String password = "";
public ClientsModelTable() {
columnName.add("CustomerID");
columnName.add("CompanyName");
columnName.add("ContactName");
columnName.add("ContactTitle");
columnName.add("Address");
columnName.add("City");
columnName.add("Region");
columnName.add("PostalCode");
columnName.add("Country");
columnName.add("Phone");
columnName.add("Fax");
Connection connection = null;
try {
connection = DriverManager.getConnection(url, user, password);
Statement stmt = connection.createStatement();
String sql = "SELECT * FROM Customers";
ResultSet result = stmt.executeQuery(sql);
while (result.next()) {
mesClients.add(new ClientObject(result.getString("CustomerId"), result.getString("CompanyName"),
result.getString("ContactName"), result.getString("ContactTitle"), result.getString("Address"),
result.getString("City"), result.getString("Region"), result.getString("PostalCode"),
result.getString("Country"), result.getString("Phone"), result.getString("Fax")));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override
public Class<?> getColumnClass(final int columnIndex) {
return String.class;
}
@Override
public int getColumnCount() {
return columnName.size();
}
@Override
public String getColumnName(final int columnIndex) {
return columnName.get(columnIndex);
}
@Override
public int getRowCount() {
return mesClients.size();
}
@Override
public Object getValueAt(final int rowIndex, final int columnIndex) {
switch (columnIndex) {
case 0:
return mesClients.get(rowIndex).getCustomerId();
case 1:
return mesClients.get(rowIndex).getCompanyName();
case 2:
return mesClients.get(rowIndex).getContactName();
case 3:
return mesClients.get(rowIndex).getContactTitles();
case 4:
return mesClients.get(rowIndex).getAddress();
case 5:
return mesClients.get(rowIndex).getCity();
case 6:
return mesClients.get(rowIndex).getRegion();
case 7:
return mesClients.get(rowIndex).getPostalCode();
case 8:
return mesClients.get(rowIndex).getCountry();
case 9:
return mesClients.get(rowIndex).getPhone();
case 10:
return mesClients.get(rowIndex).getFax();
}
return null;
}
@Override
public boolean isCellEditable(final int rowIndex, final int columnIndex) {
return false;
}
@Override
public void setValueAt(final Object aValue, final int rowIndex, final int columnIndex) {
ClientObject newCustomer = mesClients.get(rowIndex);
switch (columnIndex) {
case 0:
newCustomer.setCustomerId((String) aValue);
break;
case 1:
newCustomer.setCompanyName((String) aValue);
break;
case 2:
newCustomer.setContactName((String) aValue);
break;
case 3:
newCustomer.setContactTitles((String) aValue);
break;
case 4:
newCustomer.setAddress((String) aValue);
break;
case 5:
newCustomer.setCity((String) aValue);
break;
case 6:
newCustomer.setRegion((String) aValue);
break;
case 7:
newCustomer.setPostalCode((String) aValue);
break;
case 8:
newCustomer.setCountry((String) aValue);
break;
case 9:
newCustomer.setPhone((String) aValue);
break;
case 10:
newCustomer.setFax((String) aValue);
break;
}
}
public void addRow() {
int rowIndex = mesClients.size();
mesClients.add(new ClientObject("rien" + rowIndex, "rien", "rien",
"rien", "rien", "rien", "rien",
"rien", "rien", "rien", "rien"));
fireTableRowsInserted(rowIndex, rowIndex);
}
public void removeRow(final int rowIndex) {
mesClients.remove(rowIndex);
fireTableRowsDeleted(rowIndex, rowIndex);
}
} |
Partager