M
M
Maks Burkov2016-11-15 14:45:58
Java
Maks Burkov, 2016-11-15 14:45:58

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); // Это строка работает!
        }
    }
}

Tomcat folder:
43de203e0a964769be1a6a24e2b0dcd9.png
Jariki that I deploy.
7611da299b274190ab884838c433037a.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2016-11-15
@Maks00088

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 question

Ask a Question

731 491 924 answers to any question