First row(column names) are not showing up in java mysql

Viewed 49

this is where the frame is located. I can't figure out if the problem is in the display itself or the code where I get the table data from mysql.

public class Inventory implements ActionListener {
private static JFrame inv;
private static JLabel store;

private static JPanel panel;
private static JPanel panel2;
private static JScrollPane scrollPane;
private static  JTable tbl;
private  static JTextField input ;
private String url = "jdbc:mysql://localhost:3306/shoeStore";
private String user = "root";
private String pass = "password";
private static  JButton display;
private static  JButton clear;
private static  JButton addItem;
private static JTextField id;
private static JTextField brand;
private static JTextField name;
private static JTextField sizes;;
private static JTextField price;
private static JTextField quanti;

public void showInv() {
    inv = new JFrame("ShoePatos | Inventory");
      panel = new JPanel();
      panel2 = new JPanel();
    inv.setSize(700,800);
    inv.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    inv.setVisible(true);
    inv.setResizable(false);
    inv.setLayout(new BorderLayout());
    panel.setPreferredSize(new Dimension(100, 100));
    panel2.setPreferredSize(new Dimension(100,700));
    panel.setBackground(new Color(12,29,54,255));
    panel2.setBackground(new Color(12,29,54,255));
    inv.add(panel, BorderLayout.NORTH);
    inv.add(panel2, BorderLayout.CENTER);
    
    panel2.setBorder(new EmptyBorder(5,5,5,5));
    
        tbl = new JTable();
    tbl.setBackground(Color.orange);
    
    
    panel2.add(tbl);
    

    
    display = new JButton("Show Inventory");
    display.setBounds(50,50, 50, 50);
    display.setBackground(Color.orange);
    panel.add(display);
    display.addActionListener(this);
    
    addItem = new JButton("Add items");
    addItem.setBounds(100,50, 50, 50);
    addItem.setBackground(Color.orange);
    panel.add(addItem);
    
    addItem.addActionListener(this);
    
    
}

the code below is where I add columns and rows to the array I cannot find the reason on why the columns wont show up.

public void displayItem() {
    clear = new JButton("Clear");
    clear.setBounds(50,50, 50, 50);
    clear.setBackground(Color.orange);
    panel.add(clear);
    clear.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            tbl.setModel(new DefaultTableModel());
        inv.dispose();
            showInv();
        }
    });
    
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection con = DriverManager.getConnection(url,user,pass);
        Statement st = con.createStatement();
        String query = "select * from shoes";
        ResultSet res = st.executeQuery(query);
        ResultSetMetaData data = res.getMetaData();
        DefaultTableModel model = (DefaultTableModel) tbl.getModel();
        
        int col = data.getColumnCount();
        String[] colName = new String[col];
        
        for(int i=0;i<col;i++)
            colName[i] = data.getColumnName(i+1);
            model.setColumnIdentifiers(colName);
        
    
            tbl.getColumnModel().getColumn(0).setPreferredWidth(50); 
            tbl.getColumnModel().getColumn(1).setPreferredWidth(150); 
            tbl.getColumnModel().getColumn(2).setPreferredWidth(150); 

        String sPrice,sQuant,sID,sBrand, sName, sSize;
        while(res.next()) {

            sID = res.getString(1);
            sBrand = res.getString(2);
            sName = res.getString(3);
            sSize = res.getString(4);
            sPrice = res.getString(5);
            sQuant = res.getString(6);
            String [] row = { sID,sBrand, sName, sSize, sPrice, sQuant};
            model.addRow(row);
        }
        st.close();
        con.close();        
    } catch (ClassNotFoundException | SQLException e1) {
        
        e1.printStackTrace();
    }
    
}

images: enter image description here

1 Answers

You will have to get the column names separately and insert them in the table before the actual data:

for(int i = 1; i<=count; i++) {
     System.out.println(rsMetaData.getColumnName(i));
}

Save those in an array and you can put them in the table!

Related