Guest User

Untitled

a guest
Aug 18th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. <!-- Spring provides several options for making a DataSource available to your application.
  2. (1) Getting a DataSource from JNDI -->
  3.  
  4. <bean id=”dataSource”
  5. class=”org.springframework.jndi.JndiObjectFactoryBean“>
  6. <property name=”jndiName”>
  7. <value>java:comp/env/jdbc/myDatasource</value>
  8. </property>
  9. </bean>
  10.  
  11. <!-- OR -->
  12.  
  13. <jee:jndi-lookup id=”dataSource” jndi-name=”/jdbc/RantzDatasource” resource-ref=”true” />
  14.  
  15.  
  16. <!-- We have now wired in our server’s DataSource and its connection pooling facility
  17.  
  18. (2) Creating a DataSource connection pool -->
  19.  
  20. <bean id=“myDataSource” class=“com.mchange.v2.c3p0.ComboPooledDataSource” destroy-method=“close”>
  21. <property name=“driverClass” value=“net.sourceforge.jtds.jdbc.Driver” />
  22. <property name=“jdbcUrl” value=“jdbc:jtds:sqlserver://R2DDEV2:1433/sampleDB” />
  23. <property name=“user” value=“sa” />
  24. <property name=“password” value=“dev123″ />
  25. </bean>
  26.  
  27. <!-- We now have a DataSource with connection pooling independent of an application server.
  28.  
  29. (3) Using a DataSource while testing
  30. DriverManagerDataSource. This class can easily be configured and used with a unit test or suite of unit tests.-->
  31.  
  32. DriverManagerDataSource dataSource = new DriverManagerDataSource();
  33.  
  34. dataSource.setDriverClassName(driver);
  35.  
  36. dataSource.setUrl(url);
  37.  
  38. dataSource.setUsername(username);
  39.  
  40. dataSource.setPassword(password);
  41.  
  42.  
  43. <!-- OR -->
  44.  
  45. <bean id=”dataSource” class=”org.springframework.jdbc.datasource.DriverManagerDataSource“>
  46. <property name=”driverClassName” value=” net.sourceforge.jtds.jdbc.Driver” />
  47. <property name=”url” value=” jdbc:jtds:sqlserver://R2DDEV2:1433/sampleDB” />
  48. <property name=”username” value=”sa” />
  49. <property name=”password” value=”" />
  50. </bean>
  51.  
  52. <!-- You now have a DataSource to use when testing your data access code.
  53. Using JdbcTemplate
  54.  
  55. JdbcTemplate template = new JdbcTemplate(myDataSource);
  56.  
  57. All of Spring’s DAO template classes are thread-safe, we only need one JdbcTemplate instance for each DataSource in our application -->
  58.  
  59. <bean id=”jdbcTemplate” class=”org.springframework.jdbc.core.JdbcTemplate”>
  60. <property name=”dataSource“><ref bean=”dataSource”/></property>
  61. </bean>
  62. <bean id=”studentDao” class=”StudentDaoJdbc”>
  63. <property name=”jdbcTemplate“><ref bean=”jdbcTemplate“/></property>
  64. </bean>
  65. <bean id=”courseDao” class=”CourseDaoJdbc”>
  66.  
  67. <property name=”jdbcTemplate“><ref bean=”jdbcTemplate“/></property>
Add Comment
Please, Sign In to add comment