I am populating a JTable from an oracle database connection using JDBC. It currently is working, but I'm trying to make a modification to the code that is breaking it.
Currently it populates the JTable by querying the database when the JTable is created.
I am trying to move this functionality to an ActionListener. Basically, I want to create an empty table, then when a user presses a JButton it should run the sql query and populate and redraw the table. I've tried everything I can think of, and I can't help but think this is a basic one but my logic is failing me.
current implementation:
private void makeTable() {
Vector columnNames = new Vector();
Vector data = new Vector();
String userName = "aUsername";
String password = "aPassword";
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection connection = DriverManager.getConnection("redactedDatabaseConnection", userName, password);
String sql = "select upper(choreName) as Chore, dueDate as due, completedDate as completed, fname as completedby from chore inner join choreCompletion on chore.choreid = chorecompletion.choreid inner join users on users.userID = choreCompletion.completedby";
Statement statement = connection.prepareStatement(sql);
ResultSet rset = statement.executeQuery(sql);
ResultSetMetaData metaData = rset.getMetaData();
int columns = metaData.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement(metaData.getColumnName(i));
}
while (rset.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(rset.getObject(i));
}
data.addElement(row);
}
rset.close();
statement.close();
connection.close();
} catch (SQLException ex) {ex.printStackTrace();}
JTable choresTable = new JTable(data, columnNames);
TableColumn column;
for (int i = 0; i<choresTable.getColumnCount(); i++) {
column = choresTable.getColumnModel().getColumn(i);
column.setMaxWidth(250);
}
JScrollPane scrollPane = new JScrollPane(choresTable);
scrollPane.getViewport().setBackground(new Color(144, 167, 204));
}
EDIT: Current attempt at implementing
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Vector;
@SuppressWarnings("unchecked")
public class FillTable extends JFrame
{
private Vector columnNames, data, row;
private Connection connection;
private final String dbUsername = "aUsername";
private final String dbPassword = "aPassword";
private JTable groceryTable;
private TableColumn column;
private JScrollPane pane;
private JPanel panel;
public FillTable()
{
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
} catch(Exception e){
e.printStackTrace();
}
createComponents();
setSize(800,800);
setTitle("A filled Table");
}
public static void main(String[] args)
{
JFrame frame = new FillTable();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(false);
frame.setLocationRelativeTo(null);
frame.setResizable(true);
frame.setVisible(true);
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try {
connection = DriverManager.getConnection("redactedServerInfo", dbUsername, dbPassword);
String sql = "select upper(name) as Grocery, fname as ordered_by, dateordered as date_ordered from groceries inner join grocerylist on groceries.groceryid=grocerylist.groceryid inner join users on grocerylist.orderedby= users.userid";
Statement statement = connection.prepareStatement(sql);
ResultSet rset = statement.executeQuery(sql);
ResultSetMetaData metaData = rset.getMetaData();
int columns = metaData.getColumnCount();
for (int i=1; i<= columns; i++) {
columnNames.addElement(metaData.getColumnName(i));
}
while (rset.next()) {
row = new Vector(columns);
for(int i=1;i<=columns;i++) {
row.addElement(rset.getObject(i));
}
data.addElement(row);
}
rset.close();
statement.close();
} catch (Exception ex) {
ex.printStackTrace();
}
for(int i=0;i<groceryTable.getColumnCount(); i++) {
column = groceryTable.getColumnModel().getColumn(i);
column.setMaxWidth(250);
}
pane.repaint();
pane.revalidate();
groceryTable.repaint();
groceryTable.revalidate();
panel.repaint();
panel.revalidate();
repaint();
revalidate();
}
}
private void createComponents()
{
JButton button = new JButton("Press me");
ActionListener buttonListener = new ButtonListener();
button.addActionListener(buttonListener);
columnNames = new Vector();
data = new Vector();
row = new Vector();
groceryTable = new JTable(data, columnNames);
pane = new JScrollPane(groceryTable);
panel = new JPanel();
panel.add(button);
panel.add(pane);
add(panel);
}
}