第一种方式:.Spring常规的数据库连接方法:
1 @RunWith(SpringJUnit4ClassRunner.class) 2 @ContextConfiguration(locations="classpath:applicationContext.xml") 3 public class jdbcTemplateTest1 { 4 /* @Test 5 public void test1(){ 6 //1.创建数据库连接池(Spring) 7 DriverManagerDataSource dataSource = new DriverManagerDataSource(); 8 //2.设置参数 9 //获取驱动 10 dataSource.setDriverClass("com.mysql.jdbc.Driver"); 11 //连接数据库 12 dataSource.setJdbcUrl("jdbc:mysql:///springtest"); 13 dataSource.setUser("root"); 14 dataSource.setPassword("123"); 15 //3.创建jdbcTemplate 16 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); 17 jdbcTemplate.execute("update t_user set name='tom' where id=3"); 18 } */ 19 @Autowired 20 private JdbcTemplate jdbcTemplate; 21 @Test 22 public void test2(){ 23 jdbcTemplate.execute("update t_user set name='tom' where id=1"); 24 } 25 }