java


JTable with data from array


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.

Related Links

How to resolve this conflicting of two beans with same name in maven project?
Socket url connect/disconnect every time while passing message form android
improperly called by android.widget.LinearLayout - Activity can't launch
JavaMail API Sending email via Gmail SMTP
Maven build fails on Jenkins
ValueAnimator makes one element disappear?
Can JavaParser recognize the feed to BufferedReader?
Enable exec-maven-plugin while executing jar through java command
How to bind value of property to collection in Mybatis?
i want to create a simple generic method to count the numbers after applying filter
Get PID of some running task
Can Attribute reference in python be compared with static class in Java ?
princiapal.getName() returns ID instead of name for facebook
String.replaceFirst error? Java
How do I make one component visible in more than one pane in a JTabbedPane?
Why hibernate returns no entity from db, however given table has data?

Categories

HOME
testing
isabelle
ngrx
comparison
webstorm
mean-stack
rsync
cmd
spring-jdbc
youtube-api-v3
binary-tree
survey
wamp
gnupg
reveal.js
handsontable
export-to-csv
jplayer
systemc
libtiff
windows-7-x64
alpine
jasonette
h2db
linkerd
jaxb2
google-qpx-express-api
stringtemplate
hybridauth
uiswipegesturerecognizer
wixsharp
occlusion
reportbuilder
reportingservices-2005
midl
netcdf4
wptoolkit
promela
boost-multi-index
veracode
rdfs
gabor-filter
pillow
infix-notation
ado.net-entity-data-model
py2app
mathematica-frontend
ubuntu-10.04
photobucket
jms2
eclipse-clp
cannon.js
fouc
jqgrid-formatter
rgeo
muse
sortedlist
remobjects
notify
e10s
truevault
cdt
rdl
onactivityresult
uitouch
android-2.2-froyo
oracle-warehouse-builder
message-driven-bean
openlaszlo
industrial
terminal-services
gnu-smalltalk
quantlib-swig
tidy
cos
android-screen-support
clipper
cisco-jtapi
dice
errai
objective-c-2.0
nsmanagedobject
reddot
pydot
vdsp
floating
cxxtest
nsviewanimation
sustainable-pace
spec#
scripting-languages
uiq3

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App