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

add new line to file when appending
Convert/alter edittext input for use in another calculation.
How to exit from forEach if some condition matches in java 8
How to step into or step over code while debugging application in Android Studio
How to write Lambda expression into method references?
How to format or truncate a scientific notation Double value to three decimal places without changing the exponent part in java
Does not print, loop and array error
unable to start activity, android.content.res.Resources$NotFoundException: String resource ID #0x0 [duplicate]
Run Kurento application
Send e-mail with Java using hotmail account
Jackson - Not deserialize string “null” to null object
Reading array from txt file?
how to set pom.xml to run main class in maven project in netbeans
Select in Tapestry with name not id
Call to SOAP webservice never returns
I cant access rtf file after saving something in it

Categories

HOME
cakephp
java
blogger
oracle11g
rubygems
gps
baqend
directx
contact
applepay
ssl-client-authentication
vault
row
etl
postgres-xl
gz
remote-access
phaser
visual-studio-2005
progressive-web-apps
ef-migrations
undefined
intentfilter
ehcache
restful-authentication
claims-based-identity
numerical-methods
spring-mybatis
fluentvalidation
publish
tinymce-4
google-qpx-express-api
android-ble
host
stacked
main
git-merge
restlet
scaffold
espeak
ansible-playbook
bootstrapper
sql-server-agent
osx-mavericks
atomicity
multipeer-connectivity
domain-model
rdfs
diagnostics
synchronous
cubes
deadbolt-2
nxt
removechild
lua-5.1
qgraphicsview
captivenetwork
two-factor-authentication
netmq
thredds
android-listview
ideamart
sysinternals
census
angular-local-storage
key-management
dereference
oxwall
camanjs
ivyde
ms-project-server-2010
tidy
web2py-modules
tinn-r
angularjs-controller
errai
removeclass
mercurial-server
infobox
hamachi
horizontal-accordion
netbeans-7.1
modelstate
nsviewanimation
virtual-functions
privilege
yagni
misv

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App