JdbcTemplate.update不更新我的数据库

时间:2015-11-07 08:03:56

标签: java jsp spring-mvc

在我的.jsp文件中,我调用了一个假设使用JdbcTemplate.update更新我的数据库的对象。我使用的是Spring MVC框架,tomcat和mySql。谢谢你的帮助。

我的.jsp文件中的代码:

 <% FileDataDAO fileDataDAO = new FileDataDAO();

          fileDataDAO.test();

 %>

我的.java文件中的代码:

@Component("fileDataDao")
public class FileDataDAO {

private JdbcTemplate jdbc;
private DataSource dataSource;

public FileDataDAO() {}

@Autowired
public void setDataSource(DataSource dataSource) {

    this.dataSource = dataSource;
    jdbc = new JdbcTemplate(dataSource);

}
public void test(){

    String sql = "INSERT INTO myguests(id) VALUES(?)";
    jdbc.update(sql,5);
}
}

daoContext.xml文件

<?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"
   xmlns:jee="http://www.springframework.org/schema/jee"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/jee 
         http://www.springframework.org/schema/jee/spring-jee.xsd">


   <context:component-scan base-package="com.springapp.mvc.dao">
          <context:exclude-filter type="annotation" 
             expression="org.springframework.stereotype.Controller"/>
   </context:component-scan>

   <jee:jndi-lookup jndi-name="jdbc/TestDB" id="dataSource"
          expected-type="javax.sql.DataSource"/>


 </beans>

具体错误:

    org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/pages/home.jsp at line 54

    51: 
    52:           <% FileDataDAO fileDataDAO = new FileDataDAO();
    53: 
    54:     fileDataDAO.test();
    55: 
    56: %>
    57: 


    Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:172)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

1 个答案:

答案 0 :(得分:0)

您创建了 FileDataDAO fileDataDAO = new FileDataDAO(),但您使用的是Autowired。你需要从contexte(spring,CDI ...)

获取你的实例