创建数据库表

JSP + JDBC + MySQL 读取数据库内容到网页_第1张图片

导入JDCB驱动

JSP + JDBC + MySQL 读取数据库内容到网页_第2张图片

mysql.jsp

<%@ page language="java" %>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="com.mysql.jdbc.Driver" %>

    
        
        mysql
    
    
        <%
            //数据库URL
            String url = "jdbc:mysql://localhost/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8";
            //数据库用户名
            String userName = "root";
            //数据库密码
            String password = "root";
            //加载驱动程序
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            //创建conn对象连接数据库
            Connection conn = DriverManager.getConnection(url,userName,password);
            //创建stmt对象提交SQL语句到数据库
            Statement stmt = conn.createStatement();
            //mysl语句
            String sql = "select * from data";
            //执行查询
            ResultSet rs = stmt.executeQuery(sql);
            int i = 0;
            while (rs.next()){
                i++;
                out.println("

用户" + i + " "); out.println(rs.getString(2)); out.println("

"); } out.print("
"); //关闭数据库连接释放资源 rs.close(); stmt.close(); conn.close(); %>

效果

JSP + JDBC + MySQL 读取数据库内容到网页_第3张图片