刚开始接触hibernate,运行时eclipse提示:
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:97)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1354)
at StudentTest.main(StudentTest.java:19)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host local/hibernate, port 1433 has failed. Error: local/hibernate. Please verify the connection properties and check that a SQL Server instance is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:130)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1195)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(SQLServerConnection.java:1054)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:758)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:190)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
... 5 more
下面是hibernate.cfg.xml配置信息:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//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://local/hibernate</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.SQLServerDialect</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 打印sql语句 -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup 自动创建表结构-->
<!--<property name="hbm2ddl.auto">update</property> -->
<mapping resource="com/bjsxt/hibernate/model/student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
还望路过的好人帮帮忙,谢谢!
------解决思路----------------------
<property name="connection.url">jdbc:sqlserver://local/hibernate</property>
应该是
<property name="connection.url">jdbc:sqlserver://localhost:1433; DatabaseName=数据库名称</property>
------解决思路----------------------
他用的 hibernate3x
他代码写错 了 应该是这句
<property name="connection.url">jdbc:sqlserver://localhost:1433; DatabaseName=数据库名称</property>
hibernate 再牛逼也不能改底层的实现的。