Search code examples
javaswingjtablejtableheader

Unable to show column names in JTable


I created small form in Java in which used JTable and to fill the table. I used the following code.

import net.proteanit.sql.DbUtils;

 try
 {
        CreateConnection();
        PreparedStatement st =conn.prepareStatement("Select * from ABC;");
        ResultSet rs = st.executeQuery();
        jtable_clock.setModel(DbUtils.resultSetToTableModel(rs));
        conn.close();
 }
 catch(Exception ex)
 {    
      JOptionPane.showMessageDialog(null, ex.toString());
 } 

Here everything works fine but I am unable to set Column Names in my table.

I am getting data from table ABC but how can I set Column Names in table?


Solution

  • I did not got any problem. Try to run my code. Replace my DB username & password.

    import java.awt.HeadlessException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    
    /**
     *
     * @author Masud
     */
    public class Test extends JFrame {
    
        public Test() throws HeadlessException {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zoodb", "root", "12345678");
                Statement statement = connection.createStatement();
                ResultSet resultSet = statement.executeQuery("select * from ABC");
                JTable jTable = new JTable(DbUtils.resultSetToTableModel(resultSet));
                JScrollPane pane = new JScrollPane(jTable);
                add(pane);
            } catch (Exception ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args){
            new Test();
        }
    }