I am trying to get results from database and put them into a table in java. My database looks like this:
users[idu, username, password, ul]
teachers[idt, firstname, secondname, idu]
discipline[idd, name, idt]
class[idc, name, grade]
student[ids, idu, idc, firstname,secondname]
grades[idg, idd, ids, grade]
classskips[idcs, ids, date]
And my code for table look like this:
public void tablepanel(){
ConnectDB(serverip, serverport, serverdbname, serverusername, serverpassword);
Object[] columnNames = {"ID", "Firstname", "Secondname"};
ArrayList dataouter = new ArrayList();
try{
stgs = conn.createStatement();
getstudent = ("SELECT ids, firstname, secondname FROM student");
rsgs = stgs.executeQuery(getstudent);
rsmd = rsgs.getMetaData();
columnsNumber = rsmd.getColumnCount();
System.out.println(columnsNumber);
while(rsgs.next()){
ArrayList datainner = new ArrayList();
datainner.add(rsgs.getInt("ids"));
datainner.add(rsgs.getString("firstname"));
datainner.add(rsgs.getString("secondname"));
dataouter.add(datainner);
System.out.println(datainner);
}
System.out.println(dataouter);
}catch(Exception ex){
JOptionPane.showMessageDialog(null, "Error: " +ex);
}
JTable table = new JTable(dataouter, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane);
}
Now how I can add results from database to the table ?
Create a TableModal and set it to the table. Also create a Modal class may be Student
with id
, firstname
and secondname
fields and getters and setters for them. Create the TableModal
public class StudentsTableModal extends AbstractTableModel {
String[] columnNames = {"ID", "Firstname", "Secondname"};
Class[] types = { Integer.class, String.class, String.class };
private ArrayList<Student> data = new ArrayList<Student>();
public ArrayList<Student> getData() {
return data;
}
public void setData(ArrayList<Student> data) {
this.data = data;
fireTableDataChanged();
}
public int getRowCount() {
return data.size();
}
public int getColumnCount() {
return colNames.length;
}
@Override
public Class getColumnClass(int columnIndex) {
return types[columnIndex];
}
@Override
public String getColumnName(int column) {
return colNames[column];
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Student s = data.get(rowIndex);
switch (columnIndex) {
case 0:
return s.getId();
case 1:
return s.getFirstname();
case 2:
return s.getSecondname();
}
return null;
}
}
Now create an instance of this and set the table modal
StudentsTableModal modal=new StudentsTableModal();
table.setTableModal(modal);
When you retrieve the data from the table, store each row to a new Student
object and store that object to an ArrayList
, once done, call the setData
method on the modal with this data list.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.