日期:2014-05-17  浏览次数:20737 次

hibernate4.3连接SQLServer2012,配置正常,但无法打开数据库,同时爆出连接异常
使用hibernate4.3连接SQLServer2012,所有的配置都正常,但是出现无法打开数据库,同时爆出连接错误。
hibernate.cfg.xml配置:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=student"</property>
    <property name="connection.username">sa</property>
    <property name="connection.password">123456</property>

    <!-- JDBC connection pool (use the built-in) -->
   <!--  <property name="connection.pool_size">1</property>-->
    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.SQLServer2012Dialect</property>
<property name="javax.persistence.validation.mode">none</property> 
    <!-- Enable Hibernate's automatic session context management -->
    <!-- <property name="current_session_context_class">thread</property> -->

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">update</property>
    <mapping resource="com/test/hibernate/model/Student.hbm.xml"/>
  </session-factory>
</hibernate-configuration>


映射文件student.hbm.xml:


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.test.hibernate.model">

    <class name="Student" table="studentinfo">
    
     <id name="id"></id>
     <property name="name"/>
     <property name="age"/>
</class>
</hibernate-mapping>


测试类test.java中关键代码:


public class test {
public static void main(String[] args) {
Student s = new Student();
s.setId(1);
s.setName("test");
s.set