简体   繁体   English

Spring 3 MVC和JDBC的结果集返回null

[英]Resultset of Spring 3 MVC and JDBC returns null

Hi im new to this Spring , MVC and JdBC support. 大家好,MVC和JdBC支持。 I want to be able to connect to a mysql database..but when i run my web it returns null. 我希望能够连接到mysql数据库..但是当我运行我的网站时,它返回null。 Below codes i believe should be easy,What am i missing here ? 我认为下面的代码应该很容易,我在这里错过了什么? Thanks for all replies 感谢所有回覆

Below is my error when try to query the URL 下面是我尝试查询URL时的错误

java.lang.NullPointerException
com.simple.myacc.dao.JdbcContactDao.findAll(JdbcContactDao.java:55)
com.simple.myacc.ContactController.getAll(ContactController.java:44)

My spring.xml 我的spring.xml

.....

<context:component-scan base-package="com.simple.myacc" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/jsp/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/webcontact" />
    <property name="username" value="root" />
    <property name="password" value="password" />
</bean>

<bean id="jdbcContactDao" class="com.simple.myacc.dao.JdbcContactDao">
    <property name="dataSource" ref="dataSource" />
</bean>

My JdbcContactDao 我的JdbcContactDao

public class JdbcContactDao {
protected static Logger logger = Logger.getLogger("service");
private DataSource dataSource;
private JdbcTemplate jdbcTemplate;

public JdbcContactDao() {

}

public List<Contact> findAll() {

    String sql = "select * from contact";
    List<Contact> contacts = new ArrayList<Contact>();
    List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
    for (Map rs : rows) {
        Contact contact = new Contact();
        contact.setId((Integer) rs.get("id"));
        contact.setFirstname((String) rs.get("firstname"));
        contact.setLastname((String) rs.get("lastname"));
        contact.setEmail((String) rs.get("email"));
        contact.setPhone((String) rs.get("phone"));
        contacts.add(contact);
    }
    return contacts;
}

@Resource(name = "dataSource")
public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;

} }

My controller , some part of it 我的控制器,部分

@RequestMapping(value="/contact/list2",method = RequestMethod.GET)
public String getAll(ModelMap model) {
    dao=new JdbcContactDao();
    List<Contact> contacts = dao.findAll();

     // Attach persons to the Model
     model.addAttribute("contacts", contacts);


     return "contact.list";

}

This is the line that says the NULL 这行说NULL

        List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);

A common idiom when using the JdbcTemplate class is to configure a DataSource in your Spring configuration file, and then dependency inject that shared DataSource bean into your DAO classes; 使用JdbcTemplate类时的一个常见习惯是在Spring配置文件中配置DataSource,然后将共享的DataSource bean依赖注入到DAO类中。 the JdbcTemplate is created in the setter for the DataSource. JdbcTemplate在数据源的设置器中创建。 private JdbcTemplate jdbcTemplate; 私人JdbcTemplate jdbcTemplate;

public void setDataSource(DataSource dataSource) {
    this.jdbcTemplate = new JdbcTemplate(dataSource);
}

You can read more on this here 你可以在这里阅读更多

Your code will look like this 您的代码将如下所示

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/webcontact" />
<property name="username" value="root" />
<property name="password" value="password" />

You don't need this 你不需要这个

<bean id="jdbcContactDao" class="com.simple.myacc.dao.JdbcContactDao">
<property name="dataSource" ref="dataSource" />

Instead do this 而是这样做

 @Autowired
public void setDataSource(DataSource dataSource) {
    this.jdbcTemplate = new JdbcTemplate(dataSource);
}

and annotate your JdbcContactDao class with @Repository I think that should work 并用@Repository注释您的JdbcContactDao类,我认为应该可以使用

I'm guessing line 55 of JdbcContactDao is this one List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql); 我猜JdbcContactDao的第55行是这个List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql); You declare jdbcTemplate, but never give it a value and it's also not annotated for injection, so it's always going to be null. 您声明了jdbcTemplate,但是从不给它一个值,并且也没有为注入做注释,因此它始终为null。 So, when you attempt to use it, you will get the NPE. 因此,当您尝试使用它时,您将获得NPE。

You have your dataSource and your JdbcContactDAO bean configured in the config file. 您已经在配置文件中配置了dataSource和JdbcContactDAO bean。 So in the same way you need to inject the jdbcContactDAO bean into your Controller. 因此,您需要以相同的方式将jdbcContactDAO bean注入到Controller中。

<bean id="myController" class="mypath.MyController">
<property name="dao" ref="jdbcContactDao"/>
</bean>

And in your controller.... 在您的控制器中...

public JdbcContactDao dao;

@Resource(name="dao")
public void setDao(JdbcContactDao dao){
    this.dao = dao;
}


@RequestMapping(value="/contact/list2",method = RequestMethod.GET)
public String getAll(ModelMap model) {

    List<Contact> contacts = dao.findAll();

    // Attach persons to the Model
    model.addAttribute("contacts", contacts);


    return "contact.list";

}

Had a similar problem was connecting to old tables using java/jdbc String sql = "select user_name from table" 曾经有一个类似的问题是使用java / jdbc连接到旧表字符串sql =“从表中选择user_name”

jdbc.queryForList(sql); 

queryReturnList = jdbc.queryForList(sql);  

    for (Map mp : queryReturnList){          
        String userName = (String)mp.get("user_name");          
}

userName was always null. userName始终为null。 looking through the map of returned values found that the map was not using user_name but a label set up on the table of "User Name" Had to have DBA's fix. 浏览返回值的映射表发现该映射表未使用user_name而是在“ User Name”表上设置的标签必须具有DBA的修复程序。 Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM