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

  • Thread starter Thread starter Suxil_7
  • Start date Start date
  • Tags Tags
    Error Table
AI Thread Summary
The discussion centers on a Java Swing application that fails to display a table when the "Show" button is pressed, resulting in an error. The provided code attempts to connect to a MySQL database and retrieve data from a "student" table, but issues arise with the table display. Key points include the incorrect bounds set for the JScrollPane, which may prevent the table from being visible. Additionally, the table model is not properly initialized before adding rows, and the ResultSet processing logic could lead to errors if not structured correctly. Debugging techniques are emphasized, such as using a debugger or adding print statements to trace the execution flow and identify where the code fails.
Suxil_7
Messages
1
Reaction score
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
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
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
2
Views
5K
Replies
1
Views
1K
Replies
2
Views
2K
Replies
2
Views
3K
Replies
4
Views
5K
Replies
3
Views
3K
Back
Top