1.新建一个Dynamic Web Project项目,里面新建一个jsp文件,然后将要用的jar文件放在lib目录下,整体的框架如图所示:
2.数据库里面新建一个数据库并且填入几行记录,如下图所示:
3.one.jsp文件里面的代码如下所示:
<%@ page contentType="text/html; charset=GBK" language="java"
errorPage=""%>
<%@ page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>小脚本测试</title>
<meta name="website" content="http://www.crazyit.org" />
</head>
<body bgcolor="lightpink">
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/javaee", "root", "root");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from news_inf");
%>
<table bgcolor="lightgreen" border="1" width="300">
<%
while (rs.next()) {
%>
<tr>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
</tr>
<%
}
%>
<table>
</body>
</html>
4.在浏览器里面输入http://localhost:8080/demo/one.jsp运行可以得到结果: