-1

my code is this :

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    DefaultTableModel model=(DefaultTableModel)pl.getModel();               
        String urlBaseDonnes="jdbc:mysql://localhost:3306/test";
        Connection con;
        try{
        Class.forName("com.mysql.jdbc.Driver");
        }catch(ClassNotFoundException ex){
            System.out.println(ex);
        }
        try{
            con =DriverManager.getConnection(urlBaseDonnes,"root","");
        try (Statement stmt = con.createStatement()) {
            ResultSet rs = stmt.executeQuery("SELECT * FROM news");
            ResultSetMetaData rsMetaData = rs.getMetaData();
            int numberOfColumns = rsMetaData.getColumnCount();
            System.out.println("resultSet MetaData column Count=" + numberOfColumns);
            int j=0;
            pl.setModel(new javax.swing.table.DefaultTableModel(new Object [][] {},new String [] {"ddd","fgg","nl"}));

            // get the name of the column, and changing it

            for (int i = 1 ; i <= numberOfColumns; i++) {
                ChangeName(pl,j,rsMetaData.getColumnName(i));
                System.out.println(rsMetaData.getColumnName(i));
                System.out.println(j);
                j= j+1;
            }
            String query="SELECT * FROM news";
            rs=stmt.executeQuery(query);

            //Show the database

            while(rs.next()){
                String id=rs.getString("id");
                String titre=rs.getString("titre");
                String contenu=rs.getString("contenu");  
                model.addRow(new Object[]{id,titre,contenu});
            }
            rs.close();
        }
        con.close();
        System.out.println("close the database");
        }catch(SQLException ex){
            System.out.println(ex);
        }
}       

My goal is to change the name of the columns dynamically when i change the name of my table, this code can change the name of the column but when it's come to showing the content of the database it doesn't work, also i can't add column, for exemple if i choose a table that have 4 column, i'll have an error. I tried to add column using a DefaultTableModel but it didn't work. could you please help me with the part of adding column dynamically ? and tell me why this code doesn't show what's on the database ? Thanks

5
  • "if i choose a table that have 4 column, i'll have an error." And what is that error? I can tell you up front, you need to work on your Google Fu, because Stack Overflow and other sites have lots of answers to your exact question. Commented Aug 23, 2014 at 17:39
  • If i choose a table from my database that have 4 column or more, i'll have to add column, because i have in my Jtable only 3 default column. I don't know how to do that, i tried but i always fail Commented Aug 23, 2014 at 17:51
  • I've recently had an issue with my JTables where I had to dynamically add columns and fetch data from .ser files. However I can't understand your question to see if I may help with my answer Commented Aug 23, 2014 at 18:07
  • possible duplicate of Adding Columns to JTable dynamically Commented Aug 23, 2014 at 18:12
  • MarsAtomic, i already saw that topic, but when i try to use a DefaultTableModel it does not work. Juxhin, i have 2 problemes in my code, the first one is that i can't add columns to my table when i try to use a tablemodel i end up having errors, the second one is that the data that are in my database are not shown. this code only changes the name of the column. Commented Aug 23, 2014 at 18:28

1 Answer 1

2

that the data that are in my database are not shown. this code only changes the name of the column.

First you access the TableModel of the table:

DefaultTableModel model=(DefaultTableModel)pl.getModel(); 

Then you change the TableModel of the table:

pl.setModel(new javax.swing.table.DefaultTableModel(new Object [][] {},new String [] {"ddd","fgg","nl"}));

Then you add the data from the database to the "old" model:

model.addRow(new Object[]{id,titre,contenu});

So the data is NOT added to the current model.

If you want to create a new model then your code should be something like:

DefaultTableModel model = DefaultTableModel(Object[] columnNames, int rowCount);
pl.setModel( model ); 

Then your code will add the database data to the real model.

The question is why are you trying to hardcode the column name? When you do a select * from the database you don't know how many columns of data will be returned. You should be using more generic code. See the TableFromDatabaseExample.java code from Table From Database.

Sign up to request clarification or add additional context in comments.

4 Comments

rsMetaData.getColumnCount() does return the number of column we have in our database, now we have 3 default column in our Jtable, if we choose from our database a table that have like 4 columns, whithout adding a column in "pl.setModel(new javax.swing.table.DefaultTableModel(new Object [][] {},new String [] {"ddd","fgg","nl"}));", what we want is to dynamically add a column in our table so that whaterver the table that we choose we will have the same result.
@Sam Your question doesn't make any sense. Your code should just display the columns in the database. The query should not have the "same result" if the table has a different number of columns. I already gave you the answer to handle a generic number of columns. If you don't like the column name then you change the name by using the TableColumn.setHeaderValue() method after you create the table with the TableModel.
you said that my data is not added in my current model, could you be more specific on how i can make that happen ?
@Sam Right after that statement I gave you the two line of code you need to create the model and add it to the table so that your looping code will update the model in the table. I don't know how to be more specific.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.