Spring中使用JDBC

首先要获得DataSource连接池:要对数据库执行任何的JDBC操作,需要有一个Connection.在Spring中,Connection对象是通过DataSource获得的。

有几种方法可以得到DataSource, 其中一种方法是使用Spring提供的轻量级org.springframework.jdbc.datasource.DriverManagerDataSource,香港虚拟主机,第二种方法是使用org.apache.commons.dbcp.BasicDataSource类。

一:使用DriverMangerDataSource,这种方法是轻量级的,方便测试

DataSoureProvider {DriverManagerDataSource dataSource = new DriverManagerDataSource(); DriverManagerDataSource getInstance() { 5dataSource.setDriverClassName(“com.mysql.jdbc.Driver”); 6dataSource.setUrl(“jdbc:mysql://localhost:3306/book”); 7dataSource.setUsername(“y****”); 8dataSource.setPassword(“h*******”); 9return dataSource;10 }11 12 @Test test() {14 DataSoureProvider.getInstance();15try {16 dataSource.getConnection();17 }18catch (SQLException e) {19 e.printStackTrace();20 }21 }22 },网站空间,网站空间你可以这样理解 impossible(不可能)–I'm possible (我是可能的)。

Spring中使用JDBC

相关文章:

你感兴趣的文章:

标签云: