运行 多行 SQL 查询 JSP

Running a multi-line SQL Query In JSP

我有 JSP 这样的:

ResultSet rs = st.executeQuery("select s.folderEntry,p.id,p.title,"+
    "max(case when s.name = 'event_types' then s.stringValue end) as Event_Type,"+
    "max(case when s.name = 'attendees_no' then s.stringValue end) as No_of_Attendee,"+
    "max(case when s.name = 'booker' then s.stringValue end) as Booker,"+
    "max(case when s.name = 'startdate' then s.stringValue end) as Start_date,"+
    "max(case when s.name = 'starttime' then s.stringValue end) as Start_time,"+
    "max(case when s.name = 'enddate' then s.stringValue end) as End_date,"+
    "max(case when s.name = 'endtime' then s.stringValue end) as End_time,"+
    "max(case when s.name = 'actual_attendees_no' then s.stringValue end) as Actual_Attendees_No,"+
    "max(case when s.name = 'edb' then s.stringValue end) as EDB,"+
    "max(case when s.name = 'ss_teacher' then s.stringValue end) as Secondary_School_teacher,"+
    "p.description_text"+"<br>"+
    "from sitescape.ss_folderentries"+
    "p join sitescape.ss_customattributes s on p.id = s.folderEntry"+
    "group by"+
    "p.id,"+
    "s.folderEntry");  
while(rs.next()){%>  

我收到一个错误:

自定义 jsp 中发生错误:com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:您的 SQL 语法有误;查看与您的 MySQL 服务器版本相对应的手册,了解在第 1
行 'from sitescape.ss_folderentriesp join sitescape.ss_customattributes s on p.id = ' 附近使用的正确语法

但是我在 SQL 控制台中测试 SQL 查询,没有错误。 不知道会发生什么。有人会给出提示吗?

您没有在查询中使用 html 标记:

这个:

"p.description_text"+"<br>"+

应该是:

p.description_text"+" "+

您的代码几乎没有问题。首先,多行查询时要注意spaces(比如from会粘到前一行,最好在每行末尾加一个space ).其次,如果您想将 <br> 连接到某个字符串,请使用 CONCAT(p.description_text, '<br>')(这实际上取决于您的数据库供应商,CONCAT 应该适用于 Oracle 和 MySQL)

ResultSet rs = st.executeQuery("select s.folderEntry,p.id,p.title, "+
  "max(case when s.name = 'event_types' then s.stringValue end) as Event_Type, "+
  "max(case when s.name = 'attendees_no' then s.stringValue end) as No_of_Attendee, "+
  "max(case when s.name = 'booker' then s.stringValue end) as Booker, "+
"max(case when s.name = 'startdate' then s.stringValue end) as Start_date, "+
"max(case when s.name = 'starttime' then s.stringValue end) as Start_time, "+
"max(case when s.name = 'enddate' then s.stringValue end) as End_date, "+
"max(case when s.name = 'endtime' then s.stringValue end) as End_time, "+
"max(case when s.name = 'actual_attendees_no' then s.stringValue end) as Actual_Attendees_No, "+
"max(case when s.name = 'edb' then s.stringValue end) as EDB, "+
"max(case when s.name = 'ss_teacher' then s.stringValue end) as Secondary_School_teacher, "+
"CONCAT(p.description_text, '<br>') "+
"from sitescape.ss_folderentries "+
"p join sitescape.ss_customattributes s on p.id = s.folderEntry "+
"group by "+
 "p.id, "+
  "s.folderEntry"); 

首先,您在 SQL 查询中输入了 HTML 标记,<br> 删除该标记。

ResultSet rs = st.executeQuery("select s.folderEntry,p.id,p.title,"+
    "max(case when s.name = 'event_types' then s.stringValue end) as Event_Type,"+
    "max(case when s.name = 'attendees_no' then s.stringValue end) as No_of_Attendee,"+
    "max(case when s.name = 'booker' then s.stringValue end) as Booker,"+
    "max(case when s.name = 'startdate' then s.stringValue end) as Start_date,"+
    "max(case when s.name = 'starttime' then s.stringValue end) as Start_time,"+
    "max(case when s.name = 'enddate' then s.stringValue end) as End_date,"+
    "max(case when s.name = 'endtime' then s.stringValue end) as End_time,"+
    "max(case when s.name = 'actual_attendees_no' then s.stringValue end) as Actual_Attendees_No,"+
    "max(case when s.name = 'edb' then s.stringValue end) as EDB,"+
    "max(case when s.name = 'ss_teacher' then s.stringValue end) as Secondary_School_teacher,"+
    "p.description_text "+
    "from sitescape.ss_folderentries "+
    "p join sitescape.ss_customattributes s on p.id = s.folderEntry "+
    "group by "+
    "p.id,"+
    "s.folderEntry");  
while(rs.next()){%> 

您的 SQL 仅在 java 代码中看起来是多行的。但是您正在使用字符串连接。因此,它仅将 "" 标记内的内容加在一起。

这意味着不同部分之间没有 space,您的 <br> 不是有效的 SQL。

例如这部分:

"p.description_text"+"<br>"+
"from sitescape.ss_folderentries"+
"p join sitescape.ss_customattributes s on p.id = s.folderEntry"+
"group by"+
"p.id,"+

会给你这个字符串:

p.description_text<br>from sitescape.ss_folderentriesp join sitescape.ss_customattributes s on p.id = s.folderEntrygroup byp.id,

如您所见,别名 p 已添加到 sitescape.ss_folderentries 成为 sitescape.ss_folderentriesp,就 SQL 而言,这不是一个名称table 它知道。 s.folderEntrygroupbyp.id 也是如此,你真的很想成为 s.folderEntry group by p.id

所以基本上,您应该在每个连接部分的开头添加一个 space。这将解决单词粘在一起的问题。

你对 <br> 的意图是什么?您是要在描述文字中添加 <br> 吗?如果是这样,它应该用单引号引起来。

" p.description_text + '<br>'"

(假设您 SQL 文本连接运算符是 +