@SpringJUnitConfig(locations = "classpath:beans.xml")
public class JdbcTemplateTest {
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* 添加
* 修改
* 删除
*/
@Test
public void testUpdate() {
//1、添加操作
//第一步,编写sql语句
String sql = "INSERT INTO t_emp VALUES (null,?,?,?)";
//第二部,调用jdbcTemplate的方法,传入相关参数
// Object[] params = {"慧慧", 16, "女"};
// int rows= jdbcTemplate.update(sql,params);
int rows= jdbcTemplate.update(sql,"慧慧", 16, "女");
System.out.println(rows);
//2、修改操作
/* String sql = "update t_emp set name=? where id=? ";
int rows= jdbcTemplate.update(sql,"爱蜜莉雅", 2);
System.out.println(rows);*/
//3、删除操作
/* String sql = "delete from t_emp where id=? ";
int rows = jdbcTemplate.update(sql, 3);
System.out.println(rows);*/
}
}
报错:
java.lang.NullPointerException: Cannot invoke "org.springframework.jdbc.core.JdbcTemplate.update(String, Object[])"
because "this.jdbcTemplate" is null
用这个试试【applicationContext.xml是 spring的配置文件】
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})