Results 1 to 7 of 7
  1. #1
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Getting ONLY database table values, while skipping column names

    Got this method to populate a table from database:
    Java Code:
    private void Update_Table(){
        try{
         String sql = "SELECT * FROM Users WHERE Login='Admin'"; 
         pst = conn.prepareStatement(sql);
         rs = pst.executeQuery(sql);
         jTable1.setModel(DbUtils.resultSetToTableModel(rs));
        } 
        catch (Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }
    Currently, it gets all values that i need, but it also displays the column names (for example user_name, user_id, user_passwordetc..). I want to make custom Titles for my table (for example instead of user_id, i want the title to be just ID). What do i do to skip the column names from database and just take the values?


    Also ran into another problem, how do i display more than 1 table?
    Java Code:
    private void EsamiPranesimaiTableMethod(){
    
        try{
         String sql = "SELECT Message_name FROM Messages_group WHERE group_name='Testgroup'";
         pst = conn.prepareStatement(sql);
         rs = pst.executeQuery(sql);
         String sql2 = "SELECT Message_name FROM Messages_single WHERE Username='Testname'";
         pst = conn.prepareStatement(sql2);
         rs2 = pst.executeQuery(sql2);
         Table_EsamiPranesimai.setModel(DbUtils.resultSetToTableModel(rs));
         Table_EsamiPranesimai.setModel(DbUtils.resultSetToTableModel(rs2));
        } 
        catch (Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }
    Currently 'Table_EsamiPranesimai.setModel(DbUtils.resultSetT oTableModel(rs2));' overwrites 'Table_EsamiPranesimai.setModel(DbUtils.resultSetT oTableModel(rs));' , so i get only 1 result.. how do i make it so it would display both?

    Edit: i think the 2nd problem can be solved with a proper SQL code, something like this:
    Java Code:
    String sql = "SELECT Message_name  FROM Messages_group WHERE group_name='Testgroup' UNION SELECT Message_name FROM Messages_single WHERE Username='Testname'";
    Still didnt find an answer to the first problem tho..
    Last edited by iNko; 12-02-2012 at 09:36 PM.

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

    Default Re: Getting ONLY database table values, while skipping column names

    For the first part, what does DbUtils.resultSetToTableModel return?
    What sort of TableModel?
    If that is your own utility class then simply change the model.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: Getting ONLY database table values, while skipping column names

    Quote Originally Posted by Tolls View Post
    For the first part, what does DbUtils.resultSetToTableModel return?
    What sort of TableModel?
    If that is your own utility class then simply change the model.
    SQL code that describes witch data to take, also where
    Java Code:
    String sql = "SELECT * FROM Users WHERE Login='Admin'";
    prepares the SQL code
    Java Code:
    pst = conn.prepareStatement(sql);
    executes the SQL code
    Java Code:
    rs = pst.executeQuery(sql);
    'DbUtils.resultSetToTableModel' i think checks the executed SQL code and then fills the 'jTable1' with that data
    Java Code:
    jTable1.setModel(DbUtils.resultSetToTableModel(rs));
    This is how i understand my code..

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

    Default Re: Getting ONLY database table values, while skipping column names

    So, what is DbUtils?
    Where is it from?
    That's where you want to make the changes.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: Getting ONLY database table values, while skipping column names

    DbUtils is this - import net.proteanit.sql.DbUtils;
    its not used anywhere else

  6. #6
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: Getting ONLY database table values, while skipping column names

    ok got it figured out, it all depends on the SQL code..
    Java Code:
    String sql = "SELECT Message_name FROM Messages_group WHERE group_name='Testgroup'";
    if i add
    Java Code:
    String sql = "SELECT Message_name as RandomTitleName FROM Messages_group WHERE group_name='Testgroup'";
    The title will be RandomTitleName, instead of the one in the database table

  7. #7
    Fozter is offline Member
    Join Date
    Jun 2014
    Posts
    1
    Rep Power
    0

    Default Re: Getting ONLY database table values, while skipping column names

    Quote Originally Posted by iNko View Post
    ok got it figured out, it all depends on the SQL code..
    Java Code:
    String sql = "SELECT Message_name FROM Messages_group WHERE group_name='Testgroup'";
    if i add
    Java Code:
    String sql = "SELECT Message_name as RandomTitleName FROM Messages_group WHERE group_name='Testgroup'";
    The title will be RandomTitleName, instead of the one in the database table
    Thank you!!

Similar Threads

  1. Column names
    By asai in forum AWT / Swing
    Replies: 7
    Last Post: 02-16-2012, 06:58 PM
  2. Replies: 0
    Last Post: 01-16-2012, 07:33 AM
  3. Replies: 1
    Last Post: 03-24-2011, 12:51 PM
  4. adding sum column on jtable that is binded to a database table
    By madcloud in forum Enterprise JavaBeans (EJB)
    Replies: 4
    Last Post: 12-06-2010, 08:26 AM
  5. Replies: 1
    Last Post: 07-06-2010, 04:50 PM

Tags for this Thread

Posting Permissions

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