一、配置tomcat

此处下载的tomcat版本是8.5.59
附下载连接
​​​https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.59/bin/​​​ 下载后解压到自选目录,
此处使用IDEA配置,新建一个maven的webapp项目,等待下载完成后

😊😊😊😊😊😊😊

JavaWeb(四)—— 配置tomcat并使用JSP显示数据库中的表格_tomcat


JavaWeb(四)—— 配置tomcat并使用JSP显示数据库中的表格_mysql_02


JavaWeb(四)—— 配置tomcat并使用JSP显示数据库中的表格_maven_03


点击+号,选择34 items more,找到tomcat Server,选择Local

JavaWeb(四)—— 配置tomcat并使用JSP显示数据库中的表格_sql_04


起名tomcat8,然后选择Configure,选中tomcat解压后的文件夹位置

JavaWeb(四)—— 配置tomcat并使用JSP显示数据库中的表格_sql_05

进行部署,选中跳出来的第二项,点击ok

JavaWeb(四)—— 配置tomcat并使用JSP显示数据库中的表格_mysql_06


此时运行tomcat8,即点击绿色三角,打开浏览器访问

​​http://localhost:8080/​​ 看到hello world则表示配置成功

二、使用JSP显示数据库中的表格

<%@ page import="java.sql.*" %>
<%@ page contentType="textml;charset=UTF-8" language= "java" %>
<html>
<body>


<% String name = "zhangsan!";%>
<%! int age=10;%>
<h1><%=name%></h1>
<h1><%=age%></h1>
<table border="1">
<tr><th>Name</th><th>author</th>><th>price</th>><tr>
<%
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

try {
Class.forName("com.mysql.cj.jdbc.Driver");

con = DriverManager.getConnection("jdbc:mysql://192.168.91.136:3306/scott", "root", "123QWEasd!");

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


rs = pstmt.executeQuery();
while (rs.next()){
%>

<tr><td><%=rs.getString("name")%></td><td><%=rs.getString("author")%></td><td><%=rs.getDouble("price")%></td></tr>

<%
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
pstmt.close();
con.close();
}catch (SQLException e){
e.printStackTrace();
}
}
%>
</table>
</body>
<html>