I am writing a java swing application that collects data from textfields and stores it in a database. I have a JmenuItem that i want to use to display the data in the database. I have a class that collects the data from the database and inserts the data in the jtable. The class extends jframe. Now the problem i have is executing that class from the jmenuitem. When i click the jmenuitem it displays an empty jframe without the table.
here is the code for the class with the jtable. The class is Reports2
public class Reports2 extends JFrame {
Connection con = null;
Statement st;
ResultSet rs;
public static void main(String[]args) throws Exception {
try {
Connection con= null ;
Statement st;
ResultSet rs;
String url = "jdbc:mysql://localhost:3306/first";
String user = "root";
String password = "Admin123";
try{
con = DriverManager.getConnection(url,user,password);
st = con.createStatement();
String qry = "SELECT * FROM request";
rs = st.executeQuery(qry);
JTable table = new JTable(buildTableModel(rs));
JFrame frame = new JFrame("Reports");
frame.setSize(800, 700);
frame.add(table);
frame.setVisible(true);
} catch (SQLException ex) {
Logger.getLogger(Reports2.class.getName()).log(Level.SEVERE, null, ex);
}
}catch(Exception e){
}}
public static DefaultTableModel buildTableModel(ResultSet rs) throws Exception{
ResultSetMetaData metaData = rs.getMetaData();
Vector<String> columnNames = new Vector <String>();
int columnCount = metaData.getColumnCount();
for(int column = 1; column<=columnCount;column++){
columnNames.add(metaData.getColumnName(column));
}
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while(rs.next()){
Vector<Object> vector = new Vector<Object>();
for(int columnIndex =1;columnIndex<= columnCount;columnIndex++){
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
}
Here is the code of the jmenuitem. The Jmenuitem variable name is get_reports.
private void get_reportActionPerformed(java.awt.event.ActionEvent evt) {
Reports2 report = new Reports2();
report.setSize(800, 700);
report.setVisible(true);
You don't need a main
method in your Reports2
class. You main frame class already has a main
to launch from. Take out the main
method and wrap everything from that main
in a Report2
constructor
Your Reports2
class is already a JFrame
yet you are creating another JFrame
. Don't do that. instead just add
to the class JFrame
JTable table = new JTable(buildTableModel(rs));
//JFrame frame = new JFrame("Reports"); <--- take this out
//frame.setSize(800, 700); <--- don't set size, just pack
setTitle("Reports"); <--- set title
add(new JScrollPane(table)); <--- add to scrollpane
pack(); <--- pack
setVisible(true); <--- set visible
Notice how I took out the JFrame, since the class is already a JFrame
As seen above, I use .pack()
which is preferred rather than setting the size
And lastly, all you need to do in the actionPerformed is instantiate the Reports2
, that's it
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt)
{
Report2 report = new Report2();
}
I ran it, and it works fine with the above mentioned fixes. Given your SQL is correct, it should run for you also.
Side Note: NEVER, EVER swallow your exceptions
} catch (Exception ex) {
}
Put something meaningful that will allow you to see what exceptions are being thrown if any.
} catch (Exception ex) {
ex.printStackTrace();
}
Also I would consider using a JDialog
(instead of a second JFrame
) as you can control its modality. It's just as easy to create a JDialog
as it is to create a JFrame
. It's almost the exact same process