what's wrong with my code? i'm new to java, can someone help me? the data is not showing.
public class GUI {
private static JTextField fname = null;
private static JTextField lname = null;
private static JTextField search = null;
private JPanel panel = null;
private JPanel tablePanel = null;
private JFrame frame = null;
private JButton button1 = null;
private JButton button2 = null;
private static JTable table = null;
private JScrollPane scrollpne = null;
public JFrame getJFrame() {
frame = new JFrame();
frame.setTitle("testing");
frame.setSize(new Dimension(500, 500));
frame.setContentPane(getJPanel());
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addWindowListener(new windowEvent());
return frame;
}
public JPanel getJPanel(){
panel = new JPanel();
panel.setLayout(null);
panel.add(getJButton1(), null);
panel.add(getJButton2(),null);
panel.add(getJTextFieldfName());
panel.add(getJTextFieldlName());
panel.add(getJTextFieldSearch());
panel.add(getJpanelTable());
return panel;
}
public JPanel getJpanelTable(){
tablePanel = new JPanel();
panel.setLayout(null);
panel.setBorder(BorderFactory.createTitledBorder(null,
"Data Logs", TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, new Font("Dialog",
Font.BOLD, 13), new Color(51, 51, 51)));
panel.setBounds(new Rectangle(8, 107, 487, 201));
panel.add(getScrlPne());
return tablePanel;
}
public JButton getJButton1(){
button1 = new JButton();
button1.setText("Ok");
button1.setBounds(new Rectangle(200, 150, 100, 25));
button1.addActionListener(new buttonClick1());
return button1;
}
public JButton getJButton2(){
button2 = new JButton();
button2.setText("Search");
button2.setBounds(new Rectangle(200,200,100,25));
button2.addActionListener(new buttonClick2());
return button2;
}
public JTextField getJTextFieldfName(){
fname = new JTextField();
fname.setBounds(new Rectangle(170, 100, 150,25));
return fname;
}
public JTextField getJTextFieldlName(){
lname = new JTextField();
lname.setBounds(new Rectangle(90,50,150,25));
return lname;
}
public JTextField getJTextFieldSearch(){
search = new JTextField();
search.setBounds(new Rectangle(265,50,150,25));
return search;
}
public JScrollPane getScrlPne(){
scrollpne = new JScrollPane(table);
scrollpne.setBounds(new Rectangle(15, 270, 458, 160));
scrollpne.setViewportView(getTbl());
return scrollpne;
}
public JTable getTbl(){
table = new JTable();
table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
table.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
table.setEnabled(false);
return table;
}
static class windowEvent implements WindowListener{
public void windowOpened(WindowEvent arg0) {
String query = "SELECT * FROM person";
String url = "jdbc:mysql://localhost/names";
Connection con;
try {
con = DriverManager.getConnection(url, "root", "aldeene");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData md = rs.getMetaData();
int columnCount = md.getColumnCount();
Vector columns = new Vector(columnCount);
for(int i = 1; i <= columnCount; i++)
columns.add(md.getColumnName(i));
Vector data = new Vector();
Vector row;
while (rs.next()){
row = new Vector(columnCount);
for(int i=1; i <= columnCount; i++)
{
row.add(rs.getString(i));
}
data.add(row);
System.out.println("windows opened");
}
table = new JTable(data,columns);
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void windowActivated(WindowEvent arg0) {
}
public void windowClosed(WindowEvent arg0) {
}
public void windowClosing(WindowEvent arg0) {
}
public void windowDeactivated(WindowEvent arg0) {
}
public void windowDeiconified(WindowEvent arg0) {
}
public void windowIconified(WindowEvent arg0) {
}
}
static class buttonClick1 implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
String personfName;
String personlName;
personfName = fname.getText();
personlName = lname.getText();
try{
String url = "jdbc:mysql://localhost/names";
Connection con = DriverManager.getConnection(url, "root", "aldeene");
Statement stmt = (Statement) con.createStatement();
String insert = "INSERT INTO person VALUES ('" + personlName + "', '" + personfName + "')";
stmt.executeUpdate(insert);
System.out.println(personlName + personfName);
}catch(SQLException s) {
System.out.println("Cannot Insert To Database!");
s.printStackTrace();
}
}
}
static class buttonClick2 implements ActionListener{
public void actionPerformed(ActionEvent arg0){
String txtSearch;
txtSearch = search.getText();
String query = "SELECT * FROM person WHERE fname LIKE '" + txtSearch + "'";
String url = "jdbc:mysql://localhost/names";
Connection con;
try {
con = DriverManager.getConnection(url, "root", "aldeene");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()){
String firstName = rs.getString("fname");
String lastName = rs.getString("lname");
System.out.println(firstName);
System.out.println(lastName);
}
//JTable table = new JTable(data,columns);
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}