R
R
random2015-01-18 13:33:29
Java
random, 2015-01-18 13:33:29

SpringMVC + JDBC?

I have a web application using Spring MVC that takes data from a database (postgresql) and outputs it in HTML format.
It displays errors:
1)HTTP Status 500 - Servlet.init() for servlet dispatcher threw an exception(The server encountered an internal error that prevented it from fulfilling this request.)
After poking around in the logs, I found another error
2) java.sql.SQLException : No suitable driver found for jdbc:postgresql://localhost:5432/postgres
PS: I don't use JdbcTemplate on purpose
1) ContactDAO

public interface ContactDAO {

  public ArrayList<Contact> getContacts();
}

2)ContactDAOImpl
public class ContactDAOImpl implements ContactDAO {

  private static Connection con = null;

  public ContactDAOImpl(String dbUrl, String user, String pass) throws SQLException {
    con = DriverManager.getConnection(dbUrl, user, pass);
  }

  @Override
  public ArrayList<Contact> getContacts() {
    ArrayList<Contact> list = null;
    try {
      list = new ArrayList<Contact>();
      Statement st = con.createStatement();
      ResultSet rs = st.executeQuery("SELECT * FROM contact");
      while (rs.next()) {
        Contact aContact = new Contact();
        aContact.setId(rs.getInt("contact_id"));
        aContact.setName(rs.getString("name"));
        aContact.setEmail(rs.getString("email"));
        aContact.setAddress(rs.getString("address"));
        aContact.setTelephone(rs.getString("telephone"));
        list.add(aContact);
      }
      rs.close();
      st.close();
    } catch (SQLException ex) {
      ex.printStackTrace();
    }
    return list;
  }
}

3)HomeController
@Controller
public class HomeController {

  @Autowired
  private ContactDAO contactDAO;

  @RequestMapping(value="/")
  public ModelAndView listContact(ModelAndView model) throws IOException{
    ArrayList<Contact> contacts = contactDAO.getContacts();
    model.addObject("listContact", contacts);
    model.setViewName("home");
    
    return model;
  }
}

4)JavaConfig
@Configuration
@ComponentScan(basePackages="com.springapp.mvc")
@EnableWebMvc
public class MVCConfiguration extends WebMvcConfigurerAdapter{
  private static String dbURL = "jdbc:postgresql://localhost:5432/postgres";
  private static String username = "postgres";
  private static String password = "borntorun";

  @Bean
  public ViewResolver getViewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
  }
  
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
  }

  @Bean
  public void regDriver() {
    try {
      Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }

  @Bean
  public ContactDAO getContactDAO() throws SQLException {
    return new ContactDAOImpl(dbURL, username, password);
  }
}

5)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <display-name>SpringMVC</display-name>
  <context-param>
    <param-name>contextClass</param-name>
    <param-value>
      org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextClass</param-name>
      <param-value>
        org.springframework.web.context.support.AnnotationConfigWebApplicationContext
      </param-value>
    </init-param>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>com.springapp.mvc</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>
</web-app>

6)pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.springapp</groupId>
    <artifactId>SpringMVC</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SpringMVC</name>

    <properties>
        <spring.version>4.1.4.RELEASE</spring.version>
        <cglib.version>2.2.2</cglib.version>
    </properties>
    <dependencies>
        <!-- Spring core & mvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <!-- CGLib for @Configuration -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>${cglib.version}</version>
            <scope>runtime</scope>
        </dependency>


        <!-- Servlet Spec -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>SpringMVC</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
asd111, 2015-01-18
@asd111

Most likely the cant is somewhere in pom.xml, tk. something I didn’t notice there jar nicknames for the JDBC postgres driver
Look at what it gives Class.forName("org.postgresql.Driver");if it gives a class not found exception, it means the jar has not connected.
example of including postgres jar:

<dependency>
    <groupId>postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>VERSION</version>
</dependency>

or for version 9.2+
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>VERSION</version>
</dependency>

you may also need to register <scope>provided</scope>if you have Tomcat and put the driver jar in the lib Tomcata folder

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question