开发者

Connecting to MS SQL Server 2005 via Hibernate

I have JRE 1.6 and am using the following hibernate.cfg.xml file. I am always getting "Cannot open connection" and "The port number 1433/DB is not valid."

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="hibernate.connection.url">jdbc:sqlserver://IP/DB</property>
    <property name="hibernate.connection.username"></property>
    <property name="hibernate.connection.password"></property>
    <property name="hibernate.connection.pool_size">10</property>
    <property name="show_sql">true</property>
    <property name="dialect">org.hibernat开发者_Go百科e.dialect.SQLServerDialect</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
  </session-factory>
</hibernate-configuration>


From the official documentation:

Building the Connection URL

The general form of the connection URL is

jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]

where:

  • jdbc:sqlserver:// (Required) is known as the sub-protocol and is constant.
  • serverName (Optional) is the address of the server to connect to. This could be a DNS or IP address, or it could be localhost or 127.0.0.1 for the local computer. If not specified in the connection URL, the server name must be specified in the properties collection.
  • instanceName (Optional) is the instance to connect to on serverName. If not specified, a connection to the default instance is made.
  • portNumber (Optional) is the port to connect to on serverName. The default is 1433. If you are using the default, you do not have to specify the port, nor its preceding ':', in the URL.
  • property (Optional) is one or more option connection properties. For more information, see Setting the Connection Properties. Any property from the list can be specified. Properties can only be delimited by using the semicolon (';'), and they cannot be duplicated.

So use the following instead:

jdbc:sqlserver://IP;databaseName=DB


You are connecting to host/instance. It should be a backslash: host\instance. Are you mixing up the concepts of instance and databases?

host\instance;databaseName=DB

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜