When I press the "show" button it does not show any table and error

In summary, the conversation is about debugging code and the importance of learning how to do it. The speaker asks what tools are being used for debugging and suggests using a debugger or adding print statements to track the code's progress.
  • #1
Suxil_7
1
0
I need help. When I press show button it does not show any table and error.
Here is the code:
Java:
package Demo;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.awt.event.ActionEvent;

@SuppressWarnings("serial")
public class Datatable extends JFrame {

    private JPanel contentPane;
    private JTable table;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Datatable frame = new Datatable();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Datatable() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setContentPane(contentPane);
        contentPane.setLayout(null);
      
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(235, 172, -177, -125);
        contentPane.add(scrollPane);
      
        table = new JTable();
        scrollPane.setViewportView(table);
      
        JButton btnNewButton = new JButton("Show");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");
                    Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/mybd","root","");
                    Statement st = con.createStatement();
                    String query = "Select * from student";
                    ResultSet rs =st.executeQuery(query);
                    ResultSetMetaData rsmd=rs.getMetaData();
                    DefaultTableModel model =(DefaultTableModel)table.getModel();
                    int cols =rsmd.getColumnCount();
                    String [] colName = new String[cols];
                    for(int i =0; i<cols;i++) {
                        colName[ i] =rsmd.getColumnName(i+1);
                        model.setColumnIdentifiers(colName);
                        String id,name,course;
                        while(rs.next()) {
                            id= rs.getString(1);
                            name= rs.getString(2);
                            course= rs.getString(3);
                           
                            String[] row = {id,name,course};
                            model.addRow(row);
                        }
                    //    rs.close();
                        //st.close();
                //con.close();
                       
                    }
                } catch (ClassNotFoundException | SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
           
            }
        });
        btnNewButton.setBounds(326, 69, 85, 21);
        contentPane.add(btnNewButton);
    }
}
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
One of the most important things to learn in programming anything is how to debug your code. What are you using to debug? Are you running it in a debugger where you can step through the code? Have you tried to place prints or display messages at key locations to see what is happening?
 
  • Like
Likes Mark44, jedishrfu, berkeman and 1 other person

1. Why is the "show" button not displaying any table?

There could be multiple reasons for this issue. It could be due to a coding error in the show button function, a missing or incorrect reference to the table, or an error in the logic of the code. It is important to check the code carefully to identify and fix the issue.

2. How do I troubleshoot the "show" button not displaying a table?

The first step is to check the code for any errors or missing references. You can also use debugging tools or print statements to track the flow of the code and identify where the issue might be occurring. Additionally, checking the console for any error messages can provide helpful clues.

3. Is there a specific format or structure required for the table to be displayed?

Yes, the table should have a proper structure and format for it to be displayed correctly. This includes having a <table> tag, <thead> and <tbody> sections, and <th> and <tr> elements to define the table headers and rows, respectively.

4. Could the issue be related to the data being used for the table?

Yes, incorrect or missing data can also cause the "show" button to not display a table. Make sure that the data being used is in the correct format and has all the necessary information for the table to be generated.

5. Are there any common mistakes that can cause the "show" button to not display a table?

One common mistake is using the incorrect function or method to display the table. Another is forgetting to call the function or not providing the necessary parameters for it to work properly. It is important to carefully review the code and double-check for any small errors that might be causing the issue.

Similar threads

  • Programming and Computer Science
Replies
2
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
9
Views
4K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top