首页 > 编程知识 正文

jsp里链接数据库显示数据列表,jsp显示数据库内容

时间:2023-12-28 21:10:48 阅读:328664 作者:EWEF

本文目录一览:

在jsp页面上显示数据库一个表中所有的的内容。

在jsp页面上显示数据库一个表中所有的的内容的方法是迭代。

1、jsp页面接收所有内容的bookslist:

html

body

head

title

View Books

/title

/head

body

table border=2

tr

thBook ID/th

thTitle/th

thAuthor/th

thNo. of copies AVAILABLE/th

thNumber of favourites/th

/tr

%

ArrayListBook dbooks=(ArrayList)request.getAttribute("bookslist");

Iterator it=dbooks.iterator();

while(it.hasNext())

{

Book b=(Book)it.next();

%

tr

td%=b.bookID%/td

td%=b.bookTitle%/td

td%=b.bookAuthor%/td

td%=b.bookCopies%/td

td%=b.bookFavs%/td

/tr

%

}

%

/table

/body

/html

2、java代码获取数据库内容:

try

{

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3307/library", "root", "admin");

PreparedStatement ps=con.prepareStatement("select * from book");

ResultSet rs=ps.executeQuery();

ArrayListBook books=new ArrayListBook();

while(rs.next())

{

Book b= new Book();

b.bookID=rs.getInt(3);

b.bookTitle=rs.getString(1);

b.bookAuthor=rs.getString(2);

b.bookCopies=rs.getInt(4);

b.bookFavs=rs.getInt(5);

books.add(b);

}

req.setAttribute("bookslist",books);

con.close();

jsp如何把数据库查询到的值以列表形式展现

table border="1" width="80%" id="xxb"

thead

thinput name="" type="checkbox" value="" //th

th姓名/th

th宿舍号/th

th维修类型/th

th具体描述/th

th联系方式/th

th提交时间/th

/thead

tbody

%

while(rs.next()){

String name=rs.getString(1);

String susehao=rs.getString(2);

String wxlx=rs.getString(3);

String jtms=rs.getString(4);

String tel=rs.getString(5);

String tjsj=rs.getString(6);

%

tr

td class="checkBox"input name="" type="checkbox" value="" //td

td%=name%/td

td%=susehao%/td

td%=wxlx%/td

td%=jtms%/td

td%=tel%/td

td%=tjsj%/td

/tr

%

}

%

/tbody

%

pstmt.close();

rs.close();//关闭命令对象连接

con.close();//关闭数据库连接

}catch(SQLException e){

e.printStackTrace();

}

%

/table

jsp 如何把数据库的某个字段的数据以列表的形式显示出来

这个有多种实现方法啊,推荐你使用JSTL表达式

从数据库中查处数据放到List集合中,然后把list放入范围里(request、session等)代码如下:

request.setAttribute("list",list);

c:forEach

var="user"

items="${requestScope.list}"

varStatus="num"

${num.count}:${user.name}br

/c:forEach

就可以得到你要的结果。其中user是个变量,存储了每次循环的一个list值。num.count就代表循环次数

如何在jsp上查询并显示数据库mysql的数据表格

首先下载相应的jar包。

建立数据库连接类,

新建一个类,属性设置表里的每个字段。假设为user类

再建一个类,假设名为UserImpl和相应的sql查询方法。查询结果用ArrayList保存

在jsp页面中,java脚本,调用该方法

table

%

UserImpl odi=new UserImpl();

实例化此类,设其对象名为odi

ArrayListuser  h=(ArrayList user)odi.方法

for(User ul:h)

{

%

tr根据相应的情况写/tr

%}%

/table

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。