learn java
The moose likes JDBC and Relational Databases and the fly likes invalid column index Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "invalid column index" Watch "invalid column index" New topic
Author

invalid column index

krishna chadalawada
Greenhorn

Joined: Aug 03, 2012
Posts: 4
Hi All

i have login table , it contains ID, username, password columns. when i am trying to get username and pass using prepared statement, getting invalid column type.

please the code below


i tried with this also ps = con.prepareStatement("select * from login where username=? and password=?"); but no luck

could you please let me know to resolve this issue.

Thanks & Regards,
Krishna
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 19359
    
  10

Your PreparedStatement has two places for parameters. Their numbers are 1 and 2. Your code uses 2 and 3.
krishna chadalawada
Greenhorn

Joined: Aug 03, 2012
Posts: 4
thxs Dude..Now am able to connect database but it is not entering to while loop. could you please update me asap.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19959
    
  25

krishna chadalawada wrote:could you please update me asap.

Please EaseUp.

If your loop body is never executed that must mean that your query returns no results.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
krishna chadalawada
Greenhorn

Joined: Aug 03, 2012
Posts: 4
Thank you for your reply Rob Spoor

DAO class:

public class loginDAO {
private String userName;
private String passWord;

public loginDAO(String name, String pass){

this.userName = name;
this.passWord = pass;

}

public boolean validateUser() throws SQLException, ClassNotFoundException {
String url = "jdbcracle:thin:@kris-PC:1521rcl";
//int id = 0;
Connection con = null;
PreparedStatement stmt = null;
//Statement stmt = null;
//ResultSet rs = null;
boolean flag = false;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");

String query = "select username,password from loginuser where username = ? and password= ?"; //= ? and password = ?";
con = DriverManager.getConnection(url,"scott","tiger");
//stmt = con.createStatement();
stmt = con.prepareStatement(query);
stmt.setString(1, userName);
stmt.setString(2, passWord);

ResultSet rs = stmt.executeQuery();
if(rs.next() == true){
System.out.println("you are in true");
flag = true;
}else{
System.out.println("you are in false");
flag = false;
}



}catch(SQLException e){
System.out.println("++++++"+e.getMessage());

}

System.out.println(flag);

return flag;

}

}


Action class:

public class LoginAction extends Action {
/*
* Generated Methods
*/

/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws Exception
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {

LoginForm loginForm = (LoginForm)form;// TODO Auto-generated method stub


String userID = loginForm.getUserName();
String pass = loginForm.getPassWord();

loginDAO ld = new loginDAO(userID,pass);

boolean isLogin = ld.validateUser();

if(isLogin == true){
return mapping.findForward("sucess");

}else{
return mapping.findForward("failure");
}
}
}

please go through my code. i could not able to enter into if (rs.next())condition.
could you please let me know the reason and how can i enter into if loop?? plaese let me know asap


I want to do server side login validation so if you guys have any code please share with me


thanking you in advace.
krishna chadalawada
Greenhorn

Joined: Aug 03, 2012
Posts: 4
please go through my code. i could not able to enter into if (rs.next())condition in DAo Class
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 19359
    
  10

It's like Rob said. Your query returns zero records. If you want to produce a ResultSet for which rs.next() will return true (at least the first time you call it) then use a query which returns at least one record. In other words, not that query. Or perhaps that query with data which matches something in your database.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: invalid column index