Hi! I have a database which i can connect to and get information from, but i can't figure out how to insert my resultset into my jTable from two different classes, I found an good exaple on the web but i dont know how to split up the code. I work with the GUI editor in NEtbeans 6.8 since i'm most familiar with it.
I have bound an jButton (that lies in Class A) that will call an method in Class B which will update the jTable.
The action event code
//Creates object "b" from the class ClassB
ClassB b = new ClassB();
try {
//Call method showall in ClassB
b.showall();
} catch (ClassNotFoundException ex) {
System.out.println("Class not found);
}
{code}
And here is the method "showall"
{code}
String sql = "my sql query";
ResultSet rs = dc.query(sql);
Object[] rows;
while (rs.next()) {
//add the values to the temporary row
rows = new Object[]{rs.getInt(0), rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)};
// add the temp row to the table
// Startup is the name of my mainclass wich i have the jTable in (named "table")
Startup.table.setValueAt(rows, 1, 1);
{code}
This is the example i found
{code}
class MySQLTable
{
private static Connection con = null;
private static String URL = "jdbc:mysql://localhost:3306";
private static String driver = "com.mysql.jdbc.Driver";
private static String user = "root";
private static String pass = "";
/**
* Main aplication entry point
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException
{
// a MySQL statement
Statement stmt;
// a MySQL query
String query;
// the results from a MySQL query
ResultSet rs;
// 2 dimension array to hold table contents
// it holds temp values for now
Object rowData[][] = {{"Row1-Column1", "Row1-Column2", "Row1-Column3"}};
// array to hold column names
Object columnNames[] = {"ID", "User", "Password"};
// create a table model and table based on it
DefaultTableModel mTableModel = new DefaultTableModel(rowData, columnNames);
JTable table = new JTable(mTableModel);
// try and connect to the database
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(URL, user,pass);
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
}
// run the desired query
query = "SELECT ID, User, Password FROM users.normal";
// make a statement with the server
stmt = con.createStatement();
// execute the query and return the result
rs = stmt.executeQuery(query);
// create the gui
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(300, 150);
frame.setVisible(true);
// remove the temp row
mTableModel.removeRow(0);
// create a temporary object array to hold the result for each row
Object[] rows;
// for each row returned
while (rs.next()) {
// add the values to the temporary row
rows = new Object[]{rs.getString(1), rs.getString(2), rs.getString(3)};
// add the temp row to the table
mTableModel.addRow(rows);
}
}
private MySQLTable()
{
}
}
{code}
I would appreciate some help with this :)