2011-12-01 174 views
5

我正在嘗試開發的JSF Web應用程序中連接到我的數據庫。 使用Windows 7,NetBeans 6.9.1,SQLServer 2008,Glassfish 3.3。無法連接到Java應用程序中的SQLServer數據庫

我使用sqljdbc4.jar司機,我從我的XP系統,我已經成功連接到SQLServer的2005年

我已驗證的SQLServer 2008我的系統上運行,並且我可以連接到複製該數據庫通過SQL Server Management Studio使用我嘗試在應用程序中使用的用戶名和密碼。 SQL Server配置管理器顯示IP連接端口是1433.所以我認爲我的數據庫設置正確。

我想我正在做的事情,但我只是無法連接到數據庫。 有沒有人有任何建議,爲什麼這是失敗的,我可以檢查什麼?

謝謝。

這裏是我的連接代碼:

public void connectDB() 
{ 
    try 
    { 
     String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=ROVRDEV;user=rovrdevuser;password=rovrdevuser"; 
     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
     Connection con = DriverManager.getConnection(connectionUrl); 
     System.out.println("Database connection = "+con); 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
     System.out.println("Error connecting to database. Error: "+e.getMessage()); 
    } 

} 

當我嘗試運行它,我得到的服務器日誌以下異常:

SEVERE: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, 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:170) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1049) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716) 
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841) 
    at java.sql.DriverManager.getConnection(DriverManager.java:582) 
    at java.sql.DriverManager.getConnection(DriverManager.java:207) 
    at rmsbackoffice.SessionBean1.connectDB(SessionBean1.java:130) 
    at rmsbackoffice.SessionBean1.doInit(SessionBean1.java:107) 
    at rmsbackoffice.SessionBean1.beforePhase(SessionBean1.java:88) 
    at rmsbackoffice.org$jboss$weld$bean-web-ManagedBean-class_rmsbackoffice$SessionBean1_$$_WeldClientProxy.beforePhase(org$jboss$weld$bean-web-ManagedBean-class_rmsbackoffice$SessionBean1_$$_WeldClientProxy.java) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at com.sun.el.parser.AstValue.invoke(AstValue.java:234) 
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297) 
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:43) 
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:56) 
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 
    at javax.faces.component.UIViewRoot.notifyPhaseListeners(UIViewRoot.java:1050) 
    at javax.faces.component.UIViewRoot.notifyBefore(UIViewRoot.java:845) 
    at javax.faces.component.UIViewRoot.encodeBegin(UIViewRoot.java:959) 
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1754) 
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:401) 
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131) 
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288) 
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594) 
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) 
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) 
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98) 
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162) 
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174) 
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828) 
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725) 
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019) 
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225) 
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) 
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) 
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71) 
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) 
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) 
    at java.lang.Thread.run(Thread.java:662) 

回答

8
The TCP/IP connection to the host localhost, 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. 

檢查你的SQL Server設置允許接受TCP/IP連接。啓用TCP/IP並重新啓動SQL Server。然後再次測試你的代碼。

enter image description here

enter image description here

+0

我該怎麼做? – Burferd

+1

就是這樣,我檢查了配置管理器中的其他條目,但網絡條目未啓用。謝謝。 – Burferd

1

在企業管理器去屬性 - >連接,並檢查 「允許遠程連接到這臺服務器。」還要確保Windows防火牆中的端口1433未被阻止。最後,我會停止使用Microsoft JDBC jar。 jTDS我運氣好多了。

1

我只是試圖改變主機名和IP

"jdbc:sqlserver://localhost:1433;databaseName=ROVRDEV;user=rovrdevuser;password=rovrdevuser"; 

成爲

"jdbc:sqlserver://127.0.0.1:1433;databaseName=ROVRDEV;user=rovrdevuser;password=rovrdevuser"; 

也許只是因爲這一個 - >

enter image description here

1

實例名中缺少您連接字符串。

如果你的SQL Server實例的名稱是 「SQL2005」,那麼它應該像.....

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;instanceName=SQL2005;databaseName=ROVRDEV","rovrdevuser","rovrdevuser"); 
0

也檢查SQL Server瀏覽器不 服務運行 - > SQL Server的瀏覽器 - >啓動

相關問題