hi friends,
I am a novice java programmer.
I am in a learning process in java servlets.
But i struck into a Class method which should return a result set as an array.
I will show my code as some one can point out where am i going wrong. Currently im dealing with null value exception. Any one help me to get rid of that. Or suggest me a new (standard) method to access it. please treat me novice in java servlet
My servlet file in doGet()
protected void showAllUsers(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
try {
LoginEntryDaoImpl users = new LoginEntryDaoImpl();
List<UserInfo> allUsers = users.getAllUsers();
Iterator itr = allUsers.iterator();
while(itr.hasNext())
{
out.print("coming..................:"+itr.next());
out.println(itr.next().toString());
}
} catch (Exception e) {
out.print(e.getMessage());
} finally {
out.close();
}
}
My Dao Class
public class LoginEntryDaoImpl implements LoginEntryDao{
public void LoginUser(){
//constructor
}
public List<UserInfo> getAllUsers() {
Connection conn = null;
ResultSet rs = null;
Statement stmt=null;
List<UserInfo> users = new ArrayList<UserInfo>();
try {
LoginEntryConnection config=new LoginEntryConnection();
conn=config.getDbConnection();
stmt = conn.createStatement();
String query="SELECT id,user_name,dob from users";
rs = stmt.executeQuery(query);
while (rs.next()) {
UserInfo user = new UserInfo();
user.setId(rs.getInt("id"));
user.setName(rs.getString("user_name"));
user.setDob(rs.getString("dob"));
users.add(user);
}
}
catch(Exception e){
LoginEntryException eh=null;
eh.printErrorStatement(e);
}
finally {
if (rs != null) try { rs.close(); }
catch (SQLException e) {
LoginEntryException eh=null;
eh.printErrorStatement(e);
}
if (stmt != null) try { stmt.close(); }
catch (SQLException e) {
LoginEntryException eh=null;
eh.printErrorStatement(e);
}
if (conn != null) try { conn.close(); }catch (SQLException e)
{
LoginEntryException eh=null;
eh.printErrorStatement(e);
}
}
return users;
}//end of the method
}
My UserInFo class
public class UserInfo {
private int id;
private String name;
private String dob;
private String password;
public void setId(int id){
this.id=id;
}
public void setName(String name){
this.name=name;
}
public void setPassword(String password){
this.password=password;
}
public void setDob(String dob){
this.dob=dob;
}
public int getId(){
return this.id;
}
public String getName(){
return this.name;
}
public String getPassword(){
return this.password;
}
public String getDob(){
return this.dob;
}
}