Results 1 to 9 of 9
  1. #1
    smallmos1 is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default displaying database data in a gui

    what's the best way to do this? my gui will have three jpanels: top, middle and bottom. i want the data from my db to be displayed in the middle panel. i want to use a jtable, preferably a custom table from abstractTableModel, becuase from what i can see it invokes methods that allow for the constant updating of data from the db to the gui.......am i correct in saying this? i've exhausted every way i can think of of trying to do this, such as:

    a class QueryDB that creates a resultSet from the db, using vectors, i havn't covered these yet in college and am basically going by what i read on the net. another class called BuildGUI that extends abstract table model and puts the resultSet from QueryDB in as a parameter to build the gui. but when i try to pass it in it just wont work.

    can someone please tell me the best way to do this? or even hint at the way i should structure my classes etc. and i'll work away on that. if someone wants me to go into detail, just ask.

    this would be really appreciated

  2. #2
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    104
    Rep Power
    0

    Default

    I suggest starting simpler and enhancing it later. Once you get your resultset, just populate the JTable manually. Use the table model to update the rows. You could even use a Java timer to refresh the table periodically.

    Once you have things working, then you can look at making it more dynamic, auto-refresh, etc.

  3. #3
    smallmos1 is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    thanks for the reply. you're right i'm probably trying a bit too much at this stage. i've created a resultSet, rset, in my QueryDB class. i have another class called ViewShiftsGUI, how can i call rset in this class? i'm trying to pass the result set in as a parameter to a jtable.

  4. #4
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    104
    Rep Power
    0

    Default

    Look at this tutorial on creating a sample JTable with data: How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    Once you can create a table with columns and rows, you can access it's table model to do things like removing rows, adding rows, changing rows, etc. But try to create a sample table first. That tutorial will show you how to do that. Then you can create the table that you need (with your specific columns).

    You don't have to pass the resultset to the table. You would loop through the resultset, grab the row data, and use the table model to set the row data. There are examples in the above link to show you how to do this. Good luck.

  5. #5
    smallmos1 is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    thanks a lot, i'll give it a shot.

  6. #6
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default Displaying a table from database

    I can show you a sample code which will display a table from Mysql in the Panal..


    Java Code:
    final Vector columnNames = new Vector();
    final Vector data = new Vector();
          try{
        Class.forName("com.mysql.jdbc.Driver");
         Connection con = DriverManager.getConnection(m_Url, "root", "123");
            Statement st = con.createStatement();
            ResultSet rs= st.executeQuery("Select *from prolist");
        ResultSetMetaData md = rs.getMetaData();
    int columns = md.getColumnCount();
    for (int i = 1; i <= columns; i++) {
    columnNames.addElement( md.getColumnName(i) );
    }
    while (rs.next()) {
    Vector row = new Vector(columns);
    for (int i = 1; i <= columns; i++) {
    row.addElement( rs.getObject(i) );
    }
    data.addElement( row );
    }
    }
    catch(Exception e){}
    
    JTable table = new JTable(data, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);

    See this:
    JScrollPane scrollPane = new JScrollPane(table);
    You must add this scrollPane in your panel.It will work.

    Please,before copy-paste executing this code,edit the code as your desktop environment

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    That's really a little nasty I'm afraid.
    You have no model representing the data the user is manipulating, relying entirely on a Vector or Vectors of Objects.
    Passing changes back from this to the db will be a pain.

    As said, the OP needs to break this down.
    Either start with the front end and a model respresenting that data, building a display, and having a dummy data source at the back that simply populates a List (or does Swing require Vectors?) of <Model Class>. Or start from the db end building a model that is then stored and retrieved from the db.

  8. #8
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    Check this three line ,you will got your answer.
    ResultSet rs= st.executeQuery("Select *from TABLENAME where column='value' ");

    ResultSetMetaData md = rs.getMetaData();
    int columns = md.getColumnCount();
    getColumnCount() returns the number of columns of the table.

    then
    while (rs.next()) {
    Vector row = new Vector(columns);
    for (int i = 1; i <= columns; i++) {
    row.addElement( rs.getObject(i) );
    }
    data.addElement( row );
    }
    Simple,As I am new,I didn't understand where is your problem.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    You have no model.
    That is my problem.

    Relying on Vectors to hold elements that by rights (and by any good design) should be held as attributes of an object is nto a Good Thing.

    Yes, it might work in a simple situation, but it's a maintenance nightmare waiting to happen. That row Vector should be a class that represents the data, not a Vector.

Similar Threads

  1. Getting and Displaying Swing Components From DataBase
    By vivekkumar in forum AWT / Swing
    Replies: 1
    Last Post: 07-27-2010, 03:32 PM
  2. Displaying image in jsp from mysql database
    By mrvigneshmca in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-04-2009, 02:13 PM
  3. Displaying Data
    By skiing in forum New To Java
    Replies: 4
    Last Post: 11-15-2008, 02:56 AM
  4. Displaying data from mysql database.
    By Haresh gurav in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-09-2008, 02:54 AM
  5. Displaying different columns with database query
    By jennybgh in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-02-2007, 05:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •