2017-02-10 30 views
-1

我在找遍地方,幾本書,沒有發現簡單的東西。我有一個像SQL:春天java到jsp - 多表選擇

select 
a.id,b.name,b.points,d.name,d.second_name,c.id 
from my_courses a 
left join course b on a.idcourse=b.id 
left join student c on a.idstudent=c.id 
left join human d on c.idhuman=d.id 

,我想以執行(在Java)和顯示器(在表JSP)幾個其他隨機SQL無需DAO,POJO和其他類。是否有可能一些簡單的解決方案,如編寫一個SQL,執行SQL,獲取數組或數組或填充sql結果並將其發送到jsp以顯示在表中?非常感謝。 我在spring工具套件中使用java,spring,jsp。

+0

現在看來我正在尋找的東西就像'SqlRowSet foo = jdbcTemplate.queryForRowSet(sql);'。 –

回答

0

在您閱讀以下的答案,請明確:

  • 你應該基本永遠不要組合表示層/數據訪問層一起。你會最終陷入混亂/流淚。

但是我發現了一個解決方案,您可以詳細的在這裏找到:

data-acces in presentation layer

這裏是從源代碼示例:

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%> 
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver" 
url="jdbc:mysql://localhost/TEST" 
user="root" password="pass123"/> 

<sql:query dataSource="${snapshot}" var="result"> 
SELECT * from Employees; 
</sql:query> 

<table border="1" width="100%"> 
<tr> 
    <th>Emp ID</th> 
    <th>First Name</th> 
    <th>Last Name</th> 
    <th>Age</th> 
</tr> 
<c:forEach var="row" items="${result.rows}"> 
<tr> 
    <td><c:out value="${row.id}"/></td> 
    <td><c:out value="${row.first}"/></td> 
    <td><c:out value="${row.last}"/></td> 
    <td><c:out value="${row.age}"/></td> 
</tr> 
</c:forEach> 
</table> 

請注意,我不建議。這太糟糕了!很壞!