本文目錄一覽:
JSP獲取數據庫信息
table
width=”100%”
border=”0″
cellpadding=”0″
cellspacing=”1″
bgcolor=”#a8c7ce”
tr
align=”center”
height=”25″
bgcolor=”d3eaef”
td
width=”5%”
編號
/td
td
width=”10%”
標題
/td
td
width=”23%”
內容
/td
td
width=”10%”
發表日期
/td
td
width=”16%”
基本操作
/td
/tr
%
//獲取新聞信息集合,newList是從後台返回來的集合變量
List
nList
=
(List)
session.getAttribute(“newList”);
NewsEntity
new
=
null;
if
(nList.size()
=
0)
{
%
tr
height=”22″
bgcolor=”#FFFFFF”
align=”center”
td
colspan=”9″
align=”center”
暫無新聞信息
/td
/tr
%
}
else
{
for
(int
i
=
0;
i
nList.size();
i++)
{
new
=
(NewsEntity)
mList.get(i);
%
tr
height=”22″
bgcolor=”#FFFFFF”
align=”center”
td
%=new.getId()
%
/td
td
%=new.getTitle()
%
/td
td
%=new.getContent()
%
/td
td
%=new.time()
%
/td
td
a
href=”MusicServlet?forward=getNewsDetailByIdID=%=new.getId()%”
span
class=”STYLE2″編輯/span
/a
|
a
href=”MusicServlet?forward=doDelNewsByIdID=%=new.getId()%”
onclick=”return
confirm(‘您確定要刪除該條信息嗎?’);”span
class=”STYLE2″刪除/span
/a
/td
/tr
%
}
}
%
/table
如何在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
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頁面直接讀取到數據庫中的對應表指定字段的數據了,祝你好運!
原創文章,作者:OSXX,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/148813.html