数据源配置方式总结
??? ??? <property name="url" value="${jdbc.url}" />
??? ??? <property name="username" value="${jdbc.username}" />
??? ??? <property name="password" value="${jdbc.password}" />
??? </bean>
??? <bean id="propertyConfigurer"
??? ??? ref="dataSource" />
??? </bean>
???
??? <tx:annotation-driven/>
</beans>
下面,我们来看一下DriverManagerDataSource的简单使用:当然,我们也可以通过配置的方式直接使用DriverManagerDataSource。
java 代码DriverManagerDataSource ds = new DriverManagerDataSource (); ds.setDriverClassName("com.mysql.jdbc.Driver"); ds.setUrl("jdbc:mysql://localhost:3309/sampledb"); ds.setUsername("root"); ds.setPassword("1234"); Connection actualCon = ds.getConnection(); 二、用JNDI 读取数据源server.xml 1、示例:1)mysql
<Resource name="jdbc/mysqlonline" type="javax.sql.DataSource"
??? driverClassName="com.mysql.jdbc.Driver"
??? password="123456"
??? maxIdle="30"
??? maxWait="10000"
??? username="root"
??? url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gb2312
??? maxActive="100" />
2)orcale
<Resource name="jdbc/wapcms"? type="javax.sql.DataSource"
??? driverClassName="oracle.jdbc.driver.OracleDriver"
??? password="123456"
??? maxIdle="30"
??? maxWait="10000"
??? username="root"
??? url="jdbc:oracle:thin:@localhost:1521:nquser"
??? maxActive="100"
??? removeAbandoned="true"
??? removeAbandonedTimeout="300"
??? logAbandoned="true"/>
3)sqlserver
<Resource name="jdbc/user"? type="javax.sql.DataSource"
??? driverClassName="net.sourceforge.jtds.jdbc.Driver"
??? password="sa"
??? maxIdle="30"
??? maxWait="10000"
??? username="sa"
??? url="jdbc:jtds:sqlserver://localhost:1433/test"
??? maxActive="100"
??? removeAbandoned="true"
??? removeAbandonedTimeout="300"
??? logAbandoned="true"/>
在<Host>下
??? <Context path="" docBase="E:/WAP/WAP_CMS/WebContent"
??? ??? debug="0" reloadable="true" privileged="true" crossContext="true"
??? ??? useNaming="true">
??? ??? <ResourceLink name="jdbc/wapcms" global="jdbc/wapcms" type="javax.sql.DataSource" />
??? ??? <ResourceLink name="jdbc/mysqlonline" global="jdbc/mysqlonline" type="javax.sql.DataSource" />
??? </Context>
</Host>
--web.xml
<resource-ref>
??? <description>DB Connection</description>
??? <res-ref-name>jdbc/wapcms</res-ref-name>
??? <res-type>javax.sql.DataSource</res-type>
??? <res-auth>Container</res-auth>
</resource-ref>
--Spring-application-data.xml
<bean id="dataSource" ref="dataSource" />
</bean>2、说明:通过jndi访问抽象的资源。这样程序不至于与访问的资源耦合。比如用jndi数据库密码变了与程序无关。不需要改代码
??????? debug="5" reloadable="true" crossContext="true">
?????????????? maxActive="100" maxIdle="30" maxWait="10000"
?????????????? username="sa" password="*******"
?????????????? driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
?????????????? url="jdbc:sqlserver://localhost:1433;databaseName=frum"/>
<%?????
??? DataSource ds = null;
??? try{
??? InitialContext ctx=new InitialContext();
??? ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");??
?? //java:comp/env/这是固定的 jdbc/mysql这是前面配置取的jndi名
??? Connection conn = ds.getConnection();
??? Statement stmt = conn.createStatement();???
??? String strSql = " select * from message";
<Context path="/lizi" docBase="E:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\lizi"
??????? debug="5" reloadable="true" crossContext="true">???? <Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"
?????????????? maxActive="100" maxIdle="30" maxWait="10000"
?????????????? username="sa" password="******"
?????????????? driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
?????????????? url="jdbc:sqlserver://localhost:1433;databaseName=frum"/></Context>三、用配置文件读取数据源信息1、示例---hibernate.cfg.xml
<hibernate-configuration>
??? <session-factory>
??????? <!-- Database connection settings -->
??????? <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
??????? <property name="connection.url">jdbc:oracle:thin:@localhost:1521:ora</property>
??????? <property name="connection.username">koooso</property>
??????? <property name="connection.password">koooso</property>
??????? <!-- JDBC connection pool (use the built-in) -->
??????? <property name="connection.pool_size">5</property>
??????? <!-- 注意如果是运行在application中时,必须要有current_session_context_class这个属性,且值为
?????? thread。如果是运行在WEB服务器中则需要将值设置成jta。否则在运行时会报Exception
?????? in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured! 这个异常-->
?????? <property name="current_session_context_class">jta</property>
??????? <!-- Echo all executed SQL to stdout -->
??????? <property name="show_sql">true</property>
??????? <!-- SQL dialect -->
??????? <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
??????? <!-- Drop and re-create the database schema on startup -->
??????? <property name="hbm2ddl.auto">create</property>
??????? <mapping resource="com/chinamworld/hibernate/tf/MyTest.hbm.xml"/>
??? </session-factory>
</hibernate-configuration>
?
---sqlserver Spring
<bean id="dataSource"
?? value="sa"></property>
?? <property name="password" value="admin"></property>
</bean>
<!-- 将Hibernate交由Spring管理(Hibernate相关配置信息) ,创建SessionFactory-->
<bean id="sessionFactory"
?? />
?? </property>
?? <property name="hibernateProperties">
??? <props>
???? <prop key="hibernate.dialect">
????? org.hibernate.dialect.SQLServerDialect
???? </prop>
???? <prop key="hibernate.show_sql">true</prop>
??? </props>
?? </property>
?? <property name="mappingResources">
??? <list>
???? <value>com/wuwei/struts/dao/User.hbm.xml</value>
??? </list>
?? </property>
</bean>2、xml 代码
/>
/>
/>
/>
/>
/>
/>
/>
>
>
<!--[if !supportLists]-->1.?? <!--[endif]--><property name="driverClassName">
<!--[if !supportLists]-->2.?? <!--[endif]--><value>oracle.jdbc.driver.OracleDrivervalue>
<!--[if !supportLists]-->3.?? <!--[endif]--></property>
<!--[if !supportLists]-->4.?? <!--[endif]--><property name="url">
<!--[if !supportLists]-->5.?? <!--[endif]--><value>jdbc:oracle:thin:@10.10.10.6:1521:DataBaseNamevalue>
<!--[if !supportLists]-->6.?? <!--[endif]--></property>
<!--[if !supportLists]-->7.?? <!--[endif]--><property name="username">
<!--[if !supportLists]-->8.?? <!--[endif]--><value>testAdminvalue>
<!--[if !supportLists]-->9.?? <!--[endif]--></property>
<!--[if !supportLists]-->10.? <!--[endif]--><property name="password">
<!--[if !supportLists]-->11.? <!--[endif]--><value>123456value>
<!--[if !supportLists]-->12.? <!--[endif]--></property>
<!--[if !supportLists]-->13.? <!--[endif]--></bean>
?