简体   繁体   中英

Get the MYSQL Table Name from ResultSet

I have a SELECT query which combining three tables. I want to add them to a Jtable by separating the MYSQL tables. I just want to know how can I identify the table name in a Resultset?

resultLoad("SELECT sqNum,RegDate,JobNo,DecName,NoOfLines,EntryType,EntrySubType,EntrySubType2,Amount,Status FROM paysheet WHERE  TypeBy='" + cmbStaff.getSelectedItem().toString() + "' AND CommissionStatus='no' UNION SELECT sqNum,RegDate,JobNo,DecName,NoOfLines,EntryType,EntrySubType,EntrySubType2,Amount,Status FROM creditsheet WHERE  TypeBy='" + cmbStaff.getSelectedItem().toString() + "' AND CommissionStatus='no' ORDER BY RegDate UNION SELECT sqNumber,date,JobNumber,DecName,noOfLines,type,type,type,CommissionAmount,status FROM newjobsheet WHERE  TypeBy='" + cmbStaff.getSelectedItem().toString() + "' AND CommissionStatus='no' ORDER BY RegDate");

private void resultLoad(String loadQuery) {

    try {
        Connection c = DB.myConnection();
        Statement s = c.createStatement();
        ResultSet r = s.executeQuery(loadQuery);

        while (r.next()) {
            Vector v = new Vector();
            v.addElement(r.getString("sqNum"));
            v.addElement(r.getString("RegDate"));
            v.addElement(r.getString("JobNo"));
            v.addElement(r.getString("DecName"));
            v.addElement(r.getString("NoOfLines"));
            v.addElement(r.getString("EntryType") + " - " + r.getString("EntrySubType") + " - " + r.getString("EntrySubType2"));
            v.addElement(r.getString("Amount"));
            v.addElement(r.getString("Status"));
            tm.addRow(v);
            totalComAmount = totalComAmount + Integer.parseInt(r.getString("Amount"));
        }
    } catch (Exception e) {
        //  e.printStackTrace();
        JOptionPane.showMessageDialog(this, e, "Error!", JOptionPane.ERROR_MESSAGE);
    }
}

I want to add to the Jtable like this by sorting the dates. But the three tables containing different columns.

From your result set, you can get ResultSetMetaData. It looks like this:

rs.getMetaData().getTableName(int Column);

"I want to add them to a table by separating the tables."

Not sure what you mean by that, but:

"I just want to know how can I identify the table name in a Resultset?"

the answer is no, not unless you rewrite the query so that it does it for you.

A SELECT statement yields a new (virtual) table - any columns it delivers are technically columns of that new virtual table. The result does not remember the origin of those columns

However, you can write the query so as to give every expression in the SELECT list a column alias that allows you to identify the origin. For instance, you could do:

SELECT table1.column1 AS table1_column1
,      table1.column2 AS table1_column2
,      ...
,      table2.column1 AS table2_column1
,      ...
FROM   table1 INNER JOIN table2 ON ... etc

If the underscore is not suitable as a separator for your purpose, then you could consider to quote the aliases and pick whatever character as separator you like, including a dot:

SELECT table1.column1 AS `table1.column1`
,      ....
etc.

UPDATE:

I just noticed your query is a UNION over several tables. Obviously this method won't work in that case. I think your best bet is still to rewrite the query so that it reads:

SELECT 'paysheet' as table_name, ...other columns...
FROM   paysheet
UNION
SELECT 'creditsheet', ...other columns...
...

It seems to me like you want to SELECT data from three separate tables and have it returned in one query as one ResultSet, then you want to separate the results back out into individual tables.

This is completely counter intuitive.

If you want to have your results separated so that you are able to look at each table's results separately, your best bet is to perform 3 different SELECT queries. Basically, what you are asking it to combine 3 things and then separate them again in one pass. Don't do this; if you leave them uncombined in the first place, separating them back out won't be an issue for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM