I want to bind mysql table content with JTable. I have connected sucessfully. I set table row and width as 500. It will shows 500 rows including data and empty. Now I want to view only data. I don't want to view empty rows.
Please help me
My application get slow after insert a method getRowCountFromDB()..
class AbstractTableEmployee extends AbstractTableModel {
int row2;
private String[] columnNames = { "ID", "Name", "Company", "Department", "Location", "Mobile NO" , "Shift" };
private Object[][] data = new Object[500][500];
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
int row = 0;
try {
row = this.count();
} catch (SQLException ex) {
Logger.getLogger(AbstractTableEmployee.class.getName()).log(Level.SEVERE, null, ex);
}
return row;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
private int getRowCountFromDB() throws SQLException {
Connection con = (Connection) DBConnection.getDBConnection();
int row = 0;
Statement st = null;
ResultSet rs = null;
String Sql = "Select * from Employee_Master where status = 'Active'";
try {
st = (Statement) con.createStatement();
rs = st.executeQuery(Sql);
while (rs.next())
{
row++;
}
}
finally {
con.close();
rs.close();
st.close();
}
return row;
}
private int count() throws SQLException {
return this.getRowCountFromDB();
}
}
Thanks in advance.
The documentation for JTable gives the following example for how to use a table with a custom table model.
TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() { return 10; }
public int getRowCount() { return 10;}
public Object getValueAt(int row, int col) { return new Integer(row*col); }
};
JTable table = new JTable(dataModel);
JScrollPane scrollpane = new JScrollPane(table);
This is the minimal implementation that you'd have to provide.
Edit:
See 3.3.4.8 Counting Rows in the MySQL Reference for information on how to get a row count efficiently. It seems to me that you're fetching an entire table, when all you want is a number.
You'd want something along the lines of
Select COUNT(*) from Employee_Master where status = 'Active'
and then just reading that value.
As for your data, I see no reason for it to have 500 columns, given that your table seems to have only 7.
Anyway, depending on the size of your table, you might not want to have all the data in there at once. If that's the case, you can always fetch and cache chunks of rows, according to where in the JTable your user is scrolling. You could use LIMIT for that.
For some other ideas and tips, you should also take a look at this question which covers a similar problem.
as mentioned by another posters better would be read JTable tutorial before, since little bit crypted in the text, your method is JTable#setValueAt(Object aValue, int row, int column),
Related
I've been working on a web-service that returns an arraylist. How can I add the returning arraylist to jtable and display?
ArrayList customerDetails = new ArrayList();
try {
String sqlQuery = "SELECT * FROM customer WHERE AccountNumber="+accountNumber;
PreparedStatement stmt = DatabaseConnection.dBconn().prepareStatement(sqlQuery);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
customerDetails.add(rs.getString("Name"));
customerDetails.add(rs.getString("DoB"));
customerDetails.add(rs.getString("Address"));
customerDetails.add(rs.getString("Mobile"));
customerDetails.add(rs.getString("Email"));
customerDetails.add(rs.getString("AccountType"));
customerDetails.add(rs.getString("AccountNumber"));
customerDetails.add(rs.getString("SortCode"));
customerDetails.add(rs.getString("Balance"));
customerDetails.add(rs.getString("Card"));
}
return customerDetails;
} catch (SQLException err) {
System.out.println(err.getMessage());
}
return customerDetails;
Let's start with the fact that your ArrayList is not structured as a row/columns grouping, you will need a List within a List, where the outer list is the rows and the inner list are the column values
While we're at it, let's also make use of the PreparedStatement properly and manage the resources so they are closed properly while we're at it
ArrayList<List<String>> customerDetails = new ArrayList<>(25);
String sqlQuery = "SELECT * FROM customer WHERE AccountNumber=?";
try (PreparedStatement stmt = DatabaseConnection.dBconn().prepareStatement(sqlQuery)) {
stmt.setString(1, accountNumber);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
List<String> rowDetails = new ArrayList<>(10);
rowDetails.add(rs.getString("Name"));
rowDetails.add(rs.getString("DoB"));
rowDetails.add(rs.getString("Address"));
rowDetails.add(rs.getString("Mobile"));
rowDetails.add(rs.getString("Email"));
rowDetails.add(rs.getString("AccountType"));
rowDetails.add(rs.getString("AccountNumber"));
rowDetails.add(rs.getString("SortCode"));
rowDetails.add(rs.getString("Balance"));
rowDetails.add(rs.getString("Card"));
customerDetails.add(rowDetails);
}
}
} catch (SQLException err) {
System.out.println(err.getMessage());
}
return customerDetails;
Have a look at Using Prepared Statements and The try-with-resources Statement for more details
Now, we need a TableModel which can support it, at very basic level...
public class ListTableModel extends AbstractTableModel {
private List<List<String>> rows;
private List<String> columnNames;
public ListTableModel(List<String> columnNames, List<List<String>> rows) {
this.rows = new ArrayList<>(rows);
this.columnNames = columnNames;
}
#Override
public int getRowCount() {
return rows.size();
}
#Override
public int getColumnCount() {
return columnNames.size();
}
#Override
public String getColumnName(int column) {
return columnNames.get(column);
}
#Override
public Class<?> getColumnClass(int columnIndex) {
Class type = String.class;
return type;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
List<String> rowData = rows.get(rowIndex);
return rowData.get(columnIndex);
}
}
This takes a List for the column names and a List<List> for the row data.
Personally, I'd prefer to wrap the data into some kind of Plain Old Java Object (POJO) as it encapsulates the data and provides greater flexibility when displaying it (ie, I need to display all the properties of the object if I don't want to)
Take a look at How to Use Tables for more details
I'm working on a Swing app which is connected to MySQL database. It fetches some data from the DB and shows in the table. Besides, there are 3 buttons that suppose to changes table data ( Add row to table ), update the database (Update database ) and to discard the changes ( Discard changes ) as following,
After clicking the Add row to table button, the new entries from the form should be added in the table. Afterwards if the update button is clicked, the data will be updated in the db. Discard changes will delete the data from the last row of the table and will have nothing to do with db. The app has two classes CoffeesFrame.java and CoffeesTableModel.java I provided the sample code from both of the classes below,
public class CoffeesFrame extends JFrame implements RowSetListener {
private static final long serialVersionUID = 1L;
private static Connection myConn = null;
private static String url = "jdbc:mysql://localhost:3306/myDemo";
private static String user = "student";
private static String password = "student";
// initiate a table object
JTable table;
// labels of the table
// text data fields of the table
// buttons of the table
// initiate a table model object
CoffeesTableModel myCoffeesTableModel;
// table constructor
public CoffeesFrame(Connection myConn) throws SQLException {
table = new JTable();
createNewTableModel(myConn);
/*
label, text field and buttons
*/
Container contentPane = getContentPane();
contentPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
contentPane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
/*
Code for making the GUI
*/
button_ADD_ROW.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// showMessageDialog works
JOptionPane.showMessageDialog(CoffeesFrame.this, new String[] {
"Adding the following row:",
"Coffee name: [" + textField_COF_NAME.getText() + "]",
"Supplier ID: [" + textField_SUP_ID.getText() + "]",
"Price: [" + textField_PRICE.getText() + "]",
"Sales: [" + textField_SALES.getText() + "]",
"Total: [" + textField_TOTAL.getText() + "]" });
try {
// Insert row is not adding data to the table
myCoffeesTableModel.insertRow(
textField_COF_NAME.getText(),
Integer.parseInt(textField_SUP_ID.getText().trim()),
Float.parseFloat(textField_PRICE.getText().trim()),
Integer.parseInt(textField_SALES.getText().trim()),
Integer.parseInt(textField_TOTAL.getText().trim()));
// table.getModel();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
});
button_UPDATE_DATABASE.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
myCoffeesTableModel.coffeesRowSet.acceptChanges(myConn);
}
catch (Exception ex) {
ex.printStackTrace();
}
try {
createNewTableModel(myConn);
}
catch (Exception el) {
el.printStackTrace();
}
}
});
button_DISCARD_CHANGES.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("The changes are DISCARDED");
}
});
}
private void createNewTableModel(Connection myConn) throws SQLException {
myCoffeesTableModel = new CoffeesTableModel(getContentsOfCoffeesTable(myConn));
myCoffeesTableModel.addEventHandlersToRowSet(this);
table.setModel(myCoffeesTableModel);
}
#Override
public void rowSetChanged(RowSetEvent event) {
// TODO Auto-generated method stub
}
#Override
public void rowChanged(RowSetEvent event) {
// TODO Auto-generated method stub
}
#Override
public void cursorMoved(RowSetEvent event) {
// TODO Auto-generated method stub
}
public CachedRowSet getContentsOfCoffeesTable(Connection mycConn)
throws SQLException {
CachedRowSet crs = null;
ResultSet resultSet = null;
Statement stmt = null;
String sql = "select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES";
try {
stmt = myConn.createStatement();
resultSet = stmt.executeQuery(sql);
crs = new CachedRowSetImpl();
crs.populate(resultSet);
}
catch (Exception e) {
e.printStackTrace();
}
return crs;
}
public static void main(String[] args) throws SQLException {
try {
myConn = DriverManager.getConnection(url, user, password);
if (myConn != null) {
System.out.println("Connected to the database myDemo");
}
}
catch (SQLException ex) {
System.out
.println("An error occurred. Maybe user/password is invalid");
ex.printStackTrace();
}
myConn.setAutoCommit(false);
CoffeesFrame coffeesFrame = new CoffeesFrame(myConn);
coffeesFrame.pack();
coffeesFrame.setVisible(true);
}
}
The CoffeesTableModel.java as follows, //
public class CoffeesTableModel implements TableModel {
CachedRowSet coffeesRowSet; // The ResultSet to interpret
ResultSetMetaData metadata; // Additional information about the results
int numcols, numrows; // How many rows and columns in the table
public CoffeesTableModel(CachedRowSet rowSetArg) throws SQLException {
this.coffeesRowSet = rowSetArg;
this.metadata = this.coffeesRowSet.getMetaData();
numcols = metadata.getColumnCount();
// Retrieve the number of rows.
this.coffeesRowSet.beforeFirst();
this.numrows = 0;
while (this.coffeesRowSet.next()) {
this.numrows++;
}
this.coffeesRowSet.beforeFirst();
}
public CachedRowSet getCoffeesRowSet() {
return coffeesRowSet;
}
public void addEventHandlersToRowSet(RowSetListener listener) {
this.coffeesRowSet.addRowSetListener(listener);
}
public void insertRow(String coffeeName, int supplierID, float price,
int sales, int total) throws SQLException {
try {
this.coffeesRowSet.moveToInsertRow();
this.coffeesRowSet.updateString("COF_NAME", coffeeName);
this.coffeesRowSet.updateInt("SUP_ID", supplierID);
this.coffeesRowSet.updateFloat("PRICE", price);
this.coffeesRowSet.updateInt("SALES", sales);
this.coffeesRowSet.updateInt("TOTAL", total);
this.coffeesRowSet.insertRow();
this.coffeesRowSet.moveToCurrentRow();
}
catch (SQLException e) {
e.printStackTrace();
// JDBCTutorialUtilities.printSQLException(e);
}
}
public void close() {
try {
coffeesRowSet.getStatement().close();
}
catch (SQLException e) {
e.printStackTrace();
// JDBCTutorialUtilities.printSQLException(e);
}
}
/** Automatically close when we're garbage collected */
protected void finalize() {
close();
}
/** Method from interface TableModel; returns the number of columns */
public int getColumnCount() {
return numcols;
}
/** Method from interface TableModel; returns the number of rows */
public int getRowCount() {
return numrows;
}
/**
* Method from interface TableModel; returns the column name at columnIndex
* based on information from ResultSetMetaData
*/
public String getColumnName(int column) {
try {
return this.metadata.getColumnLabel(column + 1);
}
catch (SQLException e) {
return e.toString();
}
}
/**
* Method from interface TableModel; returns the most specific superclass
* for all cell values in the specified column. To keep things simple, all
* data in the table are converted to String objects; hence, this method
* returns the String class.
*/
public Class getColumnClass(int column) {
return String.class;
}
/**
* Method from interface TableModel; returns the value for the cell
* specified by columnIndex and rowIndex. TableModel uses this method to
* populate itself with data from the row set. SQL starts numbering its rows
* and columns at 1, but TableModel starts at 0.
*/
public Object getValueAt(int rowIndex, int columnIndex) {
try {
this.coffeesRowSet.absolute(rowIndex + 1);
Object o = this.coffeesRowSet.getObject(columnIndex + 1);
if (o == null)
return null;
else
return o.toString();
}
catch (SQLException e) {
return e.toString();
}
}
/**
* Method from interface TableModel; returns true if the specified cell is
* editable. This sample does not allow users to edit any cells from the
* TableModel (rows are added by another window control). Thus, this method
* returns false.
*/
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
// Because the sample does not allow users to edit any cells from the
// TableModel, the following methods, setValueAt, addTableModelListener,
// and removeTableModelListener, do not need to be implemented.
public void setValueAt(Object value, int row, int column) {
System.out.println("Calling setValueAt row " + row + ", column "
+ column);
}
public void addTableModelListener(TableModelListener l) {
}
public void removeTableModelListener(TableModelListener l) {
}
}
The data shows in the table is grabbed from the db and presented as table. The "Add row to table" shows the pop-up from the form insertion but don't put the data as new row in the end of the table. However, I'm not getting any error. The "Update database" button is not working and provides java.sql.SQLException: Table not specified.. How can I improve the code ?
Instead of implementing the TableModel interface, you should be extending from the AbstractTableModel, this has default functionality of some of the "normal" functionality you don't want to replicated.
Remove the addTableModelListener and removeTableModelListener, your current implementation has essentially broken the model's contract by not providing the ability to make notifications to other observers (like the JTable)
Your insertRow method MUST call fireTableRowsInserted (from AbstractTableModel) in order to notify the JTable that the model has changed and that it needs to update itself with the new data
Java makes no guarantees that finalize will be called, don't rely on it
getRowCount will be effected by the size of the RowSet, therefore, you need to be able to either keep a running value (ie you need to increment it when you add new rows) or calculate the number of rows from the RowSet itself. I believe you can do this by moving the cursor to the end (or beyond the last position) and using getRow, don't forget though, ResultSet is 1 based, not 0 based which JTable expects
I'm starting to learn how to use databases and was trying to export the data from my h2 database into a JTable. The table comes up with the correct number of rows, however, only the first row is filled with data. The rest is a blank grid. I posted some code below for the JTable. If someone needs to see more code, I'll post it.
public class Table extends JTable{
public static int rows;
public static String[][] data;
public static String[] columns = {"Author", "Customer", "Date"};
public static void populateTable() throws ClassNotFoundException, SQLException{
//Server is name of the database class
Server server = new Server();
Statement stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stat.executeQuery("SELECT * FROM data");
rs.last();
rows = rs.getRow();
rs.beforeFirst();
data = new String[3][rows];
while(rs.next()){
int i = 0;
data[0][i] = rs.getString("Author");
data[1][i] = rs.getString("Customer");
data[2][i] = rs.getString("Date");
System.out.println(rs.getString("Author"));
i = i++;
}
rs.close();
}
}
class MyTableModel extends DefaultTableModel{
String[] columnNames = {"Author", "Customer", "Date"};
MyTableModel() throws ClassNotFoundException, SQLException{
addColumn(columnNames[0]);
addColumn(columnNames[1]);
addColumn(columnNames[2]);
}
#Override
public int getRowCount() {
return rows;
}
#Override
public int getColumnCount() {
return 3;
}
#Override
public String getColumnName(int columnIndex) {
return columnNames[columnIndex];
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data[columnIndex][rowIndex];
}
I'm also able to print to the console all the data, but it just won't show up in the JTable. I have been stuck on this problem for hours but I don't know what I'm doing wrong. Thanks in advance
This statement is a no-op since i is assigned before it is incremented
i = i++;
just use
i++;
Also initialize i before entering the loop
You should either use a for loop or declare i outside of your loop. As it stands, you are setting all data to row 0 (int i = 0);
while(rs.next()){
int i = 0; // this will run for every row
data[0][i] = rs.getString("Author");
data[1][i] = rs.getString("Customer");
data[2][i] = rs.getString("Date");
System.out.println(rs.getString("Author"));
i = i++;
}
I am trying to create a checkout simulation for my coursework. So every time I search for an item I can retrieve it from the database and display it on the JTable. However, once I add an item to the list and try to add another item the old item get replaced by the new item.
I am trying to list all the item in the JTable, this is my code:
DBConnection db = new DBConnection();
try {
ResultSet rs = DBConnection.stmt.executeQuery("SELECT ID, MESSAGE FROM STOCK WHERE ID = '"+ id + "'");
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}
catch (Exception e){
System.out.println(e);
}`
The main problem is DbUtils.resultSetToTableModel(rs), which is creating a brand new TableModel, filled with the contents of the ResultSet, this, when applied to the JTable is replacing the view with the contents of the TableModel.
In order to be able to update the table, you need to update the existing TableModel...
There are a few ways this might be achieved, by the simplest might be to use a DefaultTableModel...
Start by creating a class instance field of a DefaultTableModel...
public class ... {
//...
private DefaultTableModel stockTableModel;
//...
Then, when you want to load the stock items, you will need to initialise the model, if it's not already initialised, and then add the new results to it...
DBConnection db = new DBConnection();
try (ResultSet rs = DBConnection.stmt.executeQuery("SELECT ID, MESSAGE FROM STOCK WHERE ID = '" + id + "'")) {
if (stockTableModel == null) {
stockTableModel = new DefaultTableModel();
for (int col = 0; col < metaData.getColumnCount(); col++) {
stockTableModel.addColumn(metaData.getColumnName(col + 1));
}
jTable.setModel(model);
}
while (rs.next()) {
Vector rowData = new Vector(metaData.getColumnCount());
for (int col = 0; col < metaData.getColumnCount(); col++) {
rowData.add(rs.getObject(col + 1));
}
stockTableModel.addRow(rowData);
}
} catch (SQLException exp) {
exp.printStackTrace();
}
Take a look at How to Use Tables and JDBC Database Access for more details
You can create a custom data model that allows you to insert new rows to table.
lets say that you have class, that can hold your query result fields.
public class Item implements Comparable<Item> {
private Long id;
private String message;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
public String getMessage() {
return message;
}
public void setMessage(String value) {
this.message= value;
}
#Override
public int compareTo(Item o) {
return id.compareTo(o.id);
}
}
and it needs to go to table, which has been defined somewhere like:
JTable table =new JTable();
this is a data model to your table
public class Model extends AbstractTableModel {
private List<Item> items;
public Model() {
items = new ArrayList<>();
}
#Override
public int getRowCount() {
return items.size();
}
#Override
public int getColumnCount() {
return 3;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
if (rowIndex > items.size() - 1 || rowIndex < 0) {
return "";
}
final Item get = items.get(rowIndex);
switch (columnIndex) {
case 0:
return get.getId();
case 1:
return get.getMessage();
}
return "";
}
#Override
public String getColumnName(int column) {
switch (column) {
case 0:
return "id";
case 1:
return "message";
}
return "";
}
public void addItem(Item i) {
items.add(i);
fireTableDataChanged();
}
public void addItem(ResultSet rs) {
try {
Item item = new Item();
item.setId(rs.getLong("ID"));
item.setMessage(rs.getString("MESSAGE"));
items.add(item);
fireTableDataChanged();
} catch (SQLException ex) {
Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
now create field
Model myModel=new Model();
and set it as a table model
table.setModel(myModel);
now every time you need to add something to table, just use our table model (i created two methods to insert data public void addItem(Item i) and public void addItem(ResultSet rs).
this should work. If you need to clear table sometimes, just add pubic method public void clear() to your model, in which you will clear items list and call fireTableDataChanged();. It is necessary, otherwise GUI will not refresh.
EDIT
Your code should be like
DBConnection db = new DBConnection();
try {
ResultSet rs = DBConnection.stmt.executeQuery("SELECT ID, MESSAGE FROM STOCK WHERE ID = '" + id + "'");
myModel.add(rs);
} catch (Exception e) {
System.out.println(e);
}
Just add a row to your JTable Model every time you have your result...
refer to this SO question
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.addRow(new Object[]{"Column 1", "Column 2", "Column 3"});
or in your case
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.addRow(new Object[]{searchResultData});
This question already has an answer here:
TableModel removeRow() definition [closed]
(1 answer)
Closed 9 years ago.
This is my tableModel:
public class d9 extends AbstractTableModel {
ArrayList<String> cols = new ArrayList<>();
ArrayList<ArrayList<String>> data = new ArrayList<>();
public d9() {
...
int c = resultSet.getMetaData().getColumnCount();
while (resultSet.next()) {
ArrayList<String> eachRow = new ArrayList<>();
for (int i = 1; i <= c; i++) {
eachRow.add(resultSet.getString(i));
}
data.add(eachRow);
}
...
}
#Override
public int getRowCount() {
return data.size();
}
#Override
public int getColumnCount() {
return cols.size();
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
ArrayList<String> selectedRow = data.get(rowIndex);
return selectedRow.get(columnIndex);
}
#Override
public String getColumnName(int column) {
return cols.get(column);
}
public void removeRow(int rowNumber) {
data.remove(rowNumber);
fireTableRowsDeleted(rowNumber, rowNumber);
}
}
Now, after passing a convertRowIndexToModel line number to removeRow method
Row remove from table, But after re-run program, It come back!
When you call removeRow you need to try and remove the row from the database.
Now because I have no idea what the structure of your database is, you will need to fill in the details, but this a simple outline of what you need to do
public void removeRow(int rowNumber) throws SQLException {
Connection con = ...;
PreparedStatement ps = null;
String keyValue = ...; // Get key value from the ArrayList
try {
ps = con.prepareStatement("DELETE from youDatabaseTabe where key=?");
ps.setObject(1, keyValue);
if (ps.executeUpdate() == 1) {
data.remove(rowNumber);
fireTableRowsDeleted(rowNumber, rowNumber);
} else {
throw new SQLException("Failed to remove row from database");
}
} finally {
try {
ps.close();
} catch (Exception e) {
}
}
}
You may want to spend some time having a read through JDBC Database Access