Spring数据源配置至dataSource
引入jar文件:commons-dbcp.jar、commons-pool.jar
Spring实例化org.apache.commons.dbcp.BasicDataSource类是,会调用到commons-dbcp.jar和commons-pool.jar文件
1 <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" 2 destroy-method="close"> 3 <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> 4 <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" /> 5 <property name="username" value="oracle" /> 6 <property name="password" value="oracle" /> 7 </bean>
dataSource获取Connection时,会调用数据库驱动包,本次采用的是oracle数据,所以要引入包ojdbc6.jar
1 try { 2 conn = dataSource.getConnection(); 3 statement = conn.createStatement(); 4 statement.executeUpdate("insert int book values(null,'离散数据','12.56')"); 5 } catch (SQLException e) { 6 e.printStackTrace(); 7 8 }finally{ 9 10 try { 11 statement.close(); 12 conn.close(); 13 } catch (SQLException e) { 14 // TODO Auto-generated catch block 15 e.printStackTrace(); 16 } 17 }