<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.rimi</groupId> <artifactId>SpringDBCP</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringDBCP Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> </dependencies> <build> <finalName>SpringDBCP</finalName> </build> </project>
<?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:jdbc="http://www.springframework.org/schema/jdbc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://127.0.0.1:3306/j1702?useSSL=false"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> <property name="initialSize" value="10"></property> <property name="maxActive" value="100"></property> <property name="minIdle" value="10"></property> <property name="maxIdle" value="50"></property> <property name="defaultAutoCommit" value="false"></property> <property name="maxWait" value="1000"></property> <property name="timeBetweenEvictionRunsMillis" value="3600000"></property> <property name="minEvictableIdleTimeMillis" value="3600000"></property> </bean> </beans>
import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.apache.commons.dbcp.BasicDataSource; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.sun.jndi.ldap.Connection; public class App { public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:ApplicationConnext.xml"); BasicDataSource ds=(BasicDataSource) ac.getBean("dataSource"); java.sql.Connection conn = null; String sql="select * from user where name='admin'"; PreparedStatement ps = null; ResultSet rSet=null; try { conn = ds.getConnection(); ps =conn.prepareStatement(sql); rSet=ps.executeQuery(); while(rSet.next()){ System.out.println("name:"+rSet.getString("name")); } } catch (SQLException e) { e.printStackTrace(); } } }