当前位置: 代码迷 >> Java Web开发 >> hibernate4.3连接SQLServer2012,配置正常,但无法打开数据库,同时爆出连接错误
  详细解决方案

hibernate4.3连接SQLServer2012,配置正常,但无法打开数据库,同时爆出连接错误

热度:7499   发布时间:2016-04-10 22:48:33.0
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.setAge("test");

Configuration cfg = new Configuration().configure(); 
StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties());
StandardServiceRegistry sr = srb.build();
SessionFactory sf = cfg.buildSessionFactory(sr);
Session session = sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
}
}


异常代码为:



Exception in thread "main" org.hibernate.exception.SQLGrammarException: Error calling Driver#connect
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:118)
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:140)
at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:58)
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:75)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
  相关解决方案