数据源配置方式总结
??? ??? <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>
??????? 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>
?