MyEclipse: How to display database in SWT table tamnc - Mar 19, 2010 - 04:24 AM Post subject: How to display database in SWT table
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
************************************************** *************************
I've used the tool to developer this small application normally. And the major point here that is "How can I display all records from my database into a SWT table row by row"
I've tried so much but still get the last record. So I really need your help.
My mysql database is like this:
Eno Ename Age Position
1001 David 32 Programer
1002 Tom 30 Programer
1003 Thomas 34 Web developer
Thanks for your watching
Best regardssupport-ram - Apr 13, 2010 - 01:01 PM Post subject: RE: How to display database in SWT table
tamnc,
I have modified your code as per your requirement. I would suggest please execute this code.
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
Shell shell = new Shell();
shell.setLayout(new GridLayout(1, false));
shell.setSize(450, 300);
shell.setText("SWT Application");
{
table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION | SWT.VIRTUAL);
table.setHeaderVisible(true);
//code modification by Genuitec support starts here
//table.setItemCount(100);
table.setItemCount(1);
//code modification by Genuitec support ends here
table.addListener(SWT.SetData, new Listener() {
public void handleEvent(Event event) {
//code modification by Genuitec support starts here
//TableItem item = (TableItem) event.item;
//code modification by Genuitec support ends here
try{
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/EmployeeDatabase","root","12345");
System.out.println("Connecting succesfully");
statement = connect.createStatement();
resultSet = statement.executeQuery("Select * from EMPLOYEE");
while(resultSet.next()){
//code modification by Genuitec support starts here
TableItem item = new TableItem(table, SWT.NONE);
//code modification by Genuitec support ends here
item.setText(new String[]{resultSet.getString(1),resultSet.getString(2),resultSet.getString(3),resultSet.getString(4)});
}
}catch(Exception e){
System.out.println("Cannot connect to database server");
}
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
Let me know how this works for you.Rosemarry - Jun 22, 2010 - 07:53 AM Post subject: RE: How to display database in SWT table
One example scenario where a virtual Table would be useful is for displaying the results of a search query on a library book database. This could exhibit a slow initial population time with a non-virtual Table because of the potential for huge result sets and expensive database queries required to retrieve item summary information. However, a virtual table should perform well here since the initial population time would just be the time required to create the first page of items. In a system with good result ranking heuristics these could prove to be the only items ultimately viewed by the user.support-ram - Jun 23, 2010 - 11:37 AM Post subject: RE: How to display database in SWT table
Rosemarry,
Thank you for sharing your suggestion on this.CaptainGan - Feb 23, 2011 - 04:05 PM Post subject: Re: RE: How to display database in SWT table
I used the above code,it's good ,but there is blank empty on it.
I have a more better code here from here,OK no more words show code.
package com.ui;
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}support-swapna - Feb 25, 2011 - 01:10 PM Post subject: RE: Re: RE: How to display database in SWT table
CaptainGan,
Thank you for posting the code. It will surely help other users.
Do let us know if you have any issues.olivier57 - Sep 11, 2012 - 10:22 PM Post subject: Not working
Hi,
This post is a bit old and tmanc doesn't seem to be anymore interested but I was, as a newbie to java/swt.
Problem is that suggestions doesn't work. It's still the last row which is displayed and if iI add CaptainGan last modification "table.setItemCount(0); " I even get "cannot connect to database server".
Thanks for any help
Oliviersupport-swapna - Sep 12, 2012 - 05:49 AM Post subject:
Olivier,
I am afraid this is an SWT development related query.
You can post the issue to Window Builder forum for a detailed support. Here is the link to the forum :
http://www.eclipse.org/forums/index.php/f/214/