I am working on this for nerly a week!
My JTable won�t update. I would be very happy about some advise.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class DatabaseTest extends JFrame {
JTextField hostField;
JTextField queryField;
QueryTableModel qtm;
static DbConnect td = new DbConnect();
public DatabaseTest() {
super("Database Test Frame");
//addWindowListener(new BasicWindowMonitor());
setSize(350, 200);
qtm = new QueryTableModel();
JTable table = new JTable(qtm);
JScrollPane scrollpane = new JScrollPane(table);
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3, 2));
p1.add(new JLabel("Enter the Host URL: "));
p1.add(hostField = new JTextField("jdbc
dbc:AES"));
p1.add(new JLabel("Enter your query: "));
p1.add(queryField = new JTextField());
p1.add(new JLabel("Click here to send: "));
JButton jb = new JButton("Search");
jb.addActionListener(new GuiLi());
p1.add(jb);
getContentPane().add(p1, BorderLayout.NORTH);
getContentPane().add(scrollpane, BorderLayout.CENTER);
}
public static void main(String args[]) {
DatabaseTest tt = new DatabaseTest();
tt.setVisible(true);
td.connect();
}
}
class GuiLi implements ActionListener{
DatabaseTest db;
TableData tabData;
public void actionPerformed(java.awt.event.ActionEvent actionEvent) {
tabData.setQuery(234); //234 just an example for a customer
}
}
import java.sql.*;
import java.io.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.*;
public class QueryTableModel extends AbstractTableModel {
Vector cache;
int colCount;
String[] headers;
Connection db;
Statement statement;
String driver ="sun.jdbc.odbc.JdbcOdbcDriver";
String dbURL = "jdbc
dbc:AES";
Connection conn;
public QueryTableModel() {
cache = new Vector();
}
public String getColumnName(int i) { return headers[i]; }
public int getColumnCount() { return colCount; }
public int getRowCount() { return cache.size();}
public Object getValueAt(int row, int col) {
return ((String[])cache.elementAt(row))[col];
}
public void setVector(Vector holla) {
cache = holla;
fireTableChanged(null);
}
}
import java.util.Vector;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
class DbConnect{
String driver ="sun.jdbc.odbc.JdbcOdbcDriver";
String dbURL = "jdbc
dbc:AES";
Connection conn;
Statement statement;
public DbConnect(){
}
public void connect(){
try {
Class.forName(driver);
System.out.println("get Conn");
conn = DriverManager.getConnection(dbURL);
statement = conn.createStatement();
TableData.setConnection(conn);
}
catch (ClassNotFoundException ex) {
System.err.println("not found");
System.err.println(ex);
}
catch (SQLException ex) {
System.err.println("no conn");
System.err.println(ex);
}
}
}
public class TableData {
static Vector cache;
static Connection conn;
static Statement stmt;
static int colCount;
static String[] headers;
DbConnect db;
QueryTableModel qtm = new QueryTableModel();
public static void setConnection(Connection connection)
{
conn = connection;
try
{
stmt = conn.createStatement();
}
catch(SQLException ex)
{
System.out.println(ex.getMessage());
System.exit(0);
}
}
public static void setQuery(int custId) {
cache = new Vector();
try {
String query = "Select * from cust where custId = ";
ResultSet rs = stmt.executeQuery(query + custId);
ResultSetMetaData meta = rs.getMetaData();
colCount = meta.getColumnCount();
headers = new String[colCount];
for (int h=1; h <= colCount; h++) {
headers[h-1] = meta.getColumnName(h);
}
while (rs.next()) {
String[] record = new String[colCount];
for (int i=0; i < colCount; i++) {
record[i] = rs.getString(i + 1);
}
cache.addElement(record);
}
}
catch(Exception e) {
cache = new Vector(); // blank it out and keep going.
e.printStackTrace();
}
}
public void initDB(String url) {
try {
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
}
catch(Exception e) {
System.out.println("Could not initialize the database.");
e.printStackTrace();
}
}
public void closeDB() {
try {
if (stmt != null) { stmt.close(); }
if (conn != null) { conn.close(); }
}
catch(Exception e) {
System.out.println("Could not close the current connection.");
e.printStackTrace();
}
}
}