Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

ThreadLocal DB connection

843838Jul 22 2006 — edited Jul 23 2006
Hi All, My problem is simple I think, So I will post all my classes. I coldn`t connect the database with these code. Where is the my problem. It says to me " user not found."

This is my User class
public class User {

    public String validate(String username, String password) {

    	PreparedStatement stmt = null;
        ResultSet result = null;
        String stmtSQL;
        String id = null;
            try {
            	Connection conn = ConnectionManager.getInstance().getConnnection();         // maybe problem here????
                stmtSQL = "SELECT id FROM login WHERE username = ? AND password = ?";
                stmt = conn.prepareStatement(stmtSQL);
                
                stmt.setString(1, username);
                stmt.setString(2, password);
                
                result = stmt.executeQuery();
                if (result.next()) {
                    id = result.getString("id");
                }
                stmt.close();
                conn.close();
                conn = null;
            } catch (Exception e) {
                e.printStackTrace();
            } 
        return id;
    }
}
this is controller
public class LoginController extends HttpServlet {

    //**doPost����\�
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
    	//buralarda bir yerde connection open yapmam lazim.
    	 try {
    		ConnectionManager.open(); // I think my problem at this point but how will I solve it?
		} catch (SQLException e) {
			e.printStackTrace();
		} // public ConnectionManager(){ hoshii   keredemo dou suru ka??			
		    	
        response.setContentType("text/html;charset=UTF-8"); 
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        try {
            //look at the User class and inside the database
            String loginId;
            if ((loginId = new User().validate(username, password)) != null) {

                request.setAttribute("id", loginId);
                request.setAttribute("username", username);

                getServletContext().getRequestDispatcher("/MainController").forward(request, response);
            } else {
                response.sendRedirect("/swallow/");
            }
        } catch (Throwable t) {
            System.out.println("User not found");
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    } //doGet
}
and this is connection manager class
public final class ConnectionManager {
	private static ConnectionManager instance = new ConnectionManager();
	private Properties prop;
	//private ThreadLocal<Connection> connections = new ThreadLocal<Connection>();
	private static ThreadLocal<Connection> connections = new ThreadLocal<Connection>();

	private ConnectionManager(){
		//public ConnectionManager(){
		ClassLoader cl = instance.getClass().getClassLoader();
		InputStream is = cl.getResourceAsStream("swallowJDBC.properties");
		prop = new Properties();
		try {
			prop.load(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
		public static ConnectionManager getInstance() throws IOException {		
		return instance;
	}
		//public void open() throws SQLException {
		 public static void open() throws SQLException {  // I think most probably my problem happen  here but where is it ????
		Connection conn = null; // open connection for this thread.
		 try {
		 String driver = ((Properties) conn).getProperty("swallow.jdbc.driver");
		 String url = ((Properties) conn).getProperty("swallow.jdbc.url");
		 String user = ((Properties) conn).getProperty("swallow.jdbc.user");
		 String password = ((Properties) conn).getProperty("swallow.jdbc.password");
		 Class.forName(driver);
		 //return DriverManager.getConnection(url, user, password);
		 conn = DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		connections.set(conn);
	}

	public Connection getConnnection() {
		
		return connections.get();
	}

	public void commit() throws SQLException {
		Connection conn = getConnnection();
		if (conn != null) {
			conn.commit();
			conn.close();
		}
	}
	 public void rollback() throws SQLException {
		 Connection conn = getConnnection();
	        if (conn != null) {
	            conn.rollback();
	            conn.close();
	        }
	    }
	
}
i am sorry I sent all codes... but I know you can undestand on my mistakes easly... please help...
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 20 2006
Added on Jul 22 2006
1 comment
213 views