具体代码如下: !--导入包--%@ page language= " java " import= " java.util.* " %%@page import= " java.sql.* " %%@ page contentType= " text/html;charset=UTF-8 " language= " java " %!DOCTYPE HTMLhtml head title查询所有用户的记录
具体代码如下:
<!--导入包--> <%@ page language="java" import="java.util.*" %> <%@page import="java.sql.*" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE HTML> <html> <head> <title>查询所有用户的记录</title> <style> td{ width: 65px; } </style> </head> <body> <% //创建地址 String url="jdbc:sqlserver://localhost\\SQLEXPRESS:1433;DatabaseName=StudentGrade"; //用户名 String user="sa"; //密码 String pwd=""; //连接对象 Connection conn=null; try{ //加载驱动程序类 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); }catch (Exception ex){ System.out.println("错误"+ex); } try { //获取数据库连接 conn=DriverManager.getConnection(url,user,pwd); }catch (Exception ex){ System.out.println("连接数据库错误"+ex); } //创建Statement对象 Statement stmt=conn.createStatement(); //数据库查询语句 String sqlStr="select * from Grade"; //结果集 ResultSet rs=stmt.executeQuery(sqlStr); int i = 0; %> <% while (rs.next()){ %> <table border="1" cellpadding="1" cellspacing="0"> <% if (i==0){ %> <tr> <th>学号</th> <th>姓名</th> <th>性别</th> <th>语文成绩</th> <th>数学成绩</th> <th>英语成绩</th> </tr> <% i++; } %> <tr> <td><%=rs.getInt(1)%></td> <td><%=rs.getString(2)%></td> <td><%=rs.getString(3)%></td> <td><%=rs.getInt(4)%></td> <td><%=rs.getInt(5)%></td> <td><%=rs.getInt(6)%></td> </tr> </table> <% } %> </body> </html>