Answer the question
In order to leave comments, you need to log in
Spring MVC + JDBC, can't connect?
There is an application on Spring MVC that connects to the database, takes data and pulls it out in html format.
I'm using JDK 8, Tomcat 8. I've tried many different options and haven't found a solution. Initially there was this error.
Servlet.init() for servlet dispatcherServlet threw exception
solved it by putting an SchoolDAOImpl
annotation over the class @Component
package com.springapp.mvc.controller;
import java.io.IOException;
import java.util.List;
import com.springapp.mvc.dao.SchoolDAO;
import com.springapp.mvc.model.School;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class MainController {
@Autowired
public SchoolDAO schoolDAO;
@RequestMapping(value="/", method = RequestMethod.GET )
public ModelAndView listSchools(ModelAndView model) throws IOException{
List<School> listSchools = schoolDAO.list();
model.addObject("listSchools", listSchools);
model.setViewName("home");
return model;
}
}
package com.springapp.mvc.dao;
import com.springapp.mvc.model.School;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Component
public class SchoolDAOImpl implements SchoolDAO {
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public List<School> list() {
String query = "select * from test.school";
List<School> listSchools = new ArrayList<School>();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
con = dataSource.getConnection();
ps = con.prepareStatement(query);
rs = ps.executeQuery();
while(rs.next()){
School aSchool = new School();
aSchool.setSchool__id(rs.getInt("school__id"));
aSchool.setUid(rs.getString("uid"));
aSchool.setGeom(rs.getString("geom"));
aSchool.setP8c8d9d76(rs.getString("p8c8d9d76"));
aSchool.setP1bbc78dc(rs.getInt("p1bbc78dc"));
listSchools.add(aSchool);
}
}catch(SQLException e){
e.printStackTrace();
}finally{
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return listSchools;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans [url]http://www.springframework.org/schema/beans/spring-beans.xsd[/url] [url]http://www.springframework.org/schema/context[/url] http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<context:component-scan base-package="com.springapp.mvc" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="schoolDAO" class="com.springapp.mvc.dao.SchoolDAOImpl">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/postgres" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
</beans>
<?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>
java.lang.NullPointerException
com.springapp.mvc.dao.SchoolDAOImpl.list(SchoolDAOImpl.java:31)
com.springapp.mvc.controller.MainController.listSchools(MainController.java:23)
// тут находится null
con = dataSource.getConnection();
// и тут
List<School> listSchools = schoolDAO.list();
Answer the question
In order to leave comments, you need to log in
Thanks everyone, problem solved.
I created a new project, added all the libraries through maven dependencies.
Most likely, the problem was that I added files of a different version to the spring jdbc jar project, and there was a different version in maven (and they may be in conflict).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question