在JSP中运行多行SQL查询

时间:2015-01-19 10:24:08

标签: java sql 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查询,没有错误。 不知道发生了什么。有人会暗示它吗?

4 个答案:

答案 0 :(得分:0)

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

此:

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

应该是:

p.description_text"+" "+

答案 1 :(得分:0)

您的代码几乎没有问题。首先,在进行多行查询时要注意空格(例如,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"); 

答案 2 :(得分:0)

您首先在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()){%> 

答案 3 :(得分:0)

您的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

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

你对<br>的意图是什么?您的意思是在说明文字中添加<br>吗?如果是这样,它应该是单引号。

" p.description_text + '<br>'"

(假设您的SQL文本并置运算符为+