How to read data from a mysql database in Java?

I create a UI using SWT. In the UI I have a tree structure where I plan to list some data from the database.

Currently, I only put some static data in this tree structure as shown below.

import org.eclipse.swt.*;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.*;

public class CheckListTree {
    
    public void checkPath(TreeItem item, boolean checked, boolean grayed) {
        if (item == null) return;
        if (grayed) {
            checked = true;
        } else {
            int index = 0;
            TreeItem[] items = item.getItems();
            while (index < items.length) {
                TreeItem child = items[index];
                if (child.getGrayed() || checked != child.getChecked()) {
                    checked = grayed = true;
                    break;
                }
                index++;
            }
        }
        item.setChecked(checked);
        item.setGrayed(grayed);
        checkPath(item.getParentItem(), checked, grayed);
    }

    public void checkItems(TreeItem item, boolean checked) {
        item.setGrayed(false);
        item.setChecked(checked);
        TreeItem[] items = item.getItems();
        for (int i = 0; i < items.length; i++) {
            checkItems(items[i], checked);
        }
    }

public void createCheckListTree(Composite compTab2, GridData layoutData) {
        Tree tree = new Tree(compTab2, SWT.BORDER | SWT.CHECK);
        tree.setLayoutData(layoutData);
        tree.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                if (event.detail == SWT.CHECK) {
                    TreeItem item = (TreeItem) event.item;
                    boolean checked = item.getChecked();
                    checkItems(item, checked);
                    checkPath(item.getParentItem(), checked, false);
                }
            }
        });
        
        for (int i = 1; i < 16; i++) {
            TreeItem itemI = new TreeItem(tree, SWT.NONE);
            itemI.setText("Check " + i);
            for (int j = 0; j < 4; j++) {
                TreeItem itemJ = new TreeItem(itemI, SWT.NONE);
                itemJ.setText("Check " + i + " " + j);
                for (int k = 0; k < 4; k++) {
                    TreeItem itemK = new TreeItem(itemJ, SWT.NONE);
                    itemK.setText("Item " + i + " " + j + " " + k);
                }
            }
        }
}
        //display.dispose();
    

}

Open in new window

Can you please help me on how to put data from a mysql database instead of "Item X" in this code?
TolgarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mrcoffee365Commented:
There are many tutorials on the web on how to write a java program which connects to a database.  There are specific ones for a mysql db.  For example:
http://www.dreamincode.net/forums/topic/32360-using-mysql-with-java/

What have you tried so far?  If you have tried to add a db query to your java code, then post that code.
0
girionisCommented:
Have a look at this tutorial.
0
TolgarAuthor Commented:
So, this is the code I have to query database:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class QueyDatabase {
       
    public ResultSet getFullCheckList() throws ClassNotFoundException, SQLException{
        Class.forName("com.mysql.jdbc.Driver") ;
        Connection conn = DriverManager.getConnection("jdbc:mysql://someserver/someDatabase", "someDatabase", "pwd") ;
        Statement stmt = conn.createStatement() ;
        String querycheckName = "SELECT `name` FROM `check`;" ;
        ResultSet rs = stmt.executeQuery(querycheckName) ;
        return rs;       
    }  
    
    public ResultSet getChecksByFileType(String[] FileType) throws ClassNotFoundException, SQLException{
        Class.forName("com.mysql.jdbc.Driver") ;
        Connection conn = DriverManager.getConnection("jdbc:mysql://someserver/someDatabase", "someDatabase", "pwd") ;
        Statement stmt = conn.createStatement() ;
        String queryFileTypeID = "SELECT `id`, FROM `filetype` WHERE name = FileType;"; 
        ResultSet FileTypeID = stmt.executeQuery(queryFileTypeID) ;
        String querycheckID = "SELECT `check_id`, FROM `results` WHERE filetype_id = FileTypeID;";
        ResultSet checkID = stmt.executeQuery(querycheckID) ;
        String querycheckName = "SELECT `name`, FROM `check` WHERE id = checkID;";
        ResultSet rs = stmt.executeQuery(querycheckName) ;
        return rs;
    }
}

Open in new window


And this is the code that creates the directory tree with some static data.

import org.eclipse.swt.*;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.*;

public class CheckListTree {
    
    public void checkPath(TreeItem item, boolean checked, boolean grayed) {
        if (item == null) return;
        if (grayed) {
            checked = true;
        } else {
            int index = 0;
            TreeItem[] items = item.getItems();
            while (index < items.length) {
                TreeItem child = items[index];
                if (child.getGrayed() || checked != child.getChecked()) {
                    checked = grayed = true;
                    break;
                }
                index++;
            }
        }
        item.setChecked(checked);
        item.setGrayed(grayed);
        checkPath(item.getParentItem(), checked, grayed);
    }

    public void checkItems(TreeItem item, boolean checked) {
        item.setGrayed(false);
        item.setChecked(checked);
        TreeItem[] items = item.getItems();
        for (int i = 0; i < items.length; i++) {
            checkItems(items[i], checked);
        }
    }

public void createCheckListTree(Composite compTab2, GridData layoutData) {
        Tree tree = new Tree(compTab2, SWT.BORDER | SWT.CHECK);
        tree.setLayoutData(layoutData);
        tree.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                if (event.detail == SWT.CHECK) {
                    TreeItem item = (TreeItem) event.item;
                    boolean checked = item.getChecked();
                    checkItems(item, checked);
                    checkPath(item.getParentItem(), checked, false);
                }
            }
        });
        
        for (int i = 1; i < 16; i++) {
            TreeItem itemI = new TreeItem(tree, SWT.NONE);
            itemI.setText("Check " + i);
            for (int j = 0; j < 4; j++) {
                TreeItem itemJ = new TreeItem(itemI, SWT.NONE);
                itemJ.setText("Check " + i + " " + j);
                for (int k = 0; k < 4; k++) {
                    TreeItem itemK = new TreeItem(itemJ, SWT.NONE);
                    itemK.setText("Item " + i + " " + j + " " + k);
                }
            }
        }
}
        //display.dispose();
    

}

Open in new window


Now, instead of putting static data by looping in the for loop, how can I replace it with the data that I get from the QueyDatabase class in the getFullCheckList() method?

Thanks,
0
mrcoffee365Commented:
The sql queries you have in your code will not work.  There are some typos, etc -- I don't know if they're really in your code, or if you forgot to copy everything.

So this will be easier if you make sure your sql queries are correct before trying to debug the rest of your SWT app.

Once you have correct sql, then I think that it's this code that you want to pull from the db, right?:

        for (int i = 1; i < 16; i++) {
            TreeItem itemI = new TreeItem(tree, SWT.NONE);
            itemI.setText("Check " + i);
            for (int j = 0; j < 4; j++) {
                TreeItem itemJ = new TreeItem(itemI, SWT.NONE);
                itemJ.setText("Check " + i + " " + j);
                for (int k = 0; k < 4; k++) {
                    TreeItem itemK = new TreeItem(itemJ, SWT.NONE);
                    itemK.setText("Item " + i + " " + j + " " + k);
                }
            }
        }

If so, you want to take out the 16 limit in your loop, and just loop through all the values returned from the query.  If you want to hand around a resultset, which it looks as if you want to do, then loop through the resultset.

while( rs.next() ) {
          String myval1 = rs.getString("whateveryourfieldnameis");
          String myval2 = rs.getString("fieldname2");
            TreeItem itemI = new TreeItem(tree, SWT.NONE);
            itemI.setText("Check " + myval1);
            for (int j = 0; j < 4; j++) {
                TreeItem itemJ = new TreeItem(itemI, SWT.NONE);
                itemJ.setText("Check " + myval1 + " " + myval2);
            }
}

Although -- I don't understand looping through 4 things within the 16 things.  This should all be keyed from the values in the db, so you need to change the for loop logic to be based on the elements in a resultset object.

Make sure to  close your resultset, stmt, connection when you finish.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.