使用CSVJDBC的嵌套查询

时间:2015-04-12 18:01:13

标签: java sql jdbc csvjdbc

我正在尝试使用csvJDBC执行嵌套查询。

有问题的表格具有以下属性:

comp(comp_id, comp_description)
work_opportunities(wo_id, jp_id)
link_wo_comp (wo_id, comp_id)

查询是:

SELECT comp_id, count(comp_id)
FROM link_wo_comp
WHERE wo_id IN (SELECT distinct wo_id FROM work_opportunities WHERE jp_id = '1')
GROUP BY comp_id;

我已经自己执行了子查询和主查询,但他们已经工作了。

这是我正在使用的代码:

try {
            Class.forName("org.relique.jdbc.csv.CsvDriver");

            Connection conn = DriverManager.getConnection("jdbc:relique:csv:"
                    + "D:/Desktop/Data/Cleansed/");

            Statement stmt = conn.createStatement();

            String sql = "SELECT comp_id, count(comp_id) "
                    + "FROM link_wo_comp "
                    + "WHERE wo_id IN (SELECT distinct wo_id FROM work_opportunities WHERE jp_id = '1') "
                    + "GROUP BY comp_id;";

            ResultSet results = stmt.executeQuery(sql);

            boolean append = true;
            CsvDriver.writeToCsv(results, System.out, append);

            // Clean up
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

错误日志是这样的:

java.sql.SQLException: Syntax error: Encountered "" at line 1, column 66.
Was expecting one of:

    at org.relique.jdbc.csv.CsvStatement.executeQuery(Unknown Source)
    at testing.TestRun.main(TestRun.java:30)

csvJDBC是否支持嵌套查询,如果是,我的代码有什么问题?

由于

2 个答案:

答案 0 :(得分:1)

简单来说,不管它不支持。

CsvJdbc仅接受来自single表的SQL SELECT查询,并且不支持INSERT,UPDATE,DELETE或CREATE语句。不支持SQL SELECT查询中表之间的连接。

请参阅文档链接了解更多

http://csvjdbc.sourceforge.net/doc.html

答案 1 :(得分:0)

CSVJDBC可能无法一次处理来自多个CSV文件的嵌套查询或SELECT语句,但HSQLDB支持CSV文件Text Tables,因此您可以使用以下代码:

public static void main(String[] args) {
    // create HSQLDB :file: database in the same folder as the CSV files
    String connectionUrl = "jdbc:hsqldb:file:C:/__tmp/hsqldbCSV/Cleansed/hsqldb";
    try (Connection conn = DriverManager.getConnection(connectionUrl, "SA", "")) {
        try (Statement s = conn.createStatement()) {
            s.execute("DROP TABLE IF EXISTS comp");
            s.execute("CREATE TEXT TABLE comp (comp_id int, comp_description varchar(50))");
            s.execute("SET TABLE comp SOURCE 'comp.csv'");
            s.execute("DROP TABLE IF EXISTS work_opportunities");
            s.execute("CREATE TEXT TABLE work_opportunities (wo_id int, jp_id int)");
            s.execute("SET TABLE work_opportunities SOURCE 'work_opportunities.csv'");
            s.execute("DROP TABLE IF EXISTS link_wo_comp");
            s.execute("CREATE TEXT TABLE link_wo_comp (wo_id int, comp_id int)");
            s.execute("SET TABLE link_wo_comp SOURCE 'link_wo_comp.csv'");
            String sql = "SELECT comp_id, count(comp_id) "
                    + "FROM link_wo_comp "
                    + "WHERE wo_id IN (SELECT distinct wo_id FROM work_opportunities WHERE jp_id = '1') "
                    + "GROUP BY comp_id;";
            try (ResultSet rs = s.executeQuery(sql)) {
                while (rs.next()) {
                    System.out.println(String.format("comp_id: %d, count: %d", rs.getInt(1), rs.getInt(2)));
                }
            }
            s.execute("SHUTDOWN");
        }
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
}