JDBC SQL查询字符串问题

JDBC SQL query string problem

我在将 sql 查询作为字符串插入 java 时遇到了一些问题。当我将文本直接粘贴到其中时,它不会将其保留为字符串(不知道为什么,因为其中没有双引号),所以我尝试逐行对其进行格式化,但随后出现 sql 异常(并且我知道查询有效) .谁能发现什么给我的问题?如果没有,有没有更好的方法可以插入 sql 代码?谢谢

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public FratReport getFratReport() throws SQLException {
            FratReport newfrat = NEW FratReport();
            Statement stmt;
            ResultSet results;
            String myQuery;
            myQuery = String.format("select m.nickname,"
                    +" case"
                    +" when stat_nrOfBookings/date_part('days', now()-stat_since)<0.035714 then 1"
                    +" when stat_nrOfBookings/date_part('days', now()-stat_since)<0.07142858 then 2"
                    +" when stat_nrOfBookings/date_part('days', now()-stat_since)<0.1785715 then 3"
                    +" when stat_nrOfBookings/date_part('days', now()-stat_since)<0.2857143 then 4"
                    +" else 5 end as freq,"
                    +" case"
                    +" when stat_sumPayments < 10000 then 1"
                    +" when stat_sumPayments < 100000 then 2"
                    +" when stat_sumPayments < 250000 then 3"
                    +" when stat_sumPayments < 500000 then 4"
                    +" else 5 end as am,"
                    +" case"
                    +" when count = null then 'weekday'"
                    +" when stat_nrOfBookings-count > count then 'weekday'"
                    +" else 'weekend' end as typ,"
                    +" case"
                    +" when max = null then 0"
                    +" when max<(now()-interval '4 weeks') then 1"
                    +" when max<(now()-interval '2 weeks') then 2"
                    +" when max<(now()-interval '1 week') then 3"
                    +" when max<(now()-interval '1 days') then 4"
                    +" else 5 end as rece"
                    +" from (member natural join memberstats) as m join"
                    +" (select rece.nickname, max, count from"
                    +" (select nickname , max(whenbooked) from member full join booking on member.memberNo = booking.madeBy group by member.nickname) as rece"
                    +" full join"
                    +" (select nickname, count from member full join (select nickname as nn, count(*) from (select nickname, to_char(whenbooked, 'D') from member left outer join booking on member.memberno = booking.madeBy) as days where to_char = '7' or to_char = '6' group by nickname) as c on member.nickname = c.nn) as daycount"
                    +" on rece.nickname = daycount.nickname)"
                    +" as n"
                    +" on m.nickname = n.nickname"
                    +" order by freq desc, rece desc, am desc, typ desc");


            try {
                stmt = conn.createStatement();
                results = stmt.executeQuery(myQuery);
                String newString;
                while (results.next()) {
                            newString = results.getString("nickname") +"" +
                            INTEGER.toString(results.getInt("rec"))+"" +
                            INTEGER.toString(results.getInt("am")) +""+
                            results.getString("type");
                    newfrat.addLine(newString);
                }
                stmt.close();
                results.close();
            }
            catch(SQLException e){System.out.println("yep");}
            RETURN newfrat;
}


随机一行:

1
2
+"when stat_sumPayments < 10000 then 1"
+"when stat_sumPayments < 100000 then 2"

显然,这会在连接时给你语法错误:

1
2
WHEN stat_sumPayments < 10000 THEN 1when stat_sumPayments < 100000 THEN 2
                                   ^^

解决方法是在每行末尾添加一个空格。

1
2
+"when stat_sumPayments < 10000 then 1"
+"when stat_sumPayments < 100000 then 2"

除此之外,如果你坚持使用 JDBC 而不是任何建立在 JDBC 之上的框架,那么这就是"最先进的"。否则,我可以推荐我的数据库抽象框架 jOOQ。它将帮助您在 Java DSL 中表达上述复杂查询,而不会冒此类语法错误的风险:

http://www.jooq.org