Answer the question
In order to leave comments, you need to log in
Am I using try with resources correctly or what is the problem here?
After validation, the request comes here but does not enter the Connection block, who can tell what is the problem?
I don't get any errors about the connection problem, I don't know what to look for .. Any advice would be appreciated!!!
public class LoginService {
private static ConnectionPool connectionPool = ConnectionPool.con_instance;
private static Connection con = null;
public static boolean checkUser(TreeMap<String, String> sortedData, HttpServletResponse response) throws CouponSystemException {
String name = sortedData.get("nickname");
String password = sortedData.get("password");
String email = sortedData.get("email");
System.out.println(name + ": " + password + ": " + email + " Login service"); // Печатает! После чего заходит в finally и всё!
try (Connection con = connectionPool.getConnection()){ // В сам блок как будто не заходит.
System.out.println("Before Pre"); // No input
String queryDataBase = "SELECT nick_name,password,email FROM users WHERE nick_name = ? AND password = ? AND email = ?";
PreparedStatement preparedStatement = con.prepareStatement(queryDataBase);
preparedStatement.setString(1, sortedData.get("nickname"));
preparedStatement.setString(2, sortedData.get("password"));
preparedStatement.setString(3, sortedData.get("email"));
ResultSet resultSetQuery = preparedStatement.executeQuery();
System.out.println("After Pre"); // No input
while (resultSetQuery.next()) {
if (resultSetQuery.getString("nick_name").equals(sortedData.get("nickname"))) {
if (resultSetQuery.getString("password").equals(sortedData.get("password"))) {
if (resultSetQuery.getString("email").equals(sortedData.get("email"))) {
try {
ResponseDataHandler.ToJsonResponse("success", response);
System.out.println("Success");
} catch (IOException io) {
throw new CouponSystemException(io.getMessage());
}
return true;
}
}
} else if (resultSetQuery.getString("nick_name").equals(null)) {
return false;
}
}
return false;
} catch (SQLException e) {
throw new CouponSystemException(e.getMessage());
}finally {
connectionPool.returnConnection(con); // Это строка работает!
}
}
}
Answer the question
In order to leave comments, you need to log in
It may not enter if Connection con = connectionPool.getConnection() throws an exception.
Try to debug and/or add some System.out.println("Inside catch block!");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question