i wanted to create a gui java program that reads info from MySql then output it in a JTable.
but i first tried to use the JTable because it was my first time to use it.
It worked perfectly but when i tried to read from the database it doesn't display the JTable although i don't read anydata from the database here is the code.
the function IDs() when called the JTable don't appear.
import java.awt.Dimension;
import java.sql.*;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public final class MyFrame extends JFrame{
String[] columnNames;
Object[][] data;
public MyFrame() throws ClassNotFoundException, SQLException{
setVisible(true);
setSize(900, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
int i,j;
String[] col={"ID"};
IDs();//when i remove this line the JTable appears
define();
//Create and set up the content pane.
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}//end of constructor
private void define(){
String[] columnNames = {"First Name","Last Name","Sport","# of Years","Vegetarian"};
Object[][] data = {
{"Kathy", "Smith","Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe","Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black","Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White","Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown","Pool", new Integer(10), new Boolean(false)}
};
this.columnNames=columnNames;
this.data=data;
}
private Statement connect() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1/buba","root","root");
return con.createStatement();
}
public Vector IDs() throws ClassNotFoundException, SQLException{
Vector vec=new Vector();
Statement stm=connect();
return vec;
}
}//end of class
please help i need to read data from the database