sqlserver版本为2012,使用的jar包sqljdbc4-4.0.jar c3p0-0.9.1.2.jar
找jar及pom配置: http://mvnrepository.com/
maven pom.xml配置
<dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>sqljdbc4</artifactId> <version>4.0</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency>
applicationContext.xml配置
<!-- 创建连接池 配置连接池的属性值 --> <bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property> <property name="jdbcUrl" value="jdbc:sqlserver://xxx:3433;databaseName=xxx"></property> <property name="user" value="xxx"></property> <property name="password" value="xxx"></property> </bean> <bean id="jdbcTemplateRenyun" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="ds" /> </bean> <bean id="cpicRenyunService" class="com.autoyolConsole.service.cpic.CpicRenyunService"> <property name="jdbcTemplateRenyun" ref="jdbcTemplateRenyun" /> </bean>
junit测试类
/** * */ package com.autoyolConsole.service.cpic; import java.sql.Connection; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import com.mchange.v2.c3p0.ComboPooledDataSource; /** * @author xx.xx * @function sqlserver数据库查询操作类 * @date 2017年11月6日 * @version */ @Service public class CpicRenyunService { private JdbcTemplate jdbcTemplateRenyun; @Autowired @Qualifier("ds") private ComboPooledDataSource ds; public JdbcTemplate getJdbcTemplateRenyun() { return jdbcTemplateRenyun; } public void setJdbcTemplateRenyun(JdbcTemplate jdbcTemplateRenyun) { this.jdbcTemplateRenyun = jdbcTemplateRenyun; } public void queryAutoInfor005ById(String id){ try { System.out.println("DS="+ds); java.sql.DatabaseMetaData md = ds.getConnection().getMetaData(); System.out.println("s11="+md.getDriverName()); System.out.println("s21="+md.getURL()); System.out.println("s31="+md.getUserName()); boolean f = jdbcTemplateRenyun.getDataSource().getConnection().isClosed(); Connection conn = jdbcTemplateRenyun.getDataSource().getConnection(); String s1= conn.getMetaData().getDriverName(); String s2=conn.getMetaData().getURL(); String s3=conn.getMetaData().getUserName(); System.out.println("f="+f); System.out.println("s1="+s1); System.out.println("s2="+s2); System.out.println("s3="+s3); Map<String,Object> map = jdbcTemplateRenyun.queryForMap("select * from auto_infor_005 where auto_inf05_id=1372"); System.out.println("查询返回结果:" + map); } catch (Exception e) { e.printStackTrace(); } } }