Tableau avec résultat SQL d'une BDD Access
J'ai un fichier que j'ai essayé de bricoler de façon à pouvoir avoir un tableau affichant les colonnes d'une table de ma BDD Access mais il me reste un type d'erreur que je n'arrive pas à supprimer: cannot find symbol method
JCreator me l'affiche à quelques reprises et je n'arrive pas à les résoudre: à chaque fois que je fais import java.lang.Object ou java.lang.String comme je crois comprendre, les erreurs subsistent. Et si je précise ces méthodes pour chacun des objets il me dit qu'il me manque un ";" alors que je vois pas ou en mettre dans ce cas la.
J'essaie d'avoir un code où je peux avoir les requêtes SQL que je veux au lieu d'un code qui me scanne toutes les tables de ma BDD.
Voici mon code ou j'ai pas mal c/c en effet, d'ou les commentaires en anglais qui m'aide pas mal.
Code:
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
|
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;
public class Super extends JPanel {
private boolean DEBUG = false;
public Super() {
super(new GridLayout(1,9));
JTable atable;
// Connexion
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String filename = "java.mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
database+= filename.trim() + ";DriverID=22;READONLY=true}";
Connection con = DriverManager.getConnection( database ,"","");
Statement s = con.createStatement();
// get TableModel definition to which the resultset has to be displayed
Super aModel = new Super();
//get the metadata and column count from the resultset
//Allocate an array to hold the column names
ResultSet rs = s.executeQuery("select * from Utilisateur");
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
//Allocate an array to hold the column names
String[] columnNames =
{"IdUser","Login","Password","Nom","Prenom","Adresse","CodePostal","Ville","Pays"};
// String[] columnNames = new String[colCount];
//get the column names from the resultset
for (int i=0; i< colCount; i++)
columnNames[i] = rsmd.getColumnLabel(i+1);
//set the column headers
aModel.setColumnIdentifiers(columnNames);
// Loop through the ResultSet and transfer in the Model
while(rs.next()){
//Allocate a new row
Object[] data = new Object[colCount];
//Add the column values to the row
for(int i=0;i<colCount;i++){
data[i]=rs.getObject(i+1);
}
//add the row to the table model
aModel.addRow(data);
}// Fin de la boucle while
//place the table model into the table for displaying
atable.setModel(aModel);
//close the resultset
closeResultSet(rs);
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
if (DEBUG) {
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
printDebugData(table);
}
});
}
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
// s.close();
// con.close();
// System.out.println("Connexion à la base de données stoppée.");
}// FIN DU TRY
catch (Exception e) {
System.out.println("Error: " + e);}
// Fin de la connexion
}
private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel();
System.out.println("Value of data: ");
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + model.getValueAt(i, j));
}
System.out.println();
}
System.out.println("--------------------------");
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Super");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
Super newContentPane = new Super();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
} |
L'une des erreurs se situe à ce niveau:
Code:
aModel.setColumnIdentifiers(columnNames);