Can't get the table display my results

OK After some good hours of debugging i am able to display my results on JTable for my queries. But i got another problem now. When i select Query2 from my JComboBox and click on execute button nothing happens, same thing for query3 and 4. The only thing which works is QUERY1 why? what am i doing wrong?

import java.awt.FlowLayout;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JTable;

import javax.swing.table.DefaultTableModel;

import javax.swing.table.TableModel;

import java.awt.event.ActionEvent;

import java.sql.Connection;

import java.sql.Statement;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.util.Vector;

publicclass DatabaseProgrammingextends JFrameimplements ActionListener{

staticfinal String JDBC_DRIVER ="com.mysql.jdbc.Driver";

staticfinal String DATABASE_URL ="jdbc:mysql://localhost/employees";

staticfinal String USERNAME ="jhtp6";

staticfinal String PASSWORD ="jhtp6";

privatestaticfinal String QUERY1 ="SELECT * FROM employees WHERE DEPARTMENTNAME = 'SALES'";

privatestaticfinal String QUERY2 ="SELECT * FROM hourlyEmployees WHERE hours >= '30'";

privatestaticfinal String QUERY3 ="SELECT * FROM commissionEmployees ORDER BY commissionRate DESC";

private String names[] ={"QUERY1","QUERY2","QUERY3","QUERY4"};

private Connection connection;

private ResultSet resultSet;

private Statement statement;

private ResultSetMetaData metaData;

private JTable resultTable;

private JComboBox queryBox;

private JButton button;

privateint number;

privateboolean connectedDatabase =false;

private DefaultTableModel dtm;

publicstaticvoid main(String[] args){

DatabaseProgramming frame =new DatabaseProgramming();

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public DatabaseProgramming(){

super("Testing Database");

setLayout(new FlowLayout());

//dtm = new DefaultTableModel(getColumnName(number),0);

//statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

// ResultSet.CONCUR_READ_ONLY);

queryBox =new JComboBox(names);

queryBox.setEditable(false);

//queryBox.addActionListener(this);

button =new JButton("EXECUTE");

resultTable =new JTable();

button.addActionListener(this);

add(queryBox);

add(button);

add(resultTable);

//add(result);

setSize(400,400);

}

publicvoid actionPerformed(ActionEvent e){

String selection = (String)queryBox.getSelectedItem();

if(selection.equals(names[0]))

validateQuery(QUERY1);

if(selection.equals(QUERY2))

validateQuery(QUERY2);

if(selection.equals(QUERY3))

validateQuery(QUERY3);

}

publicvoid validateQuery(String query){

try{

Class.forName(JDBC_DRIVER);

connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);

System.out.println("Yay Database Connected");

Vector col =new Vector();

Vector data =new Vector();

statement = connection.createStatement();

resultSet = statement.executeQuery(query);

metaData = resultSet.getMetaData();

number = metaData.getColumnCount();

for(int i=1; i<=number; i++)

col.addElement(metaData.getColumnName(i));

while(resultSet.next()){

Vector row =new Vector();

for(int i=1; i<=number; i++)

row.addElement(resultSet.getObject(i));

data.addElement(row);

}

resultTable.setModel(new DefaultTableModel(data,col));

resultTable.revalidate();

}

catch ( SQLException sqlException )

{

sqlException.printStackTrace();

System.exit( 1 );

}

catch(ClassNotFoundException e){

e.printStackTrace();

}

finally

{

try

{

statement.close();

connection.close();

}

catch ( SQLException sqlException )

{

JOptionPane.showMessageDialog( null,

sqlException.getMessage(),"Database error",

JOptionPane.ERROR_MESSAGE );

System.exit( 1 );

}

}

}

}

[8643 byte] By [schumachera] at [2007-11-27 10:53:47]
# 1

nvm i got it. freakin ridiculous :PpP. One last question how can i display the columnName on my JTable?

schumachera at 2007-7-29 11:46:14 > top of Java-index,Desktop,Core GUI APIs...
# 2

> One last question how can i display the columnName on my JTable?

Add the table to a scroll pane and the scroll pane to the frame.

camickra at 2007-7-29 11:46:14 > top of Java-index,Desktop,Core GUI APIs...