How can I populate my JTable with my embedded database? - java

I am having trouble showing my database filled JTable within my GUI application.
My code below runs through and creates a GUI with a panel, I then create my JTable and add it onto my application. I then run through a method that supposedly populates the table. After the populating has finished, nothing shows.
Dissecting my code, I'm led to believe somewhere is causing the data not to parse into my table, for some unknown reason, which is why I have come here.
At the click of a button, this code entails:
JTable tigerTable = new JTable();
JPanel centerPanel = new JPanel();
centerPanel.add(tigerTable, new GridBagConstraints());
FillTable(tigerTable, "SELECT * FROM TIGER_INFO");
The FillTable method as follows:
//Add buildTableModel method
public void FillTable(JTable table, String Query)
{
try
{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:derby:STOCK_CONTROL");
Statement stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stat.executeQuery(Query);
System.out.println("Connected");
//Remove previously added rows
while (table.getRowCount() > 0)
{
((DefaultTableModel) table.getModel()).removeRow(0);
}
int columns = rs.getMetaData().getColumnCount();
while (rs.next())
{
Object[] row = new Object[columns];
for (int i = 1; i <= columns; i++)
{
row[i - 1] = rs.getObject(i);
}
((DefaultTableModel) table.getModel()).insertRow(rs.getRow() - 1, row);
}
rs.close();
stat.close();
conn.close();
}
catch (InstantiationException |
IllegalAccessException |
ClassNotFoundException |
SQLException e)
{
System.out.println(e);
e.printStackTrace();
}
}
Doing so creates the application, but does not show any table with data. My database contains 3 columns and 3 rows, but I do not see my data displayed inside a JTable.
My question is, how can I populate my JTable with my database and display it correctly on my GUI application?
If you need anything else, please let me know and I will provide as much as I can.
Thank you in advance.

I think you mix the operation, i suggest to use this instruction :
Create a Bean, or Entity to store the information of your Object
Get the List of your data,
Display your data in your JTable,
So you can use this to display your data in your JTable :
....
List<TIGER_INFO> list = new ArrayList<>();//create a List of data
while (rs.next()){
//fill data in your List
list.add(new TIGER_INFO(rs.getTYPExxx("att1"), rs.getTYPExxx("att2"), rs.getTYPExxx("att3"));
}
//when you finish call your function which display your data :
fillData(list);
....
Fill date method can look like this :
private void fillData(List<TIGER_INFO> list) {
DefaultTableModel model = (DefaultTableModel) jTableName.getModel();
while (model.getRowCount() > 0) {
for (int i = 0; i < model.getRowCount(); i++) {
model.removeRow(i);
}
}
for (TIGER_INFO obj : list) {
model.addRow(new Object[]{
obj.getAttribute1(),
obj.getAttribute2,
obj.getAttribute3
});
}
}

I recommend to write a class extending AbstractTableModel. Add a function to this class filling the content based on a SQL-Statement. Then it will look something like:
TigerTableModel tigerModel = new TigerTableModel(dbConnection);
tigerModel.executeQuery("select * FROM TIGER_INFO");
JTable tigerTable = new JTable(tigerModel);
or you create a generic TableModel that is able to run every SQL-statement, so you are very flexible in your project.

I have found the solution - I've been trying to wrap my head around the problem for way too long, and I finally fixed it.
Try setting the table model manually first.
JTable tigerTable = new JTable();
tigerTable.setModel(new DefaultTableModel(0, numberOfColumns));
When you don't specify a model, the JTable creates an anonymous inner class and generally doesn't behave how you want it to.

Related

The 'getRow()' method is only allowed on scroll cursors SQLException error

I am trying to populate my JTable from my embedded database that I have set up in Netbeans.
My database contains 3 rows and 3 columns that I'd like to insert into a JTable, and make the table visible in my GUI application.
However, I am getting a java.sql.SQLException error as the title says above and my table will not turn visible in my GUI application.
Here's is my code:
public void FillTable(JTable table, String Query)
{
try
{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:derby:STOCK_CONTROL");
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(Query);
//Remove previously added rows
while (table.getRowCount() > 0)
{
((DefaultTableModel) table.getModel()).removeRow(0);
}
int columns = rs.getMetaData().getColumnCount();
while (rs.next())
{
Object[] row = new Object[columns];
for (int i = 1; i <= columns; i++)
{
row[i - 1] = rs.getObject(i);
}
//The error is being generated here at 'rs.getRow()'
((DefaultTableModel) table.getModel()).insertRow(rs.getRow() - 1, row);
}
rs.close();
stat.close();
conn.close();
}
catch (InstantiationException |
IllegalAccessException |
ClassNotFoundException |
SQLException e)
{
System.out.println(e);
e.printStackTrace();
}
}
I then create my table and call the method as above:
JTable tigerTable = new JTable();
FillTable(tigerTable, "SELECT * FROM TIGER_INFO");
I've tried to find information as to why this error is being caused, but to no avail.
My question is, how do I correctly populate my JTable from my embedded database, whilst also straying away from this unknown error I have come about?
What does this error exactly mean? I'm unsure what a 'scroll cursor' is.
As documented in ResultSet.getRow():
Note: Support for the getRow method is optional for ResultSets with a result set type of TYPE_FORWARD_ONLY
[..]
Throws:
[..]
SQLFeatureNotSupportedException - if the JDBC driver does not support this method
You either need to ask for a scrollable cursor using
conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
Or - probably better - keep track of the row number yourself instead of using ResultSet.getRow(). For example:
int rowIdx = 0;
while (rs.next()) {
Object[] row = new Object[columns];
// ...
((DefaultTableModel) table.getModel()).insertRow(rowIdx++, row);
}
It is been a while since I have done anything with swing, but it might even be simpler to just remove existing rows from the table model and call addRow.

Modifying column headers of JTable with MySQL data

I have a JTable that retrieves information from a MySQL database table. The column headers are named just like how they are in the database.
Here is the code to create the JTable:
JScrollPane spBlockViewSchedule = new JScrollPane();
spBlockViewSchedule.setBounds(10, 285, 763, 185);
pnlBlockSched.add(spBlockViewSchedule);
tblBlockViewSchedule = new JTable();
spBlockViewSchedule.setViewportView(tblBlockViewSchedule);
Here is the code that populates the JTable:
private void populateTable(String sql, JTable table) {
try {
pst = DbConnection.conn.prepareStatement(sql);
rs = pst.executeQuery();
} catch(Exception ex) {
ex.printStackTrace();
}
table.setModel(DbUtils.resultSetToTableModel(rs));
}
How do I change the column names displayed in the JTable without changing the column names of the database table itself?
Create an empty DefaultTableModel with code like:
String[] columnNames = {"Course Code", "Subject Code", "Year Level", ...};
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
Then in the code where you read the data from the ResultSet you add the data to the TableMOdel using the addRow(....) method. Something like:
while (rs.next())
{
Vector row = new Vector();
for (int i = 1; i <= columns; i++)
{
row.addElement( rs.getObject(i) );
}
model.addRow( row );
}
Finaly you create the table using:
JTable table = new JTable( model );
Edit:
Since you are using 3rd party code you either need to change the way you add data to the model. I gave you basic code above. You can see the Table From Database Example source code from Table From Database for a complete example.
Or, you can modify the column headers after the table is created with code like:
table.getColumn("course_code").setHeaderValue("Course Code");
...
table.repaint();
Edit 2:
You can get the TableColumn from the TableColumnModel:
TableColumnModel tcm = table.getTableColumnModel();
tcm.getColumn(0).setHeaderValue("Course Code");
...
table.repaint();
I am currently facing the same problem as you did.
This line:
table.setModel(DbUtils.resultSetToTableModel(rs));
provided by r2xml.jar is pretty handy :)
I overcame the problem by setting alias to my sql SELECT statement.
For example,
SELECT
EngagementMethodID AS [ID],
EngagementMethodDescription AS [Engagement Method Description]
FROM [STUDENT].[EngagementMethod]
This will populate the column header as ID and Engagement Method Description as wanted.
Hope it helps.. Despite the age of this question? haha... Just sharing
try this
int colCount = 0;
ResultSetMetaData rsMetaData = null;
colCount = rsMetaData.getColumnCount();
for (int k = 1; k <= colCount; k++) {
{ String columnName = null;
columnName = rsMetaData.getColumnName(k);
System.out.println(columnName);
}
use this code simultaneously with your fetching code.
Each JTable has a TableModel. This TableModel defines the columns(Names and data types)
so find your table model and change it accordingly.

JTable doesn't show the newly inserted row/data

I have a JTable and I populte the table as follows:
jTable_Std_info.setModel(DBControler.getALLStudents());
And the following is a static method in a class named DBControler which retrieves all the data from the database(Oracle).
public static DefaultTableModel getALLStudents() throws SQLException, Exception {
DefaultTableModel tableModel = new DefaultTableModel();
Vector rows = new Vector();
Vector columns = new Vector();
try {
conn = geConnection();
cst = conn.prepareCall("{? = call std_getInfoFunc}");
cst.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
cst.execute();
res = (ResultSet) cst.getObject(1);
System.out.print(res);
ResultSetMetaData rsm = res.getMetaData();
for (int i = 1; i <= rsm.getColumnCount(); i++) {
columns.addElement(rsm.getColumnName(i));
}
int row = 0;
while (res.next()) {
Vector vRow = new Vector(); //to store the current row
//System.out.println("Row " +row+"\n");
for (int i = 1; i <= rsm.getColumnCount(); i++) {
String columnValue = res.getString(i);
vRow.addElement(columnValue);
}
row += 1;
rows.addElement(vRow);
}
tableModel.setDataVector(rows, columns);
} catch (SQLException e) {
e.printStackTrace();
} finally {
res.close();
conn.close();
}
return tableModel;
}
So far everything works fine, but the problem is that if I insert a new record in the database, the JTable doesn't get the newly inserted row/data. Why is that and how can I fix this problem?
UPDATE:
It's retrieving the data when I commit my new insertion. So do I have to commit each time I update? Or is there any other ways to do this?
I think that you looking for Oracle Built-In Database Change Notification, not sure if is accesible for Oracle's in free-versions, if not then never mind, for MySQL is there two or three similair API for Java JDBC
But the problem is that if I insert a new record in the database, the JTable doesn't get the newly inserted row/data. Why is that?
The TableModel doesn't know when the database is updated.
and how can I fix this problem?
If your application is adding the row to the database then it also needs to add a row to the TableModel at the same time.

Populate Java table with resultSet

I'm building a java application that gets its data from an oracle database and puts it into a JTable.
My problem is I am not able to populate the table, I don't understand how to do it. Javadoc is useless.
I don't understand why the table doesn't get the rows:
if ((report.getMsg()=="selectEventoAll") && (report.getEsito()==1))
{
DefaultTableModel dtm = new DefaultTableModel();
eventi_tb.setModel(dtm);
try
{
ResultSet res_eventi = report.getRes();
i = 0;
Object[][] datiEventi = new Object[report.getRowCount()][5];
while(res_eventi.next())
{
j = 0;
while (j < 5)
{
datiEventi[i][j] = res_eventi.getObject(j+2);
j++;
}
dtm.addRow(datiEventi[i]);
i++;
}
}
You can do this using a custom implementation of AbstractTableModel.
After you get your results back, put them in a list and let this be the backing list for your table model.
See here .. http://download.oracle.com/javase/tutorial/uiswing/components/table.html#data
Table From Database should get you started.

SQL data and JTable

I'm trying one sample program for practice and i want to display results of database in JTable. The problem is i have no idea how to do that. I know how to get data from database and display it on text field or console but never tried with JTable. How to do that ?
Consider that i've table which is holding information like person name, age, city and date. i want it to be displayed via JTable. Also is it possible to update the JTable display if i add the option of adding more details in program(i mean adding entries to db then that will show immediately in JTable )?
Any suggestions, pointers on how to proceed is appreciated. Thanks in advance.
JDBC + JTable # google:
Hacking Swing: A JDBC Table Model
Mastering the JTable
Making SQL Queries with JDBC and Displaying Results with Swing
here is the code
public static TableModel resultSetToTableModel(ResultSet rs) {
try {
ResultSetMetaData metaData = rs.getMetaData();
int numberOfColumns = metaData.getColumnCount();
Vector<String> columnNames = new Vector<String>();
// Get the column names
for (int column = 0; column < numberOfColumns; column++) {
columnNames.addElement(metaData.getColumnLabel(column + 1).toUpperCase());
}
// Get all rows.
Vector<Vector<Object>> rows = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> newRow = new Vector<Object>();
for (int i = 1; i <= numberOfColumns; i++) {
newRow.addElement(rs.getObject(i));
}
rows.addElement(newRow);
}
return new DefaultTableModel(rows, columnNames);
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
you can call this method such that way
void update_Table() throws ParseException
{
// we used this try catch so that values in table automatically show(without clicking on any button) when the dialog box open
try
{
Connection con=MSUTIL.getMSConnection();
PreparedStatement pst=con.prepareStatement("select payment_mode from PaymentMode");
ResultSet rs=pst.executeQuery();
//here i call that method
table.setModel(resultSetToTableModel(rs));
table.getTableHeader().setFont(new Font("SansSerif", Font.BOLD, 14));
while(rs.next())
{
mp.paymentmode_ComboBox.addItem(rs.getString("payment_mode"));
}
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
//this method is for closing the connection
MSUTIL.cleanUp(con, pst, rs);
}
}

Categories

Resources