rs对象
把查询后的ResultSet集复制给rs
然后通过调用rs的next()方法将指针向下移动
实现循环显示数据
数据的显示是调用rs的getXxxx(列名或列号)
其中Xxxx是数据类型
如while(rs.next()){out.print(rs.getString(列名))}
希望能帮到你
在数据库提取部分数据,在JSP上显示的做法如下:思路:1、创建db连接 2、创建statement 3、执行查询 4、遍历结果并展示
完整代码如下:
<span style="font-size:12px"><span style="font-size:14px"><%@ page language="java" import="java.sql.*,java.io.*,java.util.*"%>
<%@ page contentType="text/htmlcharset=utf-8"%>
<html>
<head>
<style type="text/css">
table {
border: 2px #CCCCCC solid
width: 360px
}
td,th {
height: 30px
border: #CCCCCC 1px solid
}
</style>
</head>
<body>
<%
//驱动程序名
String driverName = "com.mysql.jdbc.Driver"
//数据库用户名
String userName = "root"
//密码
String userPasswd = "szy"
//数据库名
String dbName = "studentmanage"
//表名
String tableName = "student"
//联结字符串
String url = "jdbc:mysql://localhost:3306/" + dbName + "?user="
+ userName + "&password=" + userPasswd
Class.forName("com.mysql.jdbc.Driver").newInstance()
Connection connection = DriverManager.getConnection(url)
Statement statement = connection.createStatement()
String sql = "SELECT * FROM " + tableName
ResultSet rs = statement.executeQuery(sql)
%>
<br>
<br>
<table align="center">
<tr>
<th>
<%
out.print("学号")
%>
</th>
<th>
<%
out.print("姓名")
%>
</th>
<th>
<%
out.print("专业")
%>
</th>
<th>
<%
out.print("班级")
%>
</th>
</tr>
<%
while (rs.next()) {
%>
<tr>
<td>
<%
out.print(rs.getString(1))
%>
</td>
<td>
<%
out.print(rs.getString(2))
%>
</td>
<td>
<%
out.print(rs.getString(3))
%>
</td>
<td>
<%
out.print(rs.getString(4))
%>
</td>
</tr>
<%
}
%>
</table>
<div align="center">
<br><br><br>
<%
out.print("数据查询成功,恭喜你")
%>
</div>
<%
rs.close()
statement.close()
connection.close()
%>
</body>
</html></span><span style="font-size:24pxcolor: rgb(255, 0, 0)">
</span></span>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)