0

So I have a method retrieving data from a mysql database, and I store it in a 2 dimensional array 'values[i][k]';

Where i represents each individual record, and k presents each value in a record. How would I add this object to a JTable?

The values would look like this in the table, but I want it to be done automatically rather than me having to write it out manually...

values[0][0],values[0][1],values[0][2], values[0][3];

values[1][0],values[1][1],values[1][2], values[1][3];

values[2][0],values[2][1],values[2][2], values[2][3];

values[3][0],values[3][1],values[3][2], values[3][3];

Keeping in mind, values i and k could be any number, not a fixed value.

Any help is appreciated, thanks in advance :)

0

2 Answers 2

4

There is no need for you to create a 2 dimensional array to hold the data temporarily.

You can use the addRow(...) method of the DefaultTableModel as you read each row from the ResultSet.

Sign up to request clarification or add additional context in comments.

Comments

1

So I have a method retrieving data from a mysql database

As you have method to retrieve data from database and wants to store it in table you can do like this:

//Global Declaration

private Vector<Vector<String>> data; //used for data from database
private Vector<String> header; //used to store data header

//Display info to JTable

data = get();

//create header for the table
header = new Vector<String>();
header.add("Column1"); 
header.add("Column2");
...
model=new DefaultTableModel(data,header);
table = new JTable(model);

This will help you to get data from database

get(){
Vector<Vector<String>> doublevector = new Vector<Vector<String>>();

Connection conn = dbConnection();//Your Database connection code goes in dbConnection()
PreparedStatement pre1 = conn.prepareStatement("select * from Table");

ResultSet rs1 = pre1.executeQuery();
while(rs1.next())
{
Vector<String> singlevector = new Vector<String>();
singlevector.add(rs1.getString(1)); 
singlevector.add(rs1.getString(2)); 
....
doublevector.add(singlevector);

}

return doublevector;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.