为什么只能查表中的第一行数据?

wuxian2015 2015-11-20 12:55:46

public List<Student> FindAll(String studentName) throws Exception {
List<Student> list = new ArrayList<Student>();
String sql = "select Sid,Sname,Password,Java,c FROM student";
if (studentName != null && "".equals(studentName)) {
sql = "select Sid,Sname,Password,Java,c FROM student where Sname=?";

this.preparedStatement.setString(1, studentName);
} else {
this.preparedStatement = this.connection.prepareStatement(sql);
}
ResultSet resultSet = this.preparedStatement.executeQuery();
Student product = null;
if (resultSet.next()) {
product=new Student();
product.setStudentId(resultSet.getString(1));
product.setStudentName(resultSet.getString(2));
product.setStudent(resultSet.getString(3));
product.setInfo(resultSet.getString(4));
product.setInfo1(resultSet.getString(5));
list.add(product);
}
this.preparedStatement.close();
return list;
}







<%@ page language="java" import="java.util.*,com.eshore.st.Student" pageEncoding="utf-8"%>

<%@ page import="com.eshore.factory.DAOFactory" %>

<%
request.setCharacterEncoding("utf-8");//解决中文乱码
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>查询产品列表</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<%

String product_name=request.getParameter("username");
List<Student> list=DAOFactory.getIPMLDAOInstrance().FindAll(product_name);
//String product_name=request.getParameter("username");
// Student p=DAOFactory.getIPMLDAOInstrance().findByNmae(product_name);
%>

<table border="1">
<tr>
<td>学生ID</td>
<td>Java成绩</td>
<td>C++成绩</td>
</tr>
<%
for(int i=0;i<list.size();i++){

Student p=list.get(i);

%>
<tr>
<td><%=p.getStudentId() %></td>
<td><%=p.getInfo() %></td>
<td><%=p.getInfo1() %></td>
</tr>
<%} %>
</table>

</body>
</html>



我用表中的第二行数据登陆页面,但是显示出来的还是第一行数据的信息
...全文
222 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wuxian2015 2015-11-21

package com.eshore.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.eshore.dao.StudentDao;
import com.eshore.st.Student;


public class StudentDaoImpl implements StudentDao {
    private Connection connection = null;
    private PreparedStatement preparedStatement = null;

    public StudentDaoImpl(Connection connection) {
        this.connection = connection;
    }
    
    
    /*

    public boolean addStudent(Student student) throws Exception {
        boolean flag = false;
        String sql = "insert into student(student_id,student_name,student,info)" +
                "values(?,?,?,?)";
        this.preparedStatement = this.connection.prepareStatement(sql);
        this.preparedStatement.setString(1, student.getStudentId());
        this.preparedStatement.setString(2, student.getStudentName());
        this.preparedStatement.setString(3, student.getStudent());
        this.preparedStatement.setString(4, student.getInfo());
        if (this.preparedStatement.executeUpdate() > 0) flag = true;
        this.preparedStatement.close();
        this.preparedStatement = null;
        return flag;
    }
    */
    
    
    public boolean AddStudent(Student student) throws Exception {
        boolean flag = false;
        String sql = "insert into student(Sid,Sname,Password,Java,c)" +
                "values(?,?,?,?,?)";
        this.preparedStatement = this.connection.prepareStatement(sql);
        this.preparedStatement.setString(1, student.getStudentId());
        this.preparedStatement.setString(2, student.getStudentName());
        this.preparedStatement.setString(3, student.getStudent());
        this.preparedStatement.setString(4, student.getInfo());
        this.preparedStatement.setString(5, student.getInfo1());
        if (this.preparedStatement.executeUpdate() > 0) flag = true;
        this.preparedStatement.close();
        this.preparedStatement = null;
        return flag;
    }
    
    /*

    public List<Student> findAll(String studentName) throws Exception {
        List<Student> list = new ArrayList<Student>();
        String sql = "select student_id ,student_name,student,info FROM student";
        if (studentName != null && "".equals(studentName)) {
        	sql = "select student_id ,student_name,student,info FROM student where student_name like?";
           
            this.preparedStatement.setString(1, "%" + studentName + "%");
        } else {
        	this.preparedStatement = this.connection.prepareStatement(sql);
        }
        ResultSet resultSet = this.preparedStatement.executeQuery();
        Student product = null;
        while (resultSet.next()) {
        	product=new Student();
            product.setStudentId(resultSet.getString(1));
            product.setStudentName(resultSet.getString(2));
            product.setStudent(resultSet.getString(3));
            product.setInfo(resultSet.getString(4));
            list.add(product);
        }
        this.preparedStatement.close();
        return list;
    }
    
    */
    
    public List<Student> FindAll() throws Exception {
        List<Student> list = new ArrayList<Student>();
        String sql = "select Sid,Sname,Password,Java,c FROM student";
        this.preparedStatement = this.connection.prepareStatement(sql);
        ResultSet resultSet = this.preparedStatement.executeQuery();
        Student product = null;
        while (resultSet.next()) {
        	product=new Student();
            product.setStudentId(resultSet.getString(1));
            product.setStudentName(resultSet.getString(2));
            product.setStudent(resultSet.getString(3));
            product.setInfo(resultSet.getString(4));
            product.setInfo1(resultSet.getString(5));
            list.add(product);
        }
        this.preparedStatement.close();
        return list;
    }
    
    
    public List<Student> FindAll(String studentName) throws Exception {
        List<Student> list = new ArrayList<Student>();
        String sql = "select Sid,Sname,Password,Java,c FROM student";
        if (studentName != null && "".equals(studentName)) {
        	sql = "select Sid,Sname,Password,Java,c FROM student where Sname=?";
           
            this.preparedStatement.setString(1, studentName);
        } else {
        	this.preparedStatement = this.connection.prepareStatement(sql);
        }
        ResultSet resultSet = this.preparedStatement.executeQuery();
        Student product = null;
        if (resultSet.next()) {
        	product=new Student();
            product.setStudentId(resultSet.getString(1));
            product.setStudentName(resultSet.getString(2));
            product.setStudent(resultSet.getString(3));
            product.setInfo(resultSet.getString(4));
            product.setInfo1(resultSet.getString(5));
            list.add(product);
        }
        this.preparedStatement.close();
        return list;
    }
    
    

    public Student findById(String studentId) throws Exception {

    	Student student = null;
        //String sql = "select student_id ,student_name,student,info FROM student WHERE student_id = ?";
    	//String sql = "select Sid,Sname,Password,Java,c FROM student WHERE Sid = ?";
    	String sql = "select Sid,Sname,Password,Java,c FROM student WHERE Sid = ?";
        this.preparedStatement = this.connection.prepareStatement(sql);
        this.preparedStatement.setString(1, studentId);
        ResultSet resultSet = this.preparedStatement.executeQuery();
        if (resultSet.next()) {
        	student = new Student();
        	student.setStudentId(resultSet.getString(1));
        	student.setStudentName(resultSet.getString(2));
        	student.setStudent(resultSet.getString(3));
        	student.setInfo(resultSet.getString(4));
        	student.setInfo1(resultSet.getString(5));//新增加的代码
        }
        this.preparedStatement.close();
        return student;
    }
    
    
    public Student findByNmae(String studentName) throws Exception {

    	Student student = null;
        //String sql = "select student_id ,student_name,student,info FROM student WHERE student_id = ?";
    	//String sql = "select Sid,Sname,Password,Java,c FROM student WHERE Sid = ?";
    	String sql = "select Sid,Sname,Password,Java,c FROM student WHERE Sname = ?";
        this.preparedStatement = this.connection.prepareStatement(sql);
        this.preparedStatement.setString(1, studentName);
        ResultSet resultSet = this.preparedStatement.executeQuery();
        if (resultSet.next()) {
        	student = new Student();
        	student.setStudentId(resultSet.getString(1));
        	student.setStudentName(resultSet.getString(2));
        	student.setStudent(resultSet.getString(3));
        	student.setInfo(resultSet.getString(4));
        	student.setInfo1(resultSet.getString(5));//新增加的代码
        }
        this.preparedStatement.close();
        return student;
    }

}

登陆验证
回复
wuxian2015 2015-11-21


package com.eshore.pojo;

import com.eshore.st.Student;
import com.eshore.factory.DAOFactory;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

import java.io.Serializable;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


public class User implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	private String username="";
	private String passwd="";
	Map<String,String>userMap=null;
	Map<String ,String>errorsMap=null;
	public User() throws Exception{
		super();
		this.username="";
		this.passwd="";
		userMap=new HashMap<String,String>();
		errorsMap=new HashMap<String,String>();
		
		/*
		List<Student> list=DAOFactory.getIPMLDAOInstrance().findAll("1");
		 for(int i=0;i<list.size();i++){
		        
		        Student p=list.get(i);
		       userMap.put(p.getStudentName(), p.getStudent());
		       
		 }
		*/
		
		List<Student> list=DAOFactory.getIPMLDAOInstrance().FindAll();
		 for(int i=0;i<list.size();i++){
		        
		        Student p=list.get(i);
		       userMap.put(p.getStudentName(), p.getStudent());
		       
		 }
		 
		 /*

		userMap.put("zs", "123");
		userMap.put("lisi", "123");
		userMap.put("wangwu", "123");
		userMap.put("zhaoqi", "123");
		userMap.put("zhengliu", "123");
		
		*/

				
		
	}
	public boolean isValidate(){
		
		boolean flag=true;
		if(!this.userMap.containsKey(this.username)){
			flag=false;
			errorsMap.put("username", "该用户不存在");
			this.username="";
			
		}
		String password=this.userMap.get(this.username);
		if(password==null||!password.equals(this.passwd)){
			flag=false;
			this.passwd="";
			errorsMap.put("passwd", "密码错诶,请输入正确密码");
			this.username="";
			
		}
		return flag;
		
	}

	public String getErrors(String key){
		String errorV=this.errorsMap.get(key);
		return errorV==null?"":errorV;
		
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPasswd() {
		return passwd;
	}
	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}
	
	
	

}


回复
heijava 2015-11-20
取list里面的值 用循环遍历 if 改成循环就行了
回复
wuxian2015 2015-11-20


<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%> 
<%@ page contentType="text/html;charset=utf-8"%> 
<% 
request.setCharacterEncoding("UTF-8"); 
response.setCharacterEncoding("UTF-8"); 
response.setContentType("text/html; charset=utf-8"); 
%> 
<html> 
<body> 
<% 
Connection con=null; 
String url="jdbc:mysql://local/testWeb?user=root&password=123456"; 
//testWeb为数据库名 
Class.forName("com.mysql.jdbc.Driver").newInstance();//新建实例 
Connection conn= DriverManager.getConnection(url);//建立连接 
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
String name=request.getParameter("name"); 
String sql = "select * from student where Sname=lj"; 
 ResultSet rs=stmt.executeQuery(sql); 
if(rs.next()) {%> 
ID:<%=rs.getString("Sname")%> </br> 
姓名:<%=rs.getString("Java")%></br> 
性别:<%=rs.getString("c")%></br> 
<%}%> 
<%out.print("恭喜你!数据库操作成功! ");%> 
<% 
rs.close(); 
stmt.close(); 
conn.close(); 
%> 
</br> 
<input name="" type="button" onclick="location.href = 'index_test.jsp'" value="返回" /> 
</body> 
</html> 

求大神看看这些代码哪里错了 org.apache.jasper.JasperException: An exception occurred processing JSP page /query_test.jsp at line 15 12: String url="jdbc:mysql://local/testWeb?user=root&password=123456"; 13: //testWeb涓烘暟鎹簱鍚� 14: Class.forName("com.mysql.jdbc.Driver").newInstance();//鏂板缓瀹炰緥 15: Connection conn= DriverManager.getConnection(url);//寤虹珛杩炴帴 16: Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 17: String name=request.getParameter("name"); 18: String sql = "select * from student where Sname=lj";
回复
sinat_31535993 2015-11-20
你又没有循环,当然只有一行了
回复
XxiaoxiaoL 2015-11-20
if (resultSet.next()) {,你只取了一条!
回复
比特灵 2015-11-20
if (resultSet.next()) { 改成: while (resultSet.next()) {
回复
张狂年少 2015-11-20
怎么会用表中的第二行数据登陆页面,显示出来的还是第一行数据的信息,你用户名怎么获取的,有获取到吗
回复
浮云疮狗 2015-11-20
用for循环遍历一下 就行了
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-11-20 12:55
社区公告
暂无公告