Spring 学习笔记(四)—— bean配置之数据库连接

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m940034240/article/details/84998197

本人使用的是SQL Server数据库,一个名为demo的数据库下有note和userT两个表,如下图:

两个表的内容如下图:

接下来在Bean.xml配置数据库的连接:

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
  >
  
	
	<!--SQL Server配置 使用jdbc,对应jar包:sqljdbc42.jar -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	   <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
	   <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=demo;"/>
	   <property name="username" value="你的用户名"/>
	   <property name="password" value="你的密码"/>
	</bean>
	
	<!--SQL Server配置 使用c3p0,对应jar包:c3p0-0.9.5.2.jar、 mchange-commons-java-0.2.11.jar -->
	<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<!-- 指定连接数据库的驱动 -->
		<property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
		<!-- 指定连接数据库的URL -->
		<property name="jdbcUrl" value="jdbc:sqlserver://localhost:1433;databaseName=demo;"/>
		<!-- 指定连接数据库的用户名 -->
		<property name="user" value="你的用户名"/>
		<!-- 指定连接数据库的密码 -->
		<property name="password" value="你的密码"/>
		<!-- 指定连接数据库连接池的最大连接数 -->
		<property name="maxPoolSize" value="200"/>
		<!-- 指定连接数据库连接池的最小连接数 -->
		<property name="minPoolSize" value="2"/>
		<!-- 指定连接数据库连接池的初始连接数 -->
		<property name="initialPoolSize" value="2"/>
		<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
		<property name="maxIdleTime" value="200"/>
	</bean>
</beans>

以上配置用到了3个jar包,如有需要,可到https://download.csdn.net/download/m940034240/10849605下载

接下来在代码中进行简单的查询

public class SQLServerQueryApp {		
   public static void main(String[] args) {
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");
      //使用jdbc
      JdbcTemplate jdbcTemplateObject = new JdbcTemplate((DataSource) context.getBean("dataSource")) ;
      String SQL = "select count(*) from dbo.[userT]";
      int rowCount = jdbcTemplateObject.queryForObject(SQL, Integer.class);
      System.out.println("userT表数据条数:"+rowCount);   
      
      //使用c3p0
      DataSource ds = context.getBean("dataSource2", DataSource.class);
      Connection conn;
		try {
			conn = ds.getConnection();
			 PreparedStatement pStatement = conn.prepareStatement("select * from note");
		      ResultSet resultSet =pStatement.executeQuery();
		      List<Map<String, Object>> list = new ArrayList<>();
				while (resultSet.next()) {
					Map<String, Object> map = new HashMap<>();
					ResultSetMetaData metaData = resultSet.getMetaData();
					int count = metaData.getColumnCount();
					for(int i = 1; i <= count; i++) {
						Object object = resultSet.getObject(i);
						map.put(metaData.getColumnLabel(i), object);
					}
					list.add(map);
				}
				System.out.println("查询结果:"+list.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
   }
}

运行输出结果:

userT表数据条数:24
查询结果:[{addTime=2018-04-18 09:59:55.0, name=1B281739B5BDC2D8118D27964688CB78_qq, editTime=null, title=2018-04-18的笔记, message=, audioPath=/storage/emulated/0/VoiceNote/1B281739B5BDC2D8118D27964688CB78_qq/2018_04_18_09_59_30/1524016795230.wav}, ........内容省略]

猜你喜欢

转载自blog.csdn.net/m940034240/article/details/84998197