<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/9/24
Time: 9:30
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>把数据库的表显示在页面</title>
</head>
<body>
-%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>mysql测试</title>
</head>
<body>
-%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>mysql测试</title>
</head>
<body>
<%
try {
Class.forName("com.mysql.jdbc.Driver"); ////加载数据库驱动
String url = "jdbc:MySQL://localhost:3306/kh87?&useSSL=false&serverTimezone=UTC"; //指向数据库table
String username = "root"; //数据库用户名
String password = "ROOT"; //数据库用户密码
Connection conn = DriverManager.getConnection(url, username, password);
if(conn != null){
out.print("数据库连接成功!");
out.println("<br />");
out.println("-------------------------------");
out.print("<br />");
Statement stmt = null;
ResultSet rs = null;
String sql ="select * from user"; //查询语句
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
out.println("执行结果如下所示:");
out.println("<br />");
out.println("-------------------------------");
out.println("<br />");
out.println("id" + "\t" + "name"+"\t"+"age"+"\t");
out.println("<br />");
while (rs.next()) {
out.println(rs.getString("id")+"   "+rs.getString("name")+"   "+rs.getString("age")+"   "); //将数据库表格查询结果输出
out.print("<br />");
}
}
else{
out.print("连接失败!");
}
}
catch (Exception e) {
out.print("数据库连接异常!");
}
%>
</body>
</html>
原文:https://www.cnblogs.com/tiantongtong/p/13226778.html