本文简单的讲解使用Spring连接数据库的几种常用方法:

测试主类为:

package myspring2;

import java.sql.*;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MySpringTest {

  public static void main(String args[]) throws Exception{  

    ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");   

   DataSource dataSource=ctx.getBean("dataSource",DataSource.class);

     String sql="select * from user_inf";    

  Connection connection=dataSource.getConnection();  

    Statement stm=connection.createStatement();    

  ResultSet rs=stm.executeQuery(sql);  

    while(rs.next())     

{       System.out.println("用户名为:");  

     System.out.println(rs.getString(2));  

    }                   

}

}

第一种:使用spring自带的DriverManagerDataSource   配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>  

<beans xmlns="http://www.springframework.org/schema/beans"

  xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"  

xmlns:p="http://www.springframework.org/schema/p"

  xsi:schemaLocation=" 

          http://www.springframework.org/schema/beans     

       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

           http://www.springframework.org/schema/tx       

     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

           http://www.springframework.org/schema/context

           http://www.springframework.org/schema/context/spring-context-3.0.xsd

           http://www.springframework.org/schema/aop

           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

 <!-- 使用XML Schema的p名称空间配置 -->

  <bean name="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource"  

  p:driverClassName="com.mysql.jdbc.Driver"  

  p:url="jdbc:mysql://localhost:3306/test"

  p:username="root"

  p:password="123456"  / >  

  <!-- 采用property的普通配置 相比之下有点麻烦,但是效果是一样的哦,-->

<!--     

  <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  

    <property name="driverClassName"  value="com.mysql.jdbc.Driver" />

     <property name="url" value="jdbc:mysql://localhost:3306/test" />

     <property name="username" value="root" />

     <property name="password" value="123456" />

    </bean>

    -->       

</beans>

第二种:C3P0数据源。

需要使c3p0的核心jar包,我使用的是c3p0-0.9.1.jar,比较稳定,推荐使用。一般在下载hibernate的时候都会自带一个: 我在hibernate-release-4.3.0.Final\lib\optional\c3p0路径下找到的。

配置文件中如下:

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans"

  xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context" 

xmlns:p="http://www.springframework.org/schema/p"

  xsi:schemaLocation="

          http://www.springframework.org/schema/beans    

       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

            http://www.springframework.org/schema/tx      

     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

           http://www.springframework.org/schema/context

            http://www.springframework.org/schema/context/spring-context-3.0.xsd

           http://www.springframework.org/schema/aop

            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

  <!-- 使用XML Schema的p名称空间配置   -->

<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  

   p:driverClass="com.mysql.jdbc.Driver"  

   p:jdbcUrl="jdbc:mysql://localhost:3306/test"

   p:user="root"

   p:password="123456" >       

</bean>    

<!-- 采用property的普通配置 相比之下有点麻烦,但是效果是一样的哦 建议使用上面的-->

<!--       <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  

            <property name="driverClass"  value="com.mysql.jdbc.Driver" />    

            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" />

            <property name="user" value="root" />

            <property name="password" value="123456" />

            </bean>

  -->    

  </beans>

第三种:

使用apache的dbcp插件连接数据库 需要下载的jar包:commons-dbcp.jar,commons-pool.jar,commons-collection.jar

spring的配置文件中如下:

<?xml version="1.0" encoding="UTF-8"?>  

<beans xmlns="http://www.springframework.org/schema/beans"  

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

  xmlns:p="http://www.springframework.org/schema/p"  

xsi:schemaLocation="        

   http://www.springframework.org/schema/beans  

     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    

        http://www.springframework.org/schema/tx     

       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    

        http://www.springframework.org/schema/context  

          http://www.springframework.org/schema/context/spring-context-3.0.xsd

           http://www.springframework.org/schema/aop  

          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

 <!-- 使用XML Schema的p名称空间配置 -->

   <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource"

  p:driverClassName="com.mysql.jdbc.Driver"  

p:url="jdbc:mysql://localhost:3306/test"

  p:username="root"

  p:password="123456" >  

</bean>

    <!-- 采用property的普通配置 相比之下有点麻烦,但是效果是一样的哦 建议使用上面的-->

<!--       <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  

    <property name="driverClassName"  value="com.mysql.jdbc.Driver" />    

  <property name="url" value="jdbc:mysql://localhost:3306/test" />

     <property name="username" value="root" />  

    <property name="password" value="123456" />  

    </bean>

   -->    

  </beans>

第四种:

使用hibernate数据源   需要hiberante核心jar包,我使用的hibernate1的版本是hibernate-release-4.3.0.Final

目前三大框架较流行,spring一般与hiberante做搭档,数据库连接方式写在hiberante的配置文件中,在spring管理hibernate中的配置文件

中,直接读取hibernate核心配置文件即可。在使用hibernate连接数据库的时候需要读取hibernate.cfg.xml的配置文件和相应的实体类,

读者可参照下面的自己配置一下

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 

 <property name="configLocations"> 

   <list> 

      <value>classpath:com/config/hibernate.cfg.xml</value> 

   </list> 

 </property> 

    <property name="mappingLocations">  

<!-- 所有的实体类映射文件 --> 

        <list> 

            <value>classpath:com/hibernate/*.hbm.xml</value> 

        </list> 

</property>

Spring连接数据库的几种常用的方式的更多相关文章

  1. jQuery中ajax的4种常用请求方式

    jQuery中ajax的4种常用请求方式: 1.$.ajax()返回其创建的 XMLHttpRequest 对象. $.ajax() 只有一个参数:参数 key/value 对象,包含各配置及回调函数 ...

  2. 深入浅出spring IOC中三种依赖注入方式

    深入浅出spring IOC中三种依赖注入方式 spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,控制反转一般分为两种类型,依赖注入和 ...

  3. 转:深入浅出spring IOC中四种依赖注入方式

    转:https://blog.csdn.net/u010800201/article/details/72674420 深入浅出spring IOC中四种依赖注入方式 PS:前三种是我转载的,第四种是 ...

  4. SoapUI 的几种常用参数化方式

    今天给大家来梳理下soapui这款工具关于参数化的几种方式以及具体的应用场景 1.properties 官方文档:https://www.soapui.org/docs/functional-test ...

  5. 跨平台C++开源代码的两种常用编译方式

    作者:朱金灿 来源:http://blog.csdn.net/clever101 跨平台C++开源代码为适应各种编译器的编译,采用了两种方式方面来适配.一种是makefile方式.以著名的空间数据格式 ...

  6. 实例化Spring容器的两种常用方式

    //在类路径下寻找配置文件来实例化容器 ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"b ...

  7. Spring下配置几种常用连接池

    1.连接池概述 数据库连接是一种关键的有限的昂贵的资源,这一点在多用户的网页应用程序中体现得尤为突出.对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标.数据库连接池正是 ...

  8. 【SSH系列】深入浅出spring IOC中三种依赖注入方式

    spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,控制反转一般分为两种类型,依赖注入和依赖查找,依赖什么?为什么需要依赖?注入什么?控 ...

  9. spring IOC中三种依赖注入方式

    Spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则,用来消减计算机程序之间的耦合问题,控制反转一般分为两种类型,依赖注入和依赖查找,依赖什么?为什么需要依赖?注入 ...

随机推荐

  1. UVA 10098 Generating Fast, Sorted Permutation

    // 给你字符串 按字典序输出所有排列// 要是每个字母都不同 可以直接dfs ^_^// 用前面说的生成排列算法 也可以直接 stl next_permutation #include <io ...

  2. 常用UI布局

    1.LinearLayout(线性布局):将布局所包含的控件在线性方向上依次排列. <1>android:orientation 指定了排列方向(垂直方向(vertical).水平方向(h ...

  3. mssql的delete用用到被delete的表的别名

    +' delete m from '+@strDBName +'.dbo.m_device as m where not exists ' +' (select 1 from @tmpDevice w ...

  4. C++ 函数重载与函数匹配

    <C++ Primer>笔记,整理关于函数重载与函数匹配的笔记. 函数重载 void func(int a); //原函数 void func(double a); //正确:形参类型不同 ...

  5. JTA事务管理--配置剖析(二)

    Spring引用Tomcat的 JTA事务     Tomcat是Servlet容器,但它提供了JNDI的实现,因此用户可以象在Java EE应用程序服务器中一样,在Tomcat中使用JNDI查找JD ...

  6. JAX-WS

    JAX-WS(Java API for XML Web Services)规范是一组XML web services的JAVA API,JAX-WS允许开发者可以选择RPC-oriented或者mes ...

  7. 《Python 学习手册4th》 第十一章 赋值、表达式和打印

    ''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书) “重点 ...

  8. A Blind Watermarking for 3-D Dynamic Mesh Model Using Distribution of Temporal Wavelet Coefficients

    这周看了一篇动态网格序列水印的论文,由于目前在网格序列上做水印的工作特别少,加之我所看的这篇论文中的叙述相对简洁,理解起来颇为困难.好在请教了博士师兄,思路明朗了许多,也就把这思路整理在此了. 论文作 ...

  9. 新建虚拟目录使用UNC共享文件夹(即:虚拟目录使用UNC共享文件夹)的方法 -摘自网络

    新建虚拟目录使用UNC共享文件夹(即:虚拟目录使用UNC共享文件夹)的方法1.UNC路径:\\192.168.1.2\test\,假设连接该UNC路径的用户名为:web,密码为:123 2.在原web ...

  10. struts2+Hibernate4+spring3+EasyUI环境搭建之三:引入sututs2以及spring与sututs2整合

    1.引入struts2 <!-- struts2 和心包 排除javassist 因为hibernate也有 会发生冲突--> <dependency> <groupId ...