4

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

4 Answers 4

9

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

<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,

<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>
Sign up to request clarification or add additional context in comments.

1 Comment

Doesn't mention the name of file to save the above code into.
4

All you need is the driver class and proper dialect. See 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

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

Comments

2

I am unable to find the jars.

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

and the hibernate.cfg.xml file for the same

The dialect for SQL Server 2005 is 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.

Comments

0

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.

In ApplicationContext.xml or applicationContext-resources.xml

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

In 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

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

Now just build your code and run on Apache server.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.