这是MySQL的配置文件:
<hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">zgy01</property> <property name="hibernate.connection.pool_size">100</property> <property name="show_sql">false</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Mapping files --> <mapping resource="model.hbm.xml"/> </session-factory> </hibernate-configuration>
为SQL Server 2005指定什么?我这样做是这样的:
<hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property> <property name="hibernate.connection.url">com.microsoft.sqlserver.jdbc.SQLServerDriver</property> <property name="hibernate.connection.username">sa</property> <property name="hibernate.connection.password">lal</property> <property name="dialect">org.hibernate.dialect.SQLServerDialect</property> <property name="hibernate.connection.pool_size">100</property> <property name="show_sql">false</property> <!-- Mapping files --> <mapping resource="model.hbm.xml"/> </session-factory> </hibernate-configuration>
我的问题更确切地说是如何指定必须连接的数据库?
在MySQL中,我曾经这样做:
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
特定于数据库的 属性是:
hibernate.connection.driver_class
hibernate.connection.url
hibernate.connection.username
hibernate.connection.password
hibernate.dialect
org.hibernate.dialect.Dialect
要更改数据库,您必须:
Dialect
有两个驱动程序可以连接到SQL Server。开源jTDS和Microsoft。驱动程序类和JDBC URL取决于您使用哪一种。
驱动程序类名称为net.sourceforge.jtds.jdbc.Driver。
net.sourceforge.jtds.jdbc.Driver
sqlserver的URL格式为:
jdbc:jtds:sqlserver://<server>[:<port>][/<database>][;<property>=<value>[;...]]
因此,Hibernate配置如下所示(请注意,您可以跳过hibernate.属性中的前缀):
hibernate.
<hibernate-configuration> <session-factory> <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property> <property name="connection.url">jdbc:jtds:sqlserver://<server>[:<port>][/<database>]</property> <property name="connection.username">sa</property> <property name="connection.password">lal</property> <property name="dialect">org.hibernate.dialect.SQLServerDialect</property> ... </session-factory> </hibernate-configuration>
驱动程序类名称为com.microsoft.sqlserver.jdbc.SQLServerDriver。
com.microsoft.sqlserver.jdbc.SQLServerDriver
URL格式为:
jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
因此,Hibernate配置如下所示:
<hibernate-configuration> <session-factory> <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property> <property name="connection.url">jdbc:sqlserver://[serverName[\instanceName][:portNumber]];databaseName=<databaseName></property> <property name="connection.username">sa</property> <property name="connection.password">lal</property> <property name="dialect">org.hibernate.dialect.SQLServerDialect</property> ... </session-factory> </hibernate-configuration>