Results 1 to 1 of 1
Thread: How to display database in table
- 03-15-2010, 05:57 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 1
- Rep Power
- 0
How to display database in table
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class TableDataTesting {
private Table table;
private static Connection connect;
private static Statement statement;
private static ResultSet resultSet;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
TableDataTesting window = new TableDataTesting();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 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);
table.setItemCount(100);
table.addListener(SWT.SetData, new Listener() {
public void handleEvent(Event event) {
TableItem item = (TableItem) event.item;
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()){
item.setText(new String[]{resultSet.getString(1),resultSet.getString(2),res ultSet.getString(3),resultSet.getString(4)});
}
}catch(Exception e){
System.out.println("Cannot connect to database server");
}
}
});
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
{
TableColumn tblclmnEno = new TableColumn(table, SWT.NONE);
tblclmnEno.setWidth(100);
tblclmnEno.setText("Eno");
}
{
TableColumn tblclmnEname = new TableColumn(table, SWT.NONE);
tblclmnEname.setWidth(100);
tblclmnEname.setText("Ename");
}
{
TableColumn tblclmnAge = new TableColumn(table, SWT.NONE);
tblclmnAge.setWidth(100);
tblclmnAge.setText("Age");
}
{
TableColumn tblclmnPosition = new TableColumn(table, SWT.NONE);
tblclmnPosition.setWidth(100);
tblclmnPosition.setText("Position");
}
}
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 table row by row without creating more TableItem"
I've tried so much but still get the last record:confused:. 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 regards
Similar Threads
-
Table display
By gissah in forum NetBeansReplies: 1Last Post: 06-25-2009, 08:20 AM -
problem with display table pagination and sorting
By appuchennai in forum Web FrameworksReplies: 0Last Post: 11-12-2008, 10:04 AM -
how to display the alerts in table
By geeta_ravikanti in forum JDBCReplies: 4Last Post: 04-04-2008, 06:45 AM -
how to refresh the table when a new account is added and display instantly
By sravanthi narra in forum SWT / JFaceReplies: 4Last Post: 01-05-2008, 07:39 PM -
Data formatting for the <display:table>
By yuchuang in forum Web FrameworksReplies: 3Last Post: 12-14-2007, 10:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks