繁体   English   中英

在JSP中运行多行SQL查询

[英]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()){%>  

我收到一个错误:

An error occurred in a custom jsp: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from sitescape.ss_folderentriesp join sitescape.ss_customattributes s on p.id = ' at line 1

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

您不在查询中使用thml标记:

这个:

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

应该:

p.description_text"+" "+

您的代码很少有问题。 首先,在进行多行查询时要注意空格(例如, from将被粘贴到上一行,最好在每行的末尾添加一个空格)。 其次,如果您想将<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代码中是多行的。 但是您正在使用字符串连接。 因此,它仅将""标记内的内容加在一起。

这意味着不同部分之间没有空格,并且<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而言,它不是它知道的表的名称。 s.folderEntrygroupbyp.id相同,您确实希望s.folderEntry group by p.id成为s.folderEntry group by p.id

因此,基本上,您应该在每个串联部分的开头添加一个空格。 这将解决单词粘在一起的问题。

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

" p.description_text + '<br>'"

(假设您的SQL文本串联运算符为+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM