HSQLDB 子查询 - Java

Posted

技术标签:

【中文标题】HSQLDB 子查询 - Java【英文标题】:HSQLDB Subquery - Java 【发布时间】:2016-05-14 00:42:26 【问题描述】:

在 Microsoft Access 中,我有三个有效的查询 -

qryAwayMatches:

SELECT MatchTeam.FootballMatchID, MatchTeam.TeamID, MatchTeam.GameType, MatchProtocol.MatchTeamID, MatchProtocol.GoalNumber, MatchProtocol.YellowCardNumber, MatchProtocol.RedCardNumber
FROM MatchTeam LEFT JOIN MatchProtocol ON MatchTeam.ID = MatchProtocol.MatchTeamID
WHERE (((MatchTeam.GameType)="Away"));

qryHomeMatches:

SELECT MatchTeam.FootballMatchID, MatchTeam.TeamID, MatchTeam.GameType, MatchProtocol.MatchTeamID, MatchProtocol.GoalNumber, MatchProtocol.YellowCardNumber, MatchProtocol.RedCardNumber
FROM MatchTeam LEFT JOIN MatchProtocol ON MatchTeam.ID = MatchProtocol.MatchTeamID
WHERE (((MatchTeam.GameType)="Home"));

qryMatchResult:

SELECT qryHomeMatches.FootballMatchID, qryHomeMatches.TeamID AS HomeTeamID, qryAwayMatches.TeamID AS AwayTeamID, qryHomeMatches.GoalNumber AS HomeTeamGoals, qryAwayMatches.GoalNumber AS AwayTeamGoals, [HomeTeamGoals]>[AwayTeamGoals] AS HomeTeamWin, [HomeTeamGoals]=[AwayTeamGoals] AS NoWin, [HomeTeamGoals]<[AwayTeamGoals] AS AwayTeamWin
FROM qryHomeMatches INNER JOIN qryAwayMatches ON qryHomeMatches.FootballMatchID = qryAwayMatches.FootballMatchID;

在我的 Java 程序中,我通过以下方式在按下按钮时显示前两个查询的结果:

btnqryAwayMatches.addActionListener(new ActionListener() 
            public void actionPerformed(ActionEvent e) 
                Connection con = null;
                Statement st = null;
                ResultSet rs = null;
                String s;
                JFrame frame;
                String url = "jdbc:hsqldb:file:db_data/myFootballDB;ifexists=true;shutdown=true";
                String username = "SA";
                String password = "";

                try 
                    con = DriverManager.getConnection(url, username, password);
                    st = con.createStatement();
                    s = "SELECT MATCHTEAM.FOOTBALLMATCHID, MATCHTEAM.TEAMID, MATCHTEAM.GAMETYPE, "
                            + "MATCHPROTOCOL.MATCHTEAMID, MATCHPROTOCOL.GOALNUMBER, "
                            + "MATCHPROTOCOL.YELLOWCARDNUMBER, "
                            + "MATCHPROTOCOL.REDCARDNUMBER "
                            + "FROM MATCHTEAM LEFT JOIN MATCHPROTOCOL ON MATCHTEAM.ID = MATCHPROTOCOL.MATCHTEAMID "
                            + "WHERE (((MATCHTEAM.GAMETYPE)='Away'));";
                    rs = st.executeQuery(s);
                    ResultSetMetaData rsmt = rs.getMetaData();
                    int c = rsmt.getColumnCount();
                    Vector<String> column = new Vector<String>(c);
                    for (int i = 1; i <= c; i++) 
                        column.add(rsmt.getColumnName(i));
                    
                    Vector<Vector<String>> data = new Vector<Vector<String>>();
                    Vector<String> row = new Vector<String>();
                    while (rs.next()) 
                        row = new Vector<String>(c);
                        for(int i = 1; i <= c; i++) 
                            row.add(rs.getString(i));
                        
                        data.add(row);
                    

                frame = new JFrame();
                frame.setTitle("Away Match Results");
                frame.setSize(700,420);
                frame.setLocationByPlatform(true);
                frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                JPanel panel = new JPanel();
                JTable table = new JTable(data, column);
                JScrollPane jsp = new JScrollPane(table);
                panel.setLayout(new BorderLayout());
                panel.add(jsp, BorderLayout.CENTER);
                frame.setContentPane(panel);
                frame.setVisible(true);

                 catch(Exception exc) 
                    exc.printStackTrace();
                    JOptionPane.showMessageDialog(null, "Error");
                 finally 
                    try 
                        st.close();
                        rs.close();
                        con.close();
                     catch(Exception exception) 
                        JOptionPane.showMessageDialog(null, "Error close");
                    
                
            
        );

如何编写第三个查询以在单击按钮时执行?我知道它需要知道前两个查询的结果才能执行,所以我不确定如何编程。

【问题讨论】:

【参考方案1】:

您可以使用 WITH 子句轻松做到这一点:

WITH qryAwayMatches AS (SELECT MatchTeam.FootballMatchID, ...),
     qryHomeMatches AS (SELECT MatchTeam.FootballMatchID, ...)
     SELECT qryHomeMatches.FootballMatchID, ...

将每个查询的全文放在上面的模板中。

【讨论】:

以上是关于HSQLDB 子查询 - Java的主要内容,如果未能解决你的问题,请参考以下文章

HSQLDB 触发器语法 - 不可能在触发器内进行子查询吗?

HSQLDB 在使用嵌套查询并且数据库处于只读模式时触发 OutOfMemoryError

hsqldb 是不是提供类似于 Oracle CURSOR 的功能?

使用 Java 在 Ubuntu 上查询 HSQLDB 数据库

java 子函数查询模板

推荐学Java——数据表高级操作