Código:
Mi clase ConnectionManager:package servlets; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import proyecto.GestorUsuario; import proyecto.Usuario; public class FrontController extends HttpServlet { private static final long serialVersionUID = 1L; String user=null; /* String passwd = "practica"; int numVeces = 0; */ GestorUsuario gu = new GestorUsuario(); public void init() throws ServletException { } protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } public void processRequest(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException { if (logado(request)) { String resource = request.getParameter("res"); RequestDispatcher d = request.getRequestDispatcher(resource); d.forward(request,response); System.out.println("LOGADO ACCEDIENDO A " + resource); } else { response.sendRedirect("error.html"); } } private boolean logado(HttpServletRequest request) { boolean logado = false; HttpSession session = request.getSession(false); String userForm = request.getParameter("user"); String passwdForm = request.getParameter("passwd"); if (session == null) { session = request.getSession(); if (userForm == null || passwdForm == null || userForm.length() == 0 || passwdForm.length() == 0) { logado = false; } else { if (valido(userForm, passwdForm)) { logado = true; session.setAttribute("session.user", userForm); } else { logado = false; } } } else { if (userForm == null || passwdForm == null) { logado = true; } else { if (valido(userForm, passwdForm)) { logado = true; session.setAttribute("session.user", userForm); } else { logado = false; } } } return logado; } public boolean valido(String userForm, String passwdForm) { boolean res =false; Usuario u =gu.mostrarUsuario(userForm); if (u!=null) res= (u.getNick().equals(userForm)&& u.getContraseña().equals(passwdForm)); return res; } }
Código:
Y por ultimo uno de los metodos que usan la BD, los demas los tengo todos hechos del mismo estilo:package data; import java.io.FileInputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; public class ConnectionManager { private static ConnectionManager cm; private Driver dBDriver = null; private static Properties dbprops = new Properties(); private ConnectionManager() { try { InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("dbconfiguration.properties"); dbprops.load(is); //dbprops.load(new FileInputStream("dbconfiguration.properties")); } catch (Exception e1) { System.err.println("El fichero de propiedades de la BDs no se ha encontrado"); e1.printStackTrace(); } try { String driverName = dbprops.getProperty("driverName"); dBDriver = (Driver) Class.forName(driverName).newInstance(); DriverManager.registerDriver(dBDriver); } catch (Exception e) { System.err.println("Unable to register JDBC Driver"); e.printStackTrace(); } } public static synchronized ConnectionManager getInstance() { if (cm == null) { cm = new ConnectionManager(); } return cm; } public Connection checkOut() { Connection conn = null; try { String uri = dbprops.getProperty("dBUri"); String user = dbprops.getProperty("username"); String passwd = dbprops.getProperty("password"); conn = DriverManager.getConnection(uri,user ,passwd); } catch (Exception e) { System.err.println("Unable to open a new JDBC connection"); e.printStackTrace(); } return conn; } public void checkIn(Connection conn) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } protected void finalize() { try { DriverManager.deregisterDriver(dBDriver); } catch (SQLException e) { System.err.println("Unable to deregister JDBC driver"); e.printStackTrace(); } } }
Código:
A ver si me podeis arrojar algo de luz porque ya no se por donde meterle mano :Spublic Usuario selectUsuario(String nick) { PreparedStatement stmt = null; ResultSet result = null; Usuario u = null; String sql = "SELECT * FROM Usuarios WHERE (OID = ?) "; String usuarioOID = this.selectOIDOfUsuario(conn, nick); try { stmt = conn.prepareStatement(sql); stmt.setString(1, usuarioOID); result = stmt.executeQuery(); if(result.next()){ u = new Usuario(); u.setNumSocio(result.getInt("numSocio")); u.setNick(result.getString("nick")); u.setContraseña(result.getString("contraseña")); u.setNombre(result.getString("nombre")); u.setApellidos(result.getString("apellidos")); u.setEmail(result.getString("email")); u.setSexo(result.getString("sexo")); u.setCiudad(result.getString("ciudad")); u.setAdministrador(result.getString("administrador")); u.setNumAciertosSemanaAnterior(result.getInt("numAciertos")); u.setNumVictorias(result.getInt("numVictorias")); } } catch (SQLException e) { System.out.println("Message: " + e.getMessage()); System.out.println("SQLState: " + e.getSQLState()); System.out.println("ErrorCode: " + e.getErrorCode()); } finally { try { if (result != null) { result.close(); } if (stmt != null) { stmt.close(); } } catch (SQLException e) { } } return u; }
Gracias de antemano.