我正在尝试通过hibernate连接到Microsoft SQL 2008服务器。 以下是我的hibernate.cfg.xml文件:
<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。 我还尝试通过在url字符串的末尾添加; IntegratedSecurity = true来一次使用Windows身份验证。 除此之外,我一直在服务器属性中寻找,以确保我提供的实例是正确的以及port.I已经尝试过:telnet localhost 1433并且无法以这种方式连接,但我可以使用SQL Server进行连接管理工作室。 此外,我在cmd和TaskList / FI“PID eq 4072”/ FO LIST / V中使用NetStat -o来尝试跟踪sql server以确认端口以及。奇怪的是我无法通过这种方式跟踪sql server 。它没有出现在NetStat列表中,但如果我直接使用服务器的PID,它会显示有关它的详细信息,除了状态为未知,会话号为0且用户名为N / A.
我使用Hibernate 4.2.0和SQLJDBC4,当我使用JTDS时它是1.2.7。 java -version的输出: java版“1.6.0_30” Java(TM)SE运行时环境(版本1.6.0_30-b12) Java HotSpot(TM)64位服务器VM(内置20.5-b03,混合模式)
如果需要任何其他信息,请告诉我,第一次发布在这里。
答案 0 :(得分:7)
我认为您的SQL Server实例在1433上没有为TCP连接提供服务。 识别端口:
答案 1 :(得分:7)
真的,真的,确实检查SQL Server实例中是否启用了TCP / IP协议。
按照以下步骤(SS2012测试)确保:
<YourInstance>
的协议”如果您有任何问题,请查看this blog post了解详细信息,因为它包含屏幕截图和更多信息。
同时检查“SQL Server Browser”窗口服务是否已激活并正在运行:
就是这样。
安装新的本地SQL Server后,我所要做的就是启用TCP / IP并启动SQL Server Browser服务。
在我用来测试与SQLEXPRESS
本地实例的连接的代码下面。当然,您应该根据需要更改IP,DatabaseName和用户/密码。:
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JtdsSqlExpressInstanceConnect {
public static void main(String[] args) throws SQLException {
Connection conn = null;
ResultSet rs = null;
String url = "jdbc:jtds:sqlserver://127.0.0.1;instance=SQLEXPRESS;DatabaseName=master";
String driver = "net.sourceforge.jtds.jdbc.Driver";
String userName = "user";
String password = "password";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, userName, password);
System.out.println("Connected to the database!!! Getting table list...");
DatabaseMetaData dbm = conn.getMetaData();
rs = dbm.getTables(null, null, "%", new String[] { "TABLE" });
while (rs.next()) { System.out.println(rs.getString("TABLE_NAME")); }
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
rs.close();
}
}
}
如果您使用Maven,请将其添加到您的pom.xml:
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.2.4</version>
</dependency>