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
|
public class BaseDonnees extends AbstractTableModel{
private static final long serialVersionUID = 1L;
Vector cols = new Vector ();
Vector data = new Vector();
String sql= null;
public Vector rows() {
try {
// Connect to the Database
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=ventes.mdb";
Class.forName(driver);
Connection connection = DriverManager.getConnection(url);
// Read data from a table
sql = "INSERT INTO Clients (IdClient,Civilité, Nom, Prenom, Adresse, CodePostal, Ville) VALUES ('1','Melk', 'dvk', " +
"'Patfal', 'krogfm', 'fkokkfdvmpkl', 'gghgiuhm')";
sql = "select * from clients";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
// Get column names
/*for (int i = 1; i <= columns; i++) {
columnNames.addElement(md.getColumnName(i));
}*/
// Get row data
while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(rs.getObject(i));
}
data.addElement(row);
}
rs.close();
stmt.close();
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.getStackTrace());
}
System.out.println(data);
return data;
}
public Vector columns()
{
try {
// Connect to the Database
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=ventes.mdb";
Class.forName(driver);
Connection connection = DriverManager.getConnection(url);
sql = "select * from clients";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
//Get column names
for (int i = 1; i <= columns; i++) {
cols.addElement(md.getColumnName(i));
}
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.getStackTrace());
}
System.out.println(cols);
return cols;
} |
Partager