[英]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.folderEntrygroup
和byp.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.