Issue
I have tried some variations and i of course imported the class which contains the boolean method i meed in my Servlet, but it doesnt see the method anyway.
this is the method in my class logIN:
MyCon = Database.getConnection();
boolean result = false;
Statement statement = null;
ResultSet resultSet = null;
try {
String sql = "SELECT * FROM input_form WHERE username ='"+username+"'AND password='"+password+"'";
statement=MyCon.createStatement();
resultSet = statement.executeQuery(sql);
if (resultSet.next()) {
result=true;
}else {
result = false;
}
} catch (SQLException e) {
}
return result;
}
} ```
And when i need to access that method in servlet, it doesnt see it :
```protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
response.setContentType("text/html/charset-UTF-8");
out =response.getWriter();
boolean login = processLogin(username,password); //this is where it doesnt see it
```
what could i do for my servlet to see the method?
Solution
Create object
of class which you need to call and then use that object
to called required method.So your doPost method will look like below :
String username = request.getParameter("username");
String password = request.getParameter("password");
response.setContentType("text/html/charset-UTF-8");
out =response.getWriter();
Login l1 =new Login();//creating object of class
boolean login = l1.processLogin(username,password); //using object to call the method
//do further process
Also , use PreparedStatement
to select value in database and to avoid any SQL injection .So your method code will look like below :
PreparedStatement ps = MyCon.prepareStatement("select *from input_form where username=? AND password= ?");
//setting value for "?"
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
//if true
if (rs.next()) {
result=true;
}else{
result = false;
}
Answered By - Swati
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)