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

            //  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 );

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

     public void run() {
                new Display_All("town").setVisible(true);

Recommended Answers

All 4 Replies

This is where the column names are created...

for (int i = 1; i <= columns; i++) {
   columnNames.addElement( md.getColumnName(i) );

.. so instead of that just add your own preferred Strings to the Vector colmnNames

Yep I know but every time i tried to change it i got errors. Hw wud i change it so it wudn't make it go haywire?? thanks

Instead of that loop you just need to add the right number of Strings to columnNames. You already have an example of adding right there (it's just columnNames.addElement("My first column name"); etc)

ps: Many of our members have English as their second (or third...) language, so "leet" or other semi-coded language will be unreasonably difficult for them. Please keep to proper English.
DaniWeb Member Rules include:
"Do post in full-sentence English"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.