Java 通过休眠连接到 MS sql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2497725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:28:40  来源:igfitidea点击:

Connecting to MS sql through hibernate

javasql-server-2005hibernateorm

提问by Rima

I want to connect to MS SQl server 2005 using hibernate in java. i am unable to find the jars and the hibernate.cfg.xml file for the same. can someone help me with the same

我想在 java 中使用休眠连接到 MS SQl 服务器 2005。我无法找到相同的 jars 和 hibernate.cfg.xml 文件。有人可以帮我做同样的事情吗

回答by Droo

All you need is the driver class and proper dialect. See http://msdn.microsoft.com/en-us/library/ms378749.aspx

您所需要的只是驱动程序类和正确的方言。请参阅http://msdn.microsoft.com/en-us/library/ms378749.aspx

If you have the driver, then (at a minimum) you need to specify the connection properties: http://www.roseindia.net/hibernate/firstexample.shtml

如果您有驱动程序,那么(至少)您需要指定连接属性:http: //www.roseindia.net/hibernate/firstexample.shtml

The proper dialect appears to be: org.hibernate.dialect.SQLServerDialect

正确的方言似乎是: org.hibernate.dialect.SQLServerDialect

回答by Pascal Thivent

I am unable to find the jars.

我找不到罐子了。

Get a JDBC driver for SQL Server 2005 from Microsoftor use the open source alternative jTDS.

Microsoft获取 SQL Server 2005 的 JDBC 驱动程序或使用开源替代jTDS

and the hibernate.cfg.xml file for the same

和 hibernate.cfg.xml 文件相同

The dialect for SQL Server 2005 is org.hibernate.dialect.SQLServerDialect.

SQL Server 2005 的方言是org.hibernate.dialect.SQLServerDialect.

The other params (like the driver class name, the jdbc URL) will depend on the driver you choose. Refer to the respective documentation.

其他参数(如驱动程序类名、jdbc URL)将取决于您选择的驱动程序。请参阅相应的文档。

回答by Shashi

As mentioned by Pascal Thivent, use any one driver. In case of JTDS, use the following configuration.

正如Pascal Thivent所提到的,使用任何一个驱动程序。如果是 JTDS,请使用以下配置。

<hibernate-configuration>
<session-factory>
    <property name="connection.url">jdbc:jtds:sqlserver://XX.XX.XXX.XX:YYYY/DB-NAME</property>
    <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
    <property name="connection.username">username</property>
    <property name="connection.password">password</property>
    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
. 
.
.
</session-factory>
</hibernate-configuration>

And in case of Microsoft SQL JDBC Driver,

如果是 Microsoft SQL JDBC 驱动程序,

<hibernate-configuration>
<session-factory>
    <property name="connection.url">jdbc:microsoft:sqlserver://XX.XX.XXX.XX:YYYY/DB-NAME</property>
    <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="connection.username">username</property>
    <property name="connection.password">password</property>
    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
. 
.
.
</session-factory>
</hibernate-configuration>

回答by Anksss

I also faced and after lot of tries i found solution and its working fine for me

我也遇到过,经过多次尝试,我找到了解决方案,并且对我来说效果很好

You can create connection using JNDI connection string also.

您也可以使用 JNDI 连接字符串创建连接。

In ApplicationContext.xml or applicationContext-resources.xml

在 ApplicationContext.xml 或 applicationContext-resources.xml 中

<jee:jndi-lookup id="dataSource" lookup-on-startup="true" resource-ref="true"  jndi-name="jdbc/resourcename"/>

In Apache Context.xml

在 Apache Context.xml 中

<Resource name="jdbc/resourcename" auth="Container" type="javax.sql.DataSource"
    username=username password=password driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
    url="jdbc:sqlserver://localhost:1433;databaseName=dbname />

Add hibernate dialect in persistence.xml or hibernate.cfg.xml

在persistence.xml 或hibernate.cfg.xml 中添加hibernate 方言

<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />

Now just build your code and run on Apache server.

现在只需构建您的代码并在 Apache 服务器上运行。