在连接数据库执行完sql语句后,要把查询的值在JSP 页面中显示出来。但是出现当查询语句出现表与表之间属性关联的就无法在页面中显示查询结果。
比如简单对单个表进行查询输出。如下面的可以在网页中正常显示:
- Java code
<%String dbDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";String dbURL="jdbc:sqlserver://localhost:1433;databaseName=classroom;user=sa;password=19881206";Connection conn = null;Statement stmt= null;String sql = null;ResultSet rs = null;String str=request.getParameter("CRno"); try{ Class.forName(dbDriver); conn=DriverManager.getConnection(dbURL); stmt=conn.createStatement(); sql="SELECT CRno,Capacity FROM classroom where classroom.CRno='"+str+"'"; rs=stmt.executeQuery(sql); %> <center> <table border=2> <caption>教室信息</caption> <tr> <th>教室编号</th><th>教室容量</th> </tr> <%while(rs.next()){ %> <tr> <td><%=rs.getString("CRno")%></td> <td><%=rs.getString("Capacity")%></td> </tr> <%} conn.close(); } catch(SQLException e) {System.out.println(e); } %>
此页面能够正常的把CRno与Capacity 在table中显示出来
但是下面的就只是把table都头显示出来了,查询出来的内容没有显示。下面是通过teacher表中的Tname进行查询,把teacher表与teach_course表通过teacher.Tno=teach_course.Tno关联,course表与teach_course表通过course.Cno=teach_course.Cno关联。teacher表与dept表通过teacher.Deptno=dept.Deptno关联。查询出Deptname,Ttitle,Cname,CRno等。。
(查询语句已经查询通过,但是页面不显示查询结果)
- Java code
<% String dbDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String dbURL="jdbc:sqlserver://localhost:1433;databaseName=classroom;user=sa;password=19881206"; Connection conn = null; Statement stmt= null; String sql = null; ResultSet rs=null; String str; str=request.getParameter("Tname"); try{ Class.forName(dbDriver); conn=DriverManager.getConnection(dbURL); stmt=conn.createStatement(); sql="select Tname,Deptname,Ttitle,Cname,CRno,Weekday,Period from teacher,teach_course,course,dept where teacher.Tno=teach_course.Tno and teacher.Deptno=dept.Deptno and teach_course.Cno=course.Cno and teacher.Tname='"+str+"'"; rs=stmt.executeQuery(sql); %> <center> <table border=2> <caption>教师开课信息</caption> <tr><th>教师姓名</th><th>所属院系</th><th>职称</th><th>课程名</th><th>上课地点<th>上课时间</th><th>节次</th> </tr> <%while(rs.next()) {%> <tr> <td><%=rs.getString("Tname")%></td> <td><%=rs.getString("Deptname")%></td> <td><%=rs.getString("Ttitle")%></td> <td><%=rs.getString("Cname")%></td> <td><%=rs.getString("CRno")%></td> <td><%=rs.getString("Weekday")%></td> <td><%=rs.getString("Period")%></td> </tr> <%} conn.close(); }catch(SQLException e) { System.out.println(e); } %>
哪位能够解释下为什么啊,给个好的显示方法也行啊!万分感谢!
------解决方案--------------------
<%=rs.getString("Tname")%></td>
<td><%=rs.getString("Deptname")%></td>
<td><%=rs.getString("Ttitle")%></td>
<td><%=rs.getString("Cname")%></td>
<td><%=rs.getString("CRno")%></td>