吴裕雄--天生自然JAVA开发JSP-Servlet学习笔记:JSP脚本

<%@ page contentType="text/html; charset=GBK" language="java"
    errorPage=""%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>小脚本测试</title>
</head>
<body>
    <table bgcolor="#9999dd" border="1" width="300px">
        <!-- java脚本,这些脚本会对HTML的标签产生作用 -->
        <%
            for(int i=0; i<10; i++){
                %>
        <tr>
            <td>循环值</td>
            <td><%=i %></td>
        </tr>
        <%
            }
         %>
    </table>
</body>
</html>

<%@ page contentType="text/html; charset=GBK" language="java"
    errorPage=""%>

<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.SQLException"%>
<%@ page import="java.sql.Statement"%>
<%@ page import="java.sql.ResultSet"%>

<!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>
</head>
<body>
    <%!
    // 定义MySQL的数据库驱动程序
    public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
    // 定义MySQL数据库的连接地址
    public static final String DBURL = "jdbc:mysql://localhost:3306/taobao";
    // MySQL数据库的连接用户名
    public static final String DBUSER = "root";
    // MySQL数据库的连接密码
    public static final String DBPASS = "admin";
         %>
    <%        //注册数据库驱动
            Class.forName(DBDRIVER);
            //获取数据库连接
            Connection conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
            //创建Statement
            Statement stmt = conn.createStatement();
            //执行查询
            ResultSet rs = stmt.executeQuery("select * from person");
         %>
    <table bgcolor="#9999dd" border="1" width="300px">
        <%
                 //遍历结果集
                 while(rs.next()){
                     %>
        <tr>
            <td><%=rs.getInt(1) %></td>
            <td><%=rs.getString(2) %></td>
            <td><%=rs.getInt(3) %></td>
        </tr>
        <%
                 }
              %>
    </table>
</body>
</html>

原文地址:https://www.cnblogs.com/tszr/p/12663859.html