- This topic has 7 replies, 6 voices, and was last updated 12 years, 9 months ago by
support-swapna.
-
AuthorPosts
-
tamncMemberimport 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 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 developerThanks for your watching
Best regardsApril 13, 2010 at 7:01 am #307606
RamMembertamnc,
I have modified your code as per your requirement. I would suggest please execute this code.
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);//code modification by Genuitec support starts here
//table.setItemCount(100);
table.setItemCount(1);
//code modification by Genuitec support ends heretable.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”);
}}
});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();
}
}
}}
Let me know how this works for you.
June 22, 2010 at 1:53 am #309263
RosemarryMemberOne 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.
June 23, 2010 at 5:37 am #309329
RamMemberRosemarry,
Thank you for sharing your suggestion on this.
February 23, 2011 at 10:05 am #314704
CaptainGanMemberI 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;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;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;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”);
{
Button btnShowAdmins = new Button(shell, SWT.NONE);
btnShowAdmins.setText(“Show Admins”);
}
{
table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION
| SWT.VIRTUAL);
table.setLinesVisible(true);
table.setHeaderVisible(true);
table.setItemCount(1);
table.addListener(SWT.SetData, new Listener() {
public void handleEvent(Event event) {
table.setItemCount(0);
try {
Class.forName(“com.mysql.jdbc.Driver”);
connect = DriverManager.getConnection(
“jdbc:mysql://localhost:3306/db_eshop”, “root”,
“root”);
System.out.println(“Connecting succesfully”);
statement = connect.createStatement();
resultSet = statement
.executeQuery(“Select * from admin”);
while (resultSet.next()) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText(new String[] { resultSet.getString(1),
resultSet.getString(2),
resultSet.getString(3),
resultSet.getString(4),
resultSet.getString(5) });
}
connect.close();
} catch (Exception e) {
System.out.println(“Cannot connect to database server”);
}}
});table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
1));
{
TableColumn tblclmnNos = new TableColumn(table, SWT.NONE);
tblclmnNos.setWidth(100);
tblclmnNos.setText(“Nos”);
}
{
TableColumn tblclmnEno = new TableColumn(table, SWT.NONE);
tblclmnEno.setWidth(100);
tblclmnEno.setText(“Admin Type”);
}
{
TableColumn tblclmnEname = new TableColumn(table, SWT.NONE);
tblclmnEname.setWidth(100);
tblclmnEname.setText(“Admin Name”);
}
{
TableColumn tblclmnAge = new TableColumn(table, SWT.NONE);
tblclmnAge.setWidth(100);
tblclmnAge.setText(“Login Name”);
}
{
TableColumn tblclmnPosition = new TableColumn(table, SWT.NONE);
tblclmnPosition.setWidth(100);
tblclmnPosition.setText(“Login Password”);
}}
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}}
February 25, 2011 at 7:10 am #314771
support-swapnaModeratorCaptainGan,
Thank you for posting the code. It will surely help other users.
Do let us know if you have any issues.September 11, 2012 at 4:22 pm #330058
olivier57MemberHi,
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
Olivier
September 11, 2012 at 11:49 pm #330065
support-swapnaModeratorOlivier,
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/Hope this helps.
-
AuthorPosts