简体   繁体   English

通过休眠连接到MS SQL

[英]Connecting to MS sql through hibernate

I want to connect to MS SQl server 2005 using hibernate in java. 我想使用Java中的休眠模式连接到MS SQl服务器2005。 i am unable to find the jars and the hibernate.cfg.xml file for the same. 我找不到相同的jar和hibernate.cfg.xml文件。 can someone help me with the same 有人可以帮我吗

As mentioned by Pascal Thivent , use any one driver. Pascal Thivent所述 ,请使用任何一种驱动程序。 In case of JTDS, use the following configuration. 对于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>

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

I am unable to find the jars. 我找不到罐子。

Get a JDBC driver for SQL Server 2005 from Microsoft or 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. 其他参数(如驱动程序类名称,jdbc URL)将取决于您选择的驱动程序。 Refer to the respective documentation. 请参阅相应的文档。

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中添加休眠方言

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

Now just build your code and run on Apache server. 现在,只需构建代码并在Apache服务器上运行即可。

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

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