Hey Guys,
So I was doin a tutorial and I incorporated it into my code and it works for the most part.
The program gives you a list of town names you pick one and hit search. All good so far then the program goes into the database and pulls the relevant info from 6 tables and they display in the next gui on a Jtable.
Only problem is the column header are taken from the database aswell but I want Generic column titles as the ones from the table reflect the table they were pulled from and since there not from the same table this is a problem.
I have tried moving things around and taking things out but this leads to errors everywhere O_o
Heres the code :
public Display_All(Object town)
{
Vector columnNames = new Vector();
Vector data = new Vector();
try
{
// Connect to an Access Database
String driver = "com.mysql.jdbc.Driver";
String url ="jdbc:mysql://localhost:3356/ireland";
String userid = "root";
String password = "password";
Class.forName( driver );
Connection connection = DriverManager.getConnection( url, userid, password );
// Read data from a table
String sql = "(SELECT * FROM activities WHERE Town ='"+town+"')"
+ "UNION ALL ( SELECT * FROM attractions WHERE Town = '"+town+"')"
+ "UNION ALL ( SELECT * FROM bb WHERE Town = '"+town+"')"
+ "UNION ALL ( SELECT * FROM hotels WHERE Town = '"+town+"')"
+ "UNION ALL ( SELECT * FROM pubs WHERE Town = '"+town+"')"
+ "UNION ALL ( SELECT * FROM restaurant WHERE Town = '"+town+"')"
+ "UNION ALL ( SELECT * FROM self_catering WHERE Town = '"+town+"')";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery( sql );
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 row = new Vector(columns);
for (int i = 1; i <= columns; i++)
{
row.addElement( rs.getObject(i) );
}
data.addElement( row );
}
rs.close();
stmt.close();
connection.close();
}
catch(Exception e)
{
System.out.println( e );
}
// Create table with database data
JTable table = new JTable(data, columnNames )
{
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);
if (o != null)
{
return o.getClass();
}
}
return Object.class;
}
};
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
// JPanel buttonPanel = new JPanel();
// getContentPane().add( buttonPanel, BorderLayout.SOUTH );
}
public static void main(String[] args)
{
Display_All frame = new Display_All("town");
frame.setDefaultCloseOperation( DISPOSE_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
public void run() {
new Display_All("town").setVisible(true);
}
}