1. 程式人生 > >java.sql.SQLException: 游標位置無效問題

java.sql.SQLException: 游標位置無效問題

錯誤程式碼如下,執行出現java.sql.SQLException: 游標位置無效的問題,原因是沒有next()方法

/**
	 * 根據id獲得使用者資訊
	 * @param id
	 * @return 返回查詢資訊
	 */
	public User queryOne(String id){
		String sql = "select id,username,password,balance,phone,gender,idcard,registTime from t_user where id=?";
		User user = null;
		try {
			CachedRowSet crs = DBUtils.executesQuery(sql,id);
				user = new User(
					crs.getLong("id"),
					crs.getString("username"), 
					crs.getString("password"),
					crs.getBigDecimal("balance"), 
					crs.getString("phone"), 
					crs.getString("gender"), 
					crs.getString("idcard"), 
					crs.getDate("registtime"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

加入next()方法,錯誤解決

/**
	 * 根據id獲得使用者資訊
	 * @param id
	 * @return 返回查詢資訊
	 */
	public User queryOne(String id){
		String sql = "select id,username,password,balance,phone,gender,idcard,registTime from t_user where id=?";
		User user = null;
		try {
			CachedRowSet crs = DBUtils.executesQuery(sql,id);
			if(crs.next()){
				user = new User(
						crs.getLong("id"),
						crs.getString("username"), 
						crs.getString("password"),
						crs.getBigDecimal("balance"), 
						crs.getString("phone"), 
						crs.getString("gender"), 
						crs.getString("idcard"), 
						crs.getDate("registtime"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}