1

i am trying to perform a nested query using csvJDBC.

The tables in question has the following attributes:

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

The query is:

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;

I have executed the sub query and the main query on their own and they have worked.

This is the code i am using:

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

The error log is this:

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)

Does csvJDBC support nested queries, and if so what is wrong with my code?

Thanks

Style
  • 67
  • 9

2 Answers2

1

To put it in simple terms No it wont support.

CsvJdbc accepts only SQL SELECT queries from a single table and does not support INSERT, UPDATE, DELETE or CREATE statements. Joins between tables in SQL SELECT queries are not supported.

Refer the doc link for more

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

Madhan
  • 5,750
  • 4
  • 28
  • 61
0

CSVJDBC may not be able to handle nested queries or SELECT statements from more than one CSV file at a time, but HSQLDB supports CSV files as Text Tables so you could use code like this:

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);
    }
}
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418