I have designed a Jtable using Netbeans GUI in Swing.Now as per my requirement i have to display database table values into jtable.
Here is my code ..
Statement statement = (Statement) con.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
ResultSetMetaData metaData = (ResultSetMetaData) resultSet.getMetaData();
DefaultTableModel dtm = new DefaultTableModel();
int columns = metaData.getColumnCount();
Vector Column_Name = new Vector();
Vector data_rows = new Vector();
for (int i = 1; i < columns; i++) {
Column_Name.addElement(metaData.getColumnName(i));
}
dtm.setColumnIdentifiers(Column_Name);
while (resultSet.next()) {
data_rows = new Vector();
for (int j = 1; j < columns; j++) {
data_rows.addElement(resultSet.getString(j));
}
}
dtm.addRow(data_rows);
jTable1.setModel(dtm);
resultSet.close();
Why is this code displaying only one row of my database into jtable when there so many rows in the table.
Please help me to solve this.
Each time you overwrite you data, because you have added the row just one time, which is currently after the while loop
So try to move the adding of row data in the loop like:
while (resultSet.next()) {
data_rows = new Vector();
for (int j = 1; j < columns; j++) {
data_rows.addElement(resultSet.getString(j));
}
dtm.addRow(data_rows);
}
Related
I am using JDBC to run a SQL query in Java. I want to take the result of the query and store it in an arraylist so that I can display the data in a graph of some sort. I'm getting the same line printing out the same number of times as columnCount. Here is my code.
ArrayList <String[]> result = new ArrayList<String[]>();
int columnCount = rset.getMetaData().getColumnCount();
if(rset!=null)
{
while(rset.next())
{
found=true;
String[] row = new String[columnCount];
for (int i = 0; i < columnCount; i++) {
row[i] = rset.getString(i + 1);
row[i] = rset.getString("Date") + " "
+ rset.getString("Hour");
System.out.println(row[i]);
}
result.add(row);
}
Your second row[i] rewrites the value of the column. Just remove it and you'll see your records:
...
for (int i = 0; i < columnCount; i++) {
row[i] = rset.getString(i + 1);
}
System.out.println(Arrays.toString(row));
result.add(row);
...
I'm currently trying to fill an array list with an object of String[] with a SQL table. I was able to get the headers of my tables using this code.
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
int i = 1;
while (colCount >= i) {
String header = rsmd.getColumnName(i);
field.add(header);
i++;
However, when I'm trying to create my rows to fill the table, the array is not adding to the array list.
while (rs.next()) {
String[] row = new String[colCount];
for (int j = 0; j <= colCount; j++) {
row[j] = rs.getString(j+1);
}
data.add(row);
}
} catch (SQLException ex) {
}
System.out.println(data.size());
For some reason when I put the "data.add(row);" statement in the for loop, it only shows the same row however many times it loops. But it doesn't work outside of the for (I want to put it after the for loop inside the while). I'm outputting the size of the list to the console and it just returns 0.
String[] row = new String[colCount];
for (int j = 0; j <= colCount; j++) {
row[j] = rs.getString(j+1);
}
data.add(row);
Dear my friends, why did you decided to write "j <= colCount" meanwhile you had created "j = 0". So your code is going to throw "SqlException". Because index of last element of result set is "colCount", but your loop passed index equals to j + 1. it means equal "colCount + 1".
As you know, you try catch SQLException but you don't do anything. So your code has been failed when first row of resultset and j = colCount + 1. It can't add anything.
//Using MySql Database. creating a search query for mobile phone from IMEI No. //in swing based desktop application
int coulmn_count=rsmd.getColumnCount();
DefaultTableModel dtm=new DefaultTableModel();
Vector column_name=new Vector();
Vector data_rows=new Vector();
for (int i = 1; i <=coulmn_count; i++) {
column_name.addElement(rsmd.getColumnName(i));
}
dtm.setColumnIdentifiers(column_name);
JOptionPane.showMessageDialog(null,+coulmn_count);
while(rs.next())
{
for (int j = 0; j <coulmn_count; j++) {
data_rows=new Vector();
data_rows.addElement(rs.getString(j));
}
dtm.addRow(data_rows);
}
//JOptionPane.showMessageDialog(null,+coulmn_count);
table.setModel(dtm);
The ResultSet is 1 based, not 0 based:
It looks like you got the code correct for the heading names:
for (int i = 1; i <=coulmn_count; i++) {
column_name.addElement(rsmd.getColumnName(i));
}
But then you use 0 based for the column data.
for (int j = 0; j <coulmn_count; j++) {
data_rows=new Vector();
data_rows.addElement(rs.getString(j));
}
Fix the code to start the index from 1.
In a ResultSet from either Oracle,MySQl or MSSQL, how can you make the resultset for loop quicker?
while (rs.next()) {
for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
rs.getString(i)
}
}
This works great as long as you don't have to many columns, but as soon as you have for example 90 columns and 100 000 rows, then this for loops goes trough 100 000 rows 90 times.
One thing to try would be to just get the column count once:
int colCount = rs.getMetaData().getColumnCount();
while (rs.next()) {
for (int i = 0; i < colCount; i++) {
rs.getString(i)
}
}
In my table. I want to put some buttons into each row that I can press.
But I do not know how to do it
public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {
java.sql.ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
For doing this you have to first look at,
Editors and Renderers
How to render a cell in JTable as Button example.
Not only the above stated example, you have many sources which shows how to add a button in a JTable cell.