我試圖通過休眠連接到Microsoft SQL 2008服務器。 以下是我的hibernate.cfg.xml文件:將休眠連接到Microsoft SQL 2008服務器
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://127.0.0.1:1433;databaseName=myDBName;instanceName=myInstanceName;</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">pass</property>
<mapping resource="Obj.hbm.xml"/>
</session-factory>
</hibernate-configuration>
這裏是我用它來嘗試建立連接,並做了查詢代碼:
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class SessionsTest {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
/**
* @param args
*/
@SuppressWarnings({ "unchecked"})
public static void main(String[] args) {
sessionFactory = configureSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
List<Obj> result = (List<Obj>) session.createQuery("FROM Obj").list();
for (Obj obj : result) {
System.out.println(obj.getObjID());
}
session.getTransaction().commit();
session.close();
if (sessionFactory != null) {
sessionFactory.close();
}
}
private static SessionFactory configureSessionFactory() throws HibernateException {
Configuration config = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
sessionFactory = config.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
}
堆棧跟蹤我得到:
2013-04-13 15:02:03,449 [main] WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 08S01
2013-04-13 15:02:03,449 [main] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - The TCP/IP connection to the host 127.0.0.1, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Could not open connection
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:131)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:221)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1425)
at com.test.test.ObjTest.main(ObjTest.java:24)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 127.0.0.1, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:171)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1033)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:817)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:204)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:292)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214)
... 5 more
我試過使用不同的驅動程序(JTDS)。 我曾嘗試以各種方式更改URL字符串。 我嘗試將我的方言更改爲org.hibernate.dialect.SQLServerDialect。 我也試過在一個點上使用windows身份驗證,在URL字符串的末尾添加; IntegratedSecurity = true。 除此之外,我一直在服務器屬性中查找,以確保我提供的實例和端口是正確的。我嘗試過:telnet localhost 1433,無法連接,但可以使用SQL進行連接Server Management Studio。 進一步我使用NetStat -o在cmd和TaskList/FI「PID eq 4072」/ FO LIST/V來嘗試跟蹤sql server以確認端口aswell.奇怪的是我無法追查到sql server這它在NetStat列表中沒有出現,但如果我直接使用服務器的PID,它會顯示有關它的詳細信息,但狀態爲未知,會話號爲0,用戶名爲N/A。
我使用Hibernate 4.2.0和SQLJDBC4,當我使用JTDS時它是1.2.7。 java -version的輸出: java版本「1.6.0_30」 Java™SE運行時環境(版本1.6.0_30-b12) Java HotSpot™64位服務器虛擬機(版本20.5-b03,混合模式)
請告訴我是否需要其他信息,第一次發佈在這裏。
SQL工作室可以使用管道,而不是套接字。在你調整你的SQL服務器來監聽一個端口之前,沒有必要在Java中做任何事情。從這裏開始:http://stackoverflow.com/questions/1518823/how-to-find-the-port-for-ms-sql-server-2008 – kan