I have tried to populate the table but not working this is the code.
i am creating a small application to insert delete update my customer management system
i have the execution for the records table to get the data from the database. everything is running on my code there are no errors but my jtable is still not showing the data from my database.
package javaapplication16;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
public class NewJFrame extends javax.swing.JFrame {
private static final String DB_URL = "jdbc:mysql://localhost:3306/customer database?zeroDateTimeBehavior=CONVERT_TO_NULL";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "";
Connection sqlConn = null;
PreparedStatement pst = null;
ResultSet rs = null;
int q, i, id, deleteItem;
public NewJFrame() {
initComponents();
}
private void initializeDB() {
try {
Class.forName("com.mysql.jdbc.Driver");
sqlConn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
pst = sqlConn.prepareStatement("SELECT * FROM customer database");
rs = pst.executeQuery();
ResultSetMetaData stData = rs.getMetaData();
q= stData.getColumnCount();
DefaultTableModel RecordTable = (DefaultTableModel)jTable2.getModel();
RecordTable.setRowCount(0);
while(rs.next()){
obj columnData = new obj ();
for (i= 1; i<=q; i++){
columnData.add(rs.getString("id"));
columnData.add(rs.getString("name"));
columnData.add(rs.getString("age"));
columnData.add(rs.getString("order_number"));
}
RecordTable.addRow(columnData);
}
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {
DefaultTableModel RecordTable = (DefaultTableModel)jTable2.getModel();
int SelectedRows = jTable2.getSelectedRow();
txtID.setText(RecordTable.getValueAt(SelectedRows, 1).toString());
txtName.setText(RecordTable.getValueAt(SelectedRows, 2).toString());
txtAge.setText(RecordTable.getValueAt(SelectedRows, 3).toString());
txtOrderNo.setText(RecordTable.getValueAt(SelectedRows, 4).toString());
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel10;
private javax.swing.JPanel jPanel11;
private javax.swing.JPanel jPanel12;
private javax.swing.JPanel jPanel13;
private javax.swing.JPanel jPanel14;
private javax.swing.JPanel jPanel15;
private javax.swing.JPanel jPanel17;
private javax.swing.JPanel jPanel18;
private javax.swing.JPanel jPanel7;
private javax.swing.JPanel jPanel8;
private javax.swing.JPanel jPanel9;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTable jTable1;
private javax.swing.JTable jTable2;
private javax.swing.JButton jbtnAdd;
private javax.swing.JButton jbtnDelete;
private javax.swing.JButton jbtnExit;
private javax.swing.JButton jbtnPrint;
private javax.swing.JButton jbtnUpdate;
private javax.swing.JTextField txtAge;
private javax.swing.JTextField txtID;
private javax.swing.JTextField txtName;
private javax.swing.JTextField txtOrderNo;
// End of variables declaration
}
2
Answers
This is how you attempt to load the records from the database:
The query of
is syntactically incorrect. Your
table
is calledcustomers
, so you will need to change your query toI managed to get the original code in your question working for me. I located the classes
AbsoluteLayout
andAbsoluteConstraints
on GitHub. I fixed compilation errors in the original code that you posted, for example, the following:I changed it to:
And when I ran the code, I could not reproduce your problem. After entering data into the
JTextField
s and pressing the ADD button, theJTable
displayed the data.I assume that you are students and hence I believe that the code that you wrote is due to the fact that you don’t have much experience and therefore are unfamiliar (or unaware) of many aspects of Java (including Swing).
ActionListener
can be implemented as a lambda expression (or method reference) rather than an anonymous class.Class.forName
. In any case, there is definitely no need to call it each time you want to connect to the database. Once any class is loaded (by the JVM) it remains loaded so no need to reload it.I also assume that all those columns are VARCHAR columns since you treat them as strings in your code.
MouseListener
to handle row selection in theJTable
, you should use a selection listener.NewJFrame
to extendJFrame
. Many online examples of Swing applications do this but it is not required.JFrame
just so that you can provide an owner toJOptionPane
. Anyjava.awt.Component
subclass can serve as the owner.new java.awt.Color(0, 0, 0)
you can use the constantColor.black
.JTable
is 0 (zero) – and not 1 (one), hence methodjTable2MouseClicked
will throw an exception since there is no column with index 4.