英文:
The servlet does not show all the match results from joining two tables
问题
我在我的 JSP 中有一个字段 <c:param name="names" value="${tempstudent.name}"/>
用于显示用户名称,我的 servlet 通过 String names=request.getParameter("names")
获取这个字段,并将其作为参数发送到名为 public List<studentrgister> showcourse(String names)
的 student_db_util 类中的方法,该方法的任务是获取一个名称,并在我的 SQL 查询中使用它来显示来自 2 个表的匹配结果(mytest 表具有 firstName、lastName、id 列,class 表具有 id、class 列)。对于这个应用程序的问题是,在两个表中都存在多个匹配结果,当我联接这两个表时,但我的应用程序在我的 JSP 页面中只显示一个匹配结果。
<body>
<table>
<form action="studentregister" method="get">
<input type="hidden" name ="comand" value="seecourse"/>
<c:forEach var="tempstudent" items="${mycourse}">
<td >${tempstudent.name}</td>
</c:forEach>
</form>
</table>
JSP 页面中的 names 字段所在位置:
<form action="studentregister" method="get">
<input type="hidden" name="command" value="pass" />
<table>
<c:forEach var="tempstudent" items="${studentlist}">
<c:url var="seecourse" value="studentregister">
<c:param name="command" value="seecourse"/>
<c:param name="names" value="${tempstudent.name}"/>
</c:url>
<td >${tempstudent.email}</td>
<td >${tempstudent.name}</td>
<a href="${seecourse}">
seecourse</a>
</c:forEach>
</table>
</form>
Servlet 方法:
private void seecourse(HttpServletRequest request, HttpServletResponse response) throws Exception {
String names = request.getParameter("names");
student_db_util.showcourse(names);
List<studentrgister> student = student_db_util.showcourse(names);
request.setAttribute("mycourse", student);
RequestDispatcher rd = request.getRequestDispatcher("/showcourse.jsp");
rd.forward(request, response);
}
student_db_util 类中的方法:
public List<studentrgister> showcourse(String names) throws Exception {
List<studentrgister> students = new ArrayList<>();
String connectionURL = "jdbc:mysql://localhost:3306/web_student_tracker";
System.out.println("loading the driver");
Statement s = null;
studentrgister mystudent = null;
Connection myConn = null;
myConn = DriverManager.getConnection(connectionURL, "webstudent", "webstudent");
System.out.println("username and password are correct");
PreparedStatement myStmt = null;
ResultSet myRs = null;
String name = names;
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("driver is loaded");
String sql = "SELECT mytest.firstName,class.name FROM mytest "
+ "INNER JOIN class ON mytest.id=class.id "
+ "where mytest.firstName = ? ";
PreparedStatement ps = myConn.prepareStatement(sql);
ps.setString(1, name);
myRs = ps.executeQuery();
if (myRs.next()) {
String classname = myRs.getString("name");
mystudent = new studentrgister(classname);
students.add(mystudent);
} else {
throw new Exception("Could not find student id: " );
}
return students;
} finally {
close(myConn, myStmt, null);
}
}
英文:
I have the field in my JSP <c:param name="names" value="${tempstudent.name}"/>
for showing the user name and my servlet take this field by String names=request.getParameter("names")
and send it to the student_db_util class in the method named public List <studentrgister> showcourse(String names)
as a parameter which has a task to take a names and use it in my sql query to show match result from 2 tables(mytest table which has a firstName,lastName, id column and class table which has an id, class column). the problem for this app is that in both tables there is more than one match result when I join both tables but my app only show on match result in my JSP page
<body>
<table>
<form action="studentregister" method="get">
<input type="hidden" name ="comand" value="seecourse"/>
<c:forEach var="tempstudent" items="${mycourse}">
<td >${tempstudent.name}</td>
</c:forEach>
</form>
</table>
jsp page that names field located
<form action="studentregister" method="get">
<input type="hidden" name="command" value="pass" />
<table>
<c:forEach var="tempstudent" items="${studentlist}">
<c:url var="seecourse" value="studentregister">
<c:param name="command" value="seecourse"/>
<c:param name="names" value="${tempstudent.name}"/>
</c:url>
<td >${tempstudent.email}</td>
<td >${tempstudent.name}</td>
<a href="${seecourse}"
>
seecourse</a>
</c:forEach>
</table>
</form>
servlet method
private void seecourse(HttpServletRequest request, HttpServletResponse response) throws Exception {
String names=request.getParameter("names");
student_db_util.showcourse(names);
List<studentrgister> student=student_db_util.showcourse(names);
request.setAttribute("mycourse", student);
RequestDispatcher rd=request.getRequestDispatcher("/showcourse.jsp");
rd.forward(request,response);
}
method in student_db_util class
public List <studentrgister> showcourse(String names) throws Exception {
List<studentrgister> students = new ArrayList<>();
String connectionURL = "jdbc:mysql://localhost:3306/web_student_tracker";
System.out.println("loding the driver");
Statement s=null;
studentrgister mystudent=null;
Connection myConn = null;
myConn= DriverManager.getConnection(connectionURL, "webstudent", "webstudent");
System.out.println("username and password is correect");
PreparedStatement myStmt=null;
ResultSet myRs = null;
String name=names;
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("driver is loaded");
//myConn = dataSource.getConnection();
String sql = "SELECT mytest.firstName,class.name FROM mytest "
+ "INNER JOIN class ON mytest.id=class.id "
+ "where mytest.firstName = ? " ;
PreparedStatement ps = myConn.prepareStatement(sql);
ps.setString(1,name);
myRs=ps.executeQuery();
if (myRs.next()) {
String classname = myRs.getString("name");
//String numbername=myRs.getString("firstName");
mystudent = new studentrgister(classname);
students.add(mystudent);
}
else {
throw new Exception("Could not find student id: " );
}
return students;
}
finally {
close(myConn,myStmt,null);
}
}
答案1
得分: 0
这个List声明在方法内部,但方法返回这个变量,我假设students变量应该在方法外部的可访问范围内。你应该在类中全局声明这个List,这样它就可以在方法外部访问,或者如果你有另一个方法外部的students变量需要返回,就不能返回方法内部声明的局部变量。
List<studentrgister> students = new ArrayList<>();
英文:
This List declaration is inside the method but the method returns the variable,
i assume the students variable should be outside the method in an accessible scope to be used. You should declare the List globally in the class so it can be accessed outside the method or if you have another students variable outside to return to it cannot return the local scoped variable declared in the method.
List<studentrgister> students = new ArrayList<>();
专注分享java语言的经验与见解,让所有开发者获益!
评论