Hello members!
Could someone please give me a hand on this?
I have a JTable in my program that display query resuslt when the user search for a patient by entering the surname, the result is supposed to display in JTable in another frame.
Here is what I've done
import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class QueryTable extends JFrame
{
/**
*
*/
private static final long serialVersionUID = -4890348590846602933L;
public QueryTable() throws ClassNotFoundException, SQLException
{
Vector<String> columnNames = new Vector<String>();
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
Connection connection = null;
Statement stmt = null;
ResultSet rs = null;
try
{
// Connect to the Database (here: Oracle)
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "JDBC:ODBC:Student";
String userid = "";
String password = "";
Class.forName( driver );
connection = DriverManager.getConnection( url, userid, password );
// Read data from a table
stmt = connection.createStatement();
rs = stmt.executeQuery(PatientReg.query);
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
// Get column names
for (int i = 1; i <= columns; i++)
{
columnNames.addElement( md.getColumnName(i) );
}
// Get row data
while (rs.next())
{
Vector<Object> row = new Vector<Object>(columns);
for (int i = 1; i <= columns; i++)
{
row.addElement( rs.getObject(i) );
}
data.addElement( row );
}
}
catch(Exception e)
{
System.err.println( e );
}
finally{
if(connection != null) connection.close();
if(stmt !=null) stmt.close();
}
// Create table with database data
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
JPanel buttonPanel = new JPanel();
getContentPane().add( buttonPanel, BorderLayout.SOUTH );
}
}
This is called in the search button's actionPerformed method
//Do search button
Searchbtn = new JButton("Search");
Searchbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
QueryTable frame = new QueryTable();
frame.pack();
frame.setSize(900, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (SQLException ex) {
ex.printStackTrace();
}catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}
});
the problem is it only shows the column names but no records. though the table in the database contains records.
Thanks for helping!