简体   繁体   English

使用休眠连接到SQL Server 2008 R2

[英]Connecting to SQL Server 2008 R2 with hibernate

I am trying to connect to a Microsoft SQL 2008 server through hibernate.it's not getting connected, following is hibernate.cfg.xml 我正在尝试通过hibernate连接到Microsoft SQL 2008服务器。它没有连接,下面是hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
  <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>    
<property name="hibernate.connection.password">1234</property> 
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=TEST</property>
<property name="hibernate.connection.username">username</property> 
<property name="hibernate.default_schema">dbo</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.show_sql">true</property>
</session-factory>
</hibernate-configuration>

` `

And here is the code I use to try and establish an connection and do a query : 这是我用来尝试建立连接并执行查询的代码:

package com.simpleprogrammer;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateUtilities {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

static {

try {
Configuration config = new Configuration().configure().addResource("hibernate.cfg.xml");
serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
sessionFactory = config.buildSessionFactory(serviceRegistry);
}catch(HibernateException he){
System.out.println("Problem Caught ! " + he );
}
}

public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}

` `

The Main method : package com.simpleprogrammer; Main方法:打包com.simpleprogrammer;

import org.hibernate.Session;

public class Program {
public static void main(String[] args) {
System.out.println("Hello World!") ;
Session session = HibernateUtilities.getSessionFactory().openSession();
session.close();
}

}

jar文件丢失了jboss-transaction-api_1.2_spec-1.0.0 ,只需将其添加到项目中即可开始工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM