qq_37011814
码码码码农
采纳率33.3%
2017-04-24 09:01 浏览 2.3k
已采纳

jsp页面中无法显示数据库的表内容,求大神解答。

java文件
package xscj_bean;
import java.sql.*;
public class SQLServerConnBean {
private Statement stmt = null;
private Connection conn = null;
ResultSet rs = null;
//构造函数
public SQLServerConnBean () { }
public void OpenConn( ) throws Exception
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();

String url = "jdbc:sqlserver://localhost:1433;databaseName=classcircle";
String user = "sa";
String password = "123456789123456";
conn = DriverManager.getConnection( url, user, password );

}
catch(SQLException e)
{
System.err.println("Data.executeQuery: " + e.getMessage());
}

}
//执行查询类的SQL语句,有返回集
public ResultSet executeQuery(String sql)
{
rs = null;
try
{
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE
,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(sql);
}
catch(SQLException e)
{
System.err.println("Data.executeQuery: " + e.getMessage());
}
return rs;
}
//关闭对象
public void closeStmt()
{
try
{
stmt.close();
}
catch(SQLException e)
{
System.err.println("Date.executeQuery: " + e.getMessage());
}
}
public void closeConn()
{
try
{
conn.close();
}
catch(SQLException e)
{
System.err.println("Data.executeQuery: " + e.getMessage());
}
}
}

 jsp文件
 <%@ page contentType="text/html; charset=utf-8"%>
<%@ page language="java" import="java.sql.*"%>
<jsp:useBean id="SqlBean" scope="page" class="xscj_bean.SQLServerConnBean" />
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>JDBC连接SQL Server数据库</title>
    <style type="text/css">
    <!--
        .style1{
            color:#ff0000;
            font-size:24px;
        }
    -->
    </style>
</head> 
<body>
    <div align="center">
        <span class="style1">JDBC连接SQL Server数据库</span><br><hr><br>
    </div>
    <table border="2" bordercolor="#ffcccc" align="center">
        <tr bgcolor="cccccc" align="center">
            <td>学号</td>
            <td>姓名</td>
            <td>专业</td>
            <td>总学分</td>
        </tr>
        <%
            //查询XSB表中XH,XM,ZY和ZXF字段的前10条记录
            String sql="select top 10* From student";
            SqlBean.OpenConn( );                    //调用SqlBean中加载JDBC驱动的成员函数
            ResultSet  rs=SqlBean.executeQuery(sql);    //取得结果集

            while( rs.next( ) )
            {
        %>
        <tr>
            <td><%=rs.getString("username")%></td>
            <td><%=rs.getInt("password")%></td>
            <td><%=rs.getInt("schoolnumber")%></td>
            <td><%=rs.getInt("ZXF")%></td>
        </tr>
        <%
            }
        %>
        <%
            out.print("数据库操作成功,恭喜你!");
            rs.close();
            SqlBean.closeStmt();
            SqlBean.closeConn();
        %>
    </table>
</body>
</html>

结果却是这样的
图片说明
小白求助啊!!!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答 默认 最新

  • 已采纳
    Gr_lbxx Gr_lbxx 2017-04-24 09:30

    在后台就应该将数据放好 这块应该写在后台

    <%=rs.getString("username")%>
    <%=rs.getInt("password")%>
    <%=rs.getInt("schoolnumber")%>
    <%=rs.getInt("ZXF")%>
    用bean封装起来 前台页面直接取值
    点赞 评论 复制链接分享
  • qq_31745841 qq_31745841 2017-04-24 09:58

    现在哪儿还在前台写这些东西,后台写多简单。错误在

    里面不应该写<%= %>,应该用<%%>
    点赞 评论 复制链接分享

相关推荐