testExecuteQuary.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <%@page import="java.sql.*" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'testExecuteQuery.jsp' starting page</title> </head> <body> <% //student为数据库的名字 String url="jdbc:mysql://localhost:3306/student?useSSL=true"; String useName="root"; String password="2277092"; String sql=null; Connection conn=null; Statement stmt=null; //加载驱动类 try{ Class.forName("com.mysql.jdbc.Driver"); out.print("加载驱动类成功"); } catch(ClassNotFoundException e){ out.print("加载驱动类时出现异常"); } try{ conn=DriverManager.getConnection(url,useName,password); stmt=conn.createStatement(); sql="select * from info";//info为表名 ResultSet rs=stmt.executeQuery(sql); %> <center> <table border="1" width="270" cellspacing="0" cellpadding="0"> <tr> <td width=50 align="center" valign="middle">学号</td> <td width="80" align="center">姓名</td> <td width=140 align="center">电话</td> </tr> <% while(rs.next()){ int id=rs.getInt(1); String name=rs.getString(2); String phone=rs.getString(3); %> <tr> <td height="40" align="center" valign="middle"><%=id %></td> <td align="center" valign="middle"><%=name %></td> <td align="center" valign="middle"><%=phone %></td> </tr> <% } rs.close(); stmt.close(); %> </table> </center> <% } catch(SQLException e){ out.print("出现SQLException异常"); } finally{ try{ if(conn!=null){ conn.close(); } } catch(SQLException e){ out.print("关闭数据库连接时出现异常"); } } %> </body> </html>