解释及说明在代码的注释中即可查看
<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://数据库地址:端口号/" + 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)
%>
<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">
<%
out.print("数据查询成功,恭喜你")
%>
</div>
<%
rs.close()
statement.close()
connection.close()
%>
</body>
</html></span><span style="font-size:24pxcolor: rgb(255, 0, 0)">
</span></span>
显示结果如下所示:
在数据库提取部分数据,在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条)