This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

J Table Update (it makes me mad)  RSS feed

 
tom1978
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
}
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand how the QueryTableModel provided to the JTable is connected to the QueryTableModel attribute in the TableData class. Where exactly gets the method setVector of the QueryTableModel object used in the JTable called?
chantal
 
tom1978
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry i forgot sth. here the full code, still doesnt work.

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.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;
static 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 Kunde where KundenNr = ";

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);
}
qtm.setVector(cache);

}
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();
}
}
}

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);
}
}
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still don't understand why you are using to different QueryTableModel objects in your code. You need only one, the one that is used by your JTable.
TableData should know nothing about QueryTableModel but rather QueryTableModel should use TableData to build up the JTable data. (Backend classes should not have to know about any GUI classes, as the backend is independent of the view!)
in GUIListener:
where is TableData initialized?
Chantal
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"tom1978" -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it here.

Thanks! and welcome to the JavaRanch!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!