<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ page import ="java.sql.*"%>
<%
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/bbs?user=root&characterEncoding=gbk";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from article");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<title>Insert title here</title>
</head>
<body>
<table border = 1>
<%
while(rs.next()){
%>
<tr>
<td><%= rs.getInt("id") %></td>
<td><%= rs.getString("cont") %></td>
</tr>
<%
}
%>
</table>
</body>
<%
stmt.close();
rs.close();
conn.close();
%>
</html>jsp链接数据库
最新推荐文章于 2025-07-31 09:52:35 发布
本文介绍了一个使用Java和JSP从MySQL数据库中检索并显示数据的基本示例。通过加载MySQL JDBC驱动程序,建立数据库连接,并执行SQL查询来获取文章列表。每篇文章的ID和内容被展示在一个HTML表格中。

1125

被折叠的 条评论
为什么被折叠?



