java – How to connect PostgreSQL with Spring Mvc

java – How to connect PostgreSQL with Spring Mvc

You need to configure your database connection. You can create a classe to configure this. For example:

@Configuration
public class DatabaseConfig {

@Bean
public DriverManagerDataSource getDataSource() {

    DriverManagerDataSource bds = new DriverManagerDataSource();
    bds.setDriverClassName(org.postgresql.Drive);
    bds.setUrl(jdbc:mysql://localhost:5432/dbname);
    bds.setUsername(user);
    bds.setPassword(pass);

    return bds;
}

}

And then, you can inject the datasource in your bens.

@Controller
public class MyController {

   @Autowired
  private DataSource dataSource;
}

Try to use Spring boot with spring data. Is very simple to configure a database connection and create repositories for data manipulations (CRUD). See this https://dzone.com/articles/spring-boot-with-spring-data-jpa

java – How to connect PostgreSQL with Spring Mvc

Leave a Reply

Your email address will not be published. Required fields are marked *