本文目錄一覽:
- 1、JSP頁面怎麼得到資料庫中的數據?
- 2、jsp頁面是能否通過${}獲取資料庫里的值,如何實現
- 3、SSH框架中jsp頁面如何讀取外鍵屬性值?
- 4、jsp獲取資料庫中的數據
- 5、如何在jsp頁面獲取資料庫數據
- 6、如何在jsp頁面獲取資料庫中的數據
JSP頁面怎麼得到資料庫中的數據?
1、jsp頁面寫小腳本可以得到
2、可以是使用ajax技術 非同步進行訪問
3、可以配置web.xml 裡面配置好servlet
4、使用框架的話,就直接用框架技術來得到…….
總之,方法很多,看你是要採用哪種方法來做
jsp頁面是能否通過${}獲取資料庫里的值,如何實現
你當然要先在後台從資料庫獲取到值,然後,setAttribute到頁面,這時,頁面才可以用${}獲取到對應的值!
SSH框架中jsp頁面如何讀取外鍵屬性值?
SSH框架中jsp頁面讀取外鍵屬性值的方法如下:
當程序在載入該對象時,會將外鍵對應的對象屬性載入進來:
many-to-one name=”teacher” class=”net.zjl.po.Teacher” fetch=”select” lazy=”false”
column name=”tid” /
/many-to-one
在jsp頁面上用s標籤迭代輸出:
s:iterator value=”list”
tr
tds:property value=”id” /
/td
tds:property value=”title” /
/td
tds:property value=”tkey” /
/td
tds:property value=”comment” /
/td
tds:property value=”teacher.tname” /
/td
tds:property value=”teacher.office” /
/td
tds:property value=”teacher.phone” /
/td
/tr
/s:iterator
這裡的teacher即為該類外鍵
jsp獲取資料庫中的數據
%
//JSP頁面直接訪問資料庫
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName(“JDBC驅動”);
conn = DriverManager.getConnection(“url”, “username”, “password”);
stmt = conn.createStatement();
rs = stmt.executeQuery(“select factor, ratio from 表名 where id=1”);
while(rs.next()){
String factor = rs.getString(“factor”);
String ratio = rs.getString(“ratio”);
%
factor :%=factor %
ratio :%=ratio %
%
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
%
修改 驅動、url、username、password、表名、欄位名成你應用的相應數據,然後將這些代碼加入到你的jsp頁面,就可以在jsp頁面直接讀取到資料庫中的對應表指定欄位的數據了,祝你好運!
如何在jsp頁面獲取資料庫數據
把數據封裝在List中,把list放入request作用域鍾,在前台用foreach循環你的list就好了
如何在jsp頁面獲取資料庫中的數據
建立資料庫連接
調用方法,比如listUser userlist = DB.findAll(), req.setAttribute(“list”,userlist)
jsp部分:c:forEach items=”list” var=”user”
td${user.id}/td //顯示User對象的id屬性
/c:forEach
用到forEach,要引入jstl.jar
原創文章,作者:XGOYR,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/317078.html