Reputation: 107
public static DefaultTableModel localQuery(String searchTerm){
List<String[]> result = new ArrayList<String[]>();
String[] rowResult = new String[7];
Vector<String[]> rows = new Vector<String[]>();
Vector<String> columnNames = new Vector<String>();
Statement stmt = null;
try {
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(searchTerm);
while(rs.next()){
rowResult = new String[13];
String currentId= rs.getString("CurrentId");
String manufacturer = rs.getString("Constructor");
String type = rs.getString("ACType");
String series = rs.getString("Series");
String index = rs.getString("KeyNo");
String operator = rs.getString("Operator");
String baseCountry = rs.getString("Home_Country");
String baseAirport = rs.getString("Home_Airfield");
String cn = rs.getString("Con");
String lineNo = rs.getString("LineNum");
String hex = rs.getString("Hexcode");
String selcal=rs.getString("Selcal");
String acName = rs.getString("ACName");
rowResult[0] = (currentId);
rowResult[1] = (manufacturer);
rowResult[2] = (type);
rowResult[3] = (series);
rowResult[4] = (operator);
rowResult[5] = (baseCountry);
rowResult[6] = (baseAirport);
rowResult[7] = (cn);
rowResult[8] = (lineNo);
rowResult[9] = (hex);
rowResult[10] = (selcal);
rowResult[11] = (acName);
rowResult[12]= (index);
result.add(rowResult);
}
columnNames.add("Reg.");
columnNames.add("Manufacturer");
columnNames.add("Type");
columnNames.add("Series");
columnNames.add("Operator");
columnNames.add("Home Country");
columnNames.add("Home Airfield");
columnNames.add("C/N");
columnNames.add("Line #");
columnNames.add("Hex");
columnNames.add("Selcal");
columnNames.add("Aircraft Name");
columnNames.add("");
rs.close();
stmt.close();
//Convert from List<String[]> to a suitable vector for resultTable... Help!!!
DefaultTableModel resultTable = new DefaultTableModel(rows, columnNames);
return resultTable;
}
Hi! I'm trying this method to return a DefaultTableModel to use on a JTable. I'm struggling to format the data pulled from the database into a model friendly format. Take it easy on me, this is my third ever day of programming of any sort!
Thanks in advance :)
Upvotes: 1
Views: 4601
Reputation: 209102
You can get the column header dynamically using ResultSetMetaData
← API link
ResultSetMetaData rsMeta = resultSet.getMetaData();
int numberOfCols = rsMeta.getColumnCount();
Vector<String> columnNames = new Vector<>(); // your columns names
for (int i = 1; i <= numberOfCols; i++ ){
columnsNames.add(rsMeta.getColumnName(i));
}
You don't need the extra Vector
or the List
for the data. Just first declare your DefaultTableModel
with the constructor arguments ( Vector/Object[] colNames, int rows)
. Then just use the method addRow
from the DefaultTableModel
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
while (resultSet.next()) {
String data1 = resultSet.getString(1);
String data2 = resultSet.getString(2);
...
Object[] rowData = new Object[] { data1, data2, ... };
model.addRow(rowData);
}
return model;
Or, instead of 2., to make sure you're getting the correct number of columns, you could use the Vector inside the while
loop and loop through the column data.
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
while (resultSet.next()) {
Vector<String> row = new Vector<>();
for (int i = 1; i <= numberOfCols; i++) {
row.add(resultSet.getString(i));
}
model.addRow(row);
}
return model;
See more methods and constructors see DefaultTableModel API
Upvotes: 1