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

流烟 2014-03-09 09:34:34
使用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)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
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.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at test.main(test.java:20)

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 无法打开登录所请求的数据库 "student""。登录失败。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:246)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:83)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2529)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:1905)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:1893)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1045)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:817)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:55)
... 13 more



在Exception in thread "main" org.hibernate.exception.SQLGrammarException错误中的最后一句里,输出at test.main(test.java:20);

点击查看是SessionFactory sf = cfg.buildSessionFactory(sr);难道说这错了嘛,但是这配置没问题,都是按照教程做的。

最后一个错误是Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 无法打开登录所请求的数据库 "student""。登录失败。

不知是啥问题,数据库中有此数据库,并且书写格式正确
麻烦大神指点一二,感激不尽!
...全文
434 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Qu_Li 2015-04-21
  • 打赏
  • 举报
回复
楼主是什么问题?
流烟 2014-03-09
  • 打赏
  • 举报
回复
引用 1 楼 lihbsd 的回复:
<property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=student"</property> 这个双引号的问题
问题已经全部解决了,多谢多谢,拷贝数据库地址的时候大意了
流烟 2014-03-09
  • 打赏
  • 举报
回复
引用 1 楼 lihbsd 的回复:
<property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=student"</property> 这个双引号的问题
改正过来了,但是又出新的问题:Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 不允许从数据类型 nvarchar 到 varbinary 的隐式转换。请使用 CONVERT 函数来运行此查询。
lihbsd 2014-03-09
  • 打赏
  • 举报
回复
<property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=student"</property> 这个双引号的问题

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧