如何在JSP页面显示mysql数据库内容
1个回答
展开全部
显示数据库数据的jsp代码如下:
解释及说明在代码的注释中即可查看 <span style="font-size:12px;"><span style="font-size:14px;"><%@ page language="java" import="java.sql.*,java.io.*,java.util.*"%> <%@ page contentType="text/html;charset=utf-8"%> <html> <head> <style type="text/css"> table { border: 2px #CCCCCC solid; width: 360px; } td,th { height: 30px; border: #CCCCCC 1px solid; } </style> </head> <body> <% //驱动程序名 String driverName = "com.mysql.jdbc.Driver"; //数据库用户名 String userName = "root"; //密码 String userPasswd = "szy"; //数据库名 String dbName = "studentmanage"; //表名 String tableName = "student"; //连接字符串 String url = "jdbc:mysql://数据库地址:端口号/" + dbName + "?user=" + userName + "&password=" + userPasswd; Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection connection = DriverManager.getConnection(url); Statement statement = connection.createStatement(); String sql = "SELECT * FROM " + tableName; ResultSet rs = statement.executeQuery(sql); %> <table align="center"> <tr> <th> <% out.print("学号"); %> </th> <th> <% out.print("姓名"); %> </th> <th> <% out.print("专业"); %> </th> <th> <% out.print("班级"); %> </th> </tr> <% while (rs.next()) { %> <tr> <td> <% out.print(rs.getString(1)); %> </td> <td> <% out.print(rs.getString(2)); %> </td> <td> <% out.print(rs.getString(3)); %> </td> <td> <% out.print(rs.getString(4)); %> </td> </tr> <% } %> </table> <div align="center"> <% out.print("数据查询成功,恭喜你"); %> </div> <% rs.close(); statement.close(); connection.close(); %> </body> </html></span><span style="font-size:24px;color: rgb(255, 0, 0);"> </span></span>
解释及说明在代码的注释中即可查看 <span style="font-size:12px;"><span style="font-size:14px;"><%@ page language="java" import="java.sql.*,java.io.*,java.util.*"%> <%@ page contentType="text/html;charset=utf-8"%> <html> <head> <style type="text/css"> table { border: 2px #CCCCCC solid; width: 360px; } td,th { height: 30px; border: #CCCCCC 1px solid; } </style> </head> <body> <% //驱动程序名 String driverName = "com.mysql.jdbc.Driver"; //数据库用户名 String userName = "root"; //密码 String userPasswd = "szy"; //数据库名 String dbName = "studentmanage"; //表名 String tableName = "student"; //连接字符串 String url = "jdbc:mysql://数据库地址:端口号/" + dbName + "?user=" + userName + "&password=" + userPasswd; Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection connection = DriverManager.getConnection(url); Statement statement = connection.createStatement(); String sql = "SELECT * FROM " + tableName; ResultSet rs = statement.executeQuery(sql); %> <table align="center"> <tr> <th> <% out.print("学号"); %> </th> <th> <% out.print("姓名"); %> </th> <th> <% out.print("专业"); %> </th> <th> <% out.print("班级"); %> </th> </tr> <% while (rs.next()) { %> <tr> <td> <% out.print(rs.getString(1)); %> </td> <td> <% out.print(rs.getString(2)); %> </td> <td> <% out.print(rs.getString(3)); %> </td> <td> <% out.print(rs.getString(4)); %> </td> </tr> <% } %> </table> <div align="center"> <% out.print("数据查询成功,恭喜你"); %> </div> <% rs.close(); statement.close(); connection.close(); %> </body> </html></span><span style="font-size:24px;color: rgb(255, 0, 0);"> </span></span>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询