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

测试主类为:

  1. package myspring2;
  2.  
  3. import java.sql.*;
  4.  
  5. import javax.sql.DataSource;
  6.  
  7. import org.springframework.context.ApplicationContext;
  8.  
  9. import org.springframework.context.support.ClassPathXmlApplicationContext;
  10.  
  11. public class MySpringTest {
  12.  
  13. public static void main(String args[]) throws Exception{
  14.  
  15. ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
  16.  
  17. DataSource dataSource=ctx.getBean("dataSource",DataSource.class);
  18.  
  19. String sql="select * from user_inf";
  20.  
  21. Connection connection=dataSource.getConnection();
  22.  
  23. Statement stm=connection.createStatement();
  24.  
  25. ResultSet rs=stm.executeQuery(sql);
  26.  
  27. while(rs.next())
  28.  
  29. { System.out.println("用户名为:");
  30.  
  31. System.out.println(rs.getString(2));
  32.  
  33. }
  34.  
  35. }
  36.  
  37. }

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.  
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6.  
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8.  
  9. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.  
  11. xmlns:context="http://www.springframework.org/schema/context"
  12.  
  13. xmlns:p="http://www.springframework.org/schema/p"
  14.  
  15. xsi:schemaLocation="
  16.  
  17. http://www.springframework.org/schema/beans
  18.  
  19. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  20.  
  21. http://www.springframework.org/schema/tx
  22.  
  23. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  24.  
  25. http://www.springframework.org/schema/context
  26.  
  27. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  28.  
  29. http://www.springframework.org/schema/aop
  30.  
  31. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  32.  
  33. <!-- 使用XML Schema的p名称空间配置 -->
  34.  
  35. <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
  36.  
  37. p:driverClassName="com.mysql.jdbc.Driver"
  38.  
  39. p:url="jdbc:mysql://localhost:3306/test"
  40.  
  41. p:username="root"
  42.  
  43. p:password="123456" / >
  44.  
  45. <!-- 采用property的普通配置 相比之下有点麻烦,但是效果是一样的哦,-->
  46.  
  47. <!--
  48.  
  49. <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  50.  
  51. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  52.  
  53. <property name="url" value="jdbc:mysql://localhost:3306/test" />
  54.  
  55. <property name="username" value="root" />
  56.  
  57. <property name="password" value="123456" />
  58.  
  59. </bean>
  60.  
  61. -->
  62.  
  63. </beans>

第二种:C3P0数据源。

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

配置文件中如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.  
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6.  
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8.  
  9. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.  
  11. xmlns:context="http://www.springframework.org/schema/context"
  12.  
  13. xmlns:p="http://www.springframework.org/schema/p"
  14.  
  15. xsi:schemaLocation="
  16.  
  17. http://www.springframework.org/schema/beans
  18.  
  19. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  20.  
  21. http://www.springframework.org/schema/tx
  22.  
  23. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  24.  
  25. http://www.springframework.org/schema/context
  26.  
  27. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  28.  
  29. http://www.springframework.org/schema/aop
  30.  
  31. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  32.  
  33. <!-- 使用XML Schema的p名称空间配置 -->
  34.  
  35. <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
  36.  
  37. p:driverClass="com.mysql.jdbc.Driver"
  38.  
  39. p:jdbcUrl="jdbc:mysql://localhost:3306/test"
  40.  
  41. p:user="root"
  42.  
  43. p:password="123456" >
  44.  
  45. </bean>
  46.  
  47. <!-- 采用property的普通配置 相比之下有点麻烦,但是效果是一样的哦 建议使用上面的-->
  48.  
  49. <!-- <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  50.  
  51. <property name="driverClass" value="com.mysql.jdbc.Driver" />
  52.  
  53. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" />
  54.  
  55. <property name="user" value="root" />
  56.  
  57. <property name="password" value="123456" />
  58.  
  59. </bean>
  60.  
  61. -->
  62.  
  63. </beans>

第三种:

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

spring的配置文件中如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.  
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6.  
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8.  
  9. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  10.  
  11. xmlns:context="http://www.springframework.org/schema/context"
  12.  
  13. xmlns:p="http://www.springframework.org/schema/p"
  14.  
  15. xsi:schemaLocation="
  16.  
  17. http://www.springframework.org/schema/beans
  18.  
  19. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  20.  
  21. http://www.springframework.org/schema/tx
  22.  
  23. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  24.  
  25. http://www.springframework.org/schema/context
  26.  
  27. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  28.  
  29. http://www.springframework.org/schema/aop
  30.  
  31. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  32.  
  33. <!-- 使用XML Schema的p名称空间配置 -->
  34.  
  35. <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  36.  
  37. p:driverClassName="com.mysql.jdbc.Driver"
  38.  
  39. p:url="jdbc:mysql://localhost:3306/test"
  40.  
  41. p:username="root"
  42.  
  43. p:password="123456" >
  44.  
  45. </bean>
  46.  
  47. <!-- 采用property的普通配置 相比之下有点麻烦,但是效果是一样的哦 建议使用上面的-->
  48.  
  49. <!-- <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  50.  
  51. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  52.  
  53. <property name="url" value="jdbc:mysql://localhost:3306/test" />
  54.  
  55. <property name="username" value="root" />
  56.  
  57. <property name="password" value="123456" />
  58.  
  59. </bean>
  60.  
  61. -->
  62.  
  63. </beans>

第四种:

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

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

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

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

  1. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  2.  
  3. <property name="configLocations">
  4.  
  5. <list>
  6.  
  7. <value>classpath:com/config/hibernate.cfg.xml</value>
  8.  
  9. </list>
  10.  
  11. </property>
  12.  
  13. <property name="mappingLocations">
  14.  
  15. <!-- 所有的实体类映射文件 -->
  16.  
  17. <list>
  18.  
  19. <value>classpath:com/hibernate/*.hbm.xml</value>
  20.  
  21. </list>
  22.  
  23. </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. Java [Leetcode 328]Odd Even Linked List

    题目描述: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...

  2. 【DFS】NYOJ-325-zb的生日

    [题目链接:NYOJ-325] 一道以我名字命名的题目,难道要我生日的时候再A? 思路:依旧深搜,但这个问题应该有一个专有名词吧,看别的博客说是 “容量为 sum/2 的背包问题”,不懂... // ...

  3. Web开发人员必备工具-Emmet (Zen Coding)

    如果你从事前端开发或者web开发的话,一定听说过Zen coding - 一种快速编写HTML/CSS代码的方法.它使用仿CSS选择器的语法来快速开发HTML和CSS - 由Sergey Chikuy ...

  4. Java之UncaughtExceptionHandler

    概述: UncaughtExceptionHandler是为了捕获没有被捕获的异常,包括运行时异常,执行错误(内存溢出等),子线程抛出的异常等,你可以在uncaughtException(xx)里对后 ...

  5. Android-AnimationDrawable(一)

    大家平时见到的最多的可能就是Frame动画了,Android中当然也少不了它.它的使用更加简单,只需要创建一个 AnimationDrawabledF对象来表示Frame动画,然后通过addFrame ...

  6. tcprstat的使用方式

    两种使用方式:1)本机直接在线采集:2)分析tcpdump采集到的离线pcap文件   1. 本机直接在线采集 参数:   -p :指定只采集此TCP port的请求   -t  : 采集输出的时间间 ...

  7. HDU 5432 Pyramid Split

    题意:有n个底面是正方形的四棱锥,用一个水平截面将所有四棱锥分成两半,要求上一半体积的和等于下一半,求水平截面的高度,输出整数部分. 解法:二分截面高度.比赛的时候二分写不明白了orz…… 代码: # ...

  8. java多线程学习笔记——简单

    进程:程序(任务)的执行过程——动态性. 持有资源(共享内存,共享文件)和线程. 线程:线程是系统中最小的执行单元,统一进程中有多个线程,线程共享进程的资源. 线程交互:互斥与同步. 注意:多线程是异 ...

  9. Apache OFBiz 学习笔记 之 实体引擎

    1.概述     entity engine和常见的ORM有一点很大的不同,他的mapping object只有一个 GenericEntity,称它的entity engine 为adaptive ...

  10. C++类与对象

    [1]类的内存问题 类是抽象的,不占用内存,而对象是具体的,占用 存储空间.在一开始时弄清对象和类的关系是十分 重要的.[2]类的声明 如果在类的定义中既不指定private也不指定public,则系 ...