日期:2014-05-19  浏览次数:20875 次

dao层出现空指针异常--初学者求助
我用spring jdbc模板进行增删改查,测试时总是出现空指针异常,查询总是jdbcTemplate.update/query()语句出错。请大家帮帮忙

原文件如下:

【StudentDaoImp.java】
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bbs.dao.StudentDao;
import com.bbs.model.Student;

@Repository
@SuppressWarnings("unchecked")
public class StudentDaoImp implements StudentDao {
protected JdbcTemplate jdbcTemplate;
protected void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}

@Transactional(rollbackFor=Exception.class,propagation=Propagation.REQUIRED)
public boolean deleteStudents(int id) {
String sql = "delete from students where id =" + id;
int rows = jdbcTemplate.update(sql);
if(rows == 1)
return true;
else 
return false;
}

public List<Student> getAllStudents() {
String sql = "select * from students";
List<Student> studentsList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Student>(Student.class));
return studentsList;
}

public Student getStudentsById(int id) {
String sql = "select * from students where id =" + id;
List<Student> students = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Student>(Student.class));
return students.get(0);
}

public boolean updateStudents(Student student) {
String sql = "update students set name=?, sex=?, age=?, phone=? where id=? ";
int rows = jdbcTemplate.update(sql, student.getName(), student.getSex(), student.getAge(),
student.getPhone(), student.getId());
if(rows == 1)
return true;
else 
return false;
}

public boolean addStudents(Student student) {
String sql = "insert into students(name, sex, age, phone) values(?, ?, ?, ?)";
int rows = jdbcTemplate.update(sql, student.getName(), student.getSex(), student.getAge(), student.getPhone());
if(rows == 1)
return true;
else 
return false;
}

}


【bean.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
   
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="com.