2
votes

I'm trying to connect to a postgresql DB from a java aplication developed on Spring.

The odd thing is that if I ran it as a java aplication, it works fine.

When you try to run it on the server it trows an exception

java.lang.ClassNotFoundException: org.postgresql.Driver at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1157) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at com.CBTD.database.BaseDeDatos.establecerConexion(BaseDeDatos.java:31) ....

Is there any incompatibility between Spring (STS) and postgresql?

The controller code:

@RequestMapping(value = "/inicio", method = RequestMethod.GET)
public String ObtenerUsuario(@RequestParam(required=true) String user, @RequestParam(required=true) String pass){

    LoginExpert login = new LoginExpert();
    if (login.validarUsuario(user, pass)){
        return "inicio";
    }else{
        return "login";
    }

}

An intermediate class

public boolean validarUsuario(String user, String pass){

    TablaUsuario tu = new TablaUsuario();
    Usuario us = new Usuario();
    try {
        us = tu.buscarUsuario(user);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return (us.getContrasena().equals(pass));

}

The class where I access the DB (this part running as a java app works fine)

public Usuario buscarUsuario(String nombre) throws SQLException{

    String sql = "SELECT * from usuario WHERE nombre_usuario = '"+nombre+"'";

    BaseDeDatos.getInstancia().establecerConexion();
    ResultSet rs = BaseDeDatos.getInstancia().ejecutarQuery(sql);

    Usuario us= new Usuario();
    while(rs.next()){
        us.setNombreUsuario(rs.getObject(2).toString());
        us.setContrasena(rs.getObject(3).toString());
        us.setActivo(rs.getBoolean(4));
    }


    BaseDeDatos.getInstancia().cerrarConexion();

    return us;

}
2

2 Answers

2
votes

Try to put porstgresql driver in WEB-INF/lib folder

1
votes

i alraedy fixed it. i needed to add the postgresql dependency in the pom.xml file

<dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901-1.jdbc4</version>
    </dependency>

now it works perfectly fine =) thanks fpr the help!!