2

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 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

sorry, but... how do i do that?
WEB-INF directory is root directory for your application, with libraries, configuration files and classes. All libraries should you put in WEB-INF/lib directory. You should download PostgreSQL jdbc driver and put it in your lib directory.
1

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!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.