facebook

How to display database in SWT table

  1. MyEclipse Archived
  2.  > 
  3. Matisse/Swing UI Development
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #306873 Reply

    tamnc
    Member

    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 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 regards

    #307606 Reply

    Ram
    Member

    tamnc,

    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 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”);
    }

    }
    });

    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.

    #309263 Reply

    Rosemarry
    Member

    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.

    #309329 Reply

    Ram
    Member

    Rosemarry,

    Thank you for sharing your suggestion on this.

    #314704 Reply

    CaptainGan
    Member

    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;

    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();
    }
    }
    }

    }

    #314771 Reply

    support-swapna
    Moderator

    CaptainGan,

    Thank you for posting the code. It will surely help other users.
    Do let us know if you have any issues.

    #330058 Reply

    olivier57
    Member

    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

    Olivier

    #330065 Reply

    support-swapna
    Moderator

    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/

    Hope this helps.

Viewing 8 posts - 1 through 8 (of 8 total)
Reply To: How to display database in SWT table

You must be logged in to post in the forum log in