java,不与sql server连接
我有两台电脑,它们有相同的窗口,相同的java版本,所有的东西都一样,但一台电脑的代码是有效的,另一台电脑的代码是无效的。 我不知道是代码的错误还是我在设置一台电脑时做错了什么。 我需要你帮我看看密码是否正确java,不与sql server连接,java,sql-server,eclipse,Java,Sql Server,Eclipse,我有两台电脑,它们有相同的窗口,相同的java版本,所有的东西都一样,但一台电脑的代码是有效的,另一台电脑的代码是无效的。 我不知道是代码的错误还是我在设置一台电脑时做错了什么。 我需要你帮我看看密码是否正确 try { txtBDUrl.setEnabled(false); txtPassword.setEnabled(false); txtUser.setEnabled(false); if(txtBDUrl.
try
{
txtBDUrl.setEnabled(false);
txtPassword.setEnabled(false);
txtUser.setEnabled(false);
if(txtBDUrl.getText().length() > 0)
{
Map properties = new HashMap();
String pass = txtPassword.getText();
properties.put("javax.persistence.jdbc.url", txtBDUrl.getText());
if (cboDBDriver.getSelectedIndex() ==2)
{
properties.put("javax.persistence.jdbc.driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver");
selectedDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
}
properties.put("javax.persistence.jdbc.password",pass);
properties.put("javax.persistence.jdbc.user", txtUser.getText());
entityManagerFactory = Persistence.createEntityManagerFactory("PrescricaoReceitasPU",properties);
EntityManager entityManager = entityManagerFactory.createEntityManager();
if(entityManager.isOpen())
{
JOptionPane.showMessageDialog(new JFrame(),
"Foi possível conectar à base de dados com sucesso",
"Configuração",JOptionPane.INFORMATION_MESSAGE);
btnConfirmar.setEnabled(true);
}else
{
JOptionPane.showMessageDialog(new JFrame(),
"Não foi possível conectar à base de dados",
"Configuração",JOptionPane.ERROR_MESSAGE);
btnConfirmar.setEnabled(false);
return;
}
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(new JFrame(),
"Não foi possível conectar à base de dados",
"Configuração",JOptionPane.ERROR_MESSAGE);
Logger.getLogger(PrescricaoReceitasApp.LogName).log(Level.INFO, ex.getMessage(), ex);
}
finally
{
txtBDUrl.setEnabled(true);
txtPassword.setEnabled(true);
txtUser.setEnabled(true);
}
}//GEN-LAST:event_btnTestConnectionActionPerformed
编辑:这是OP的错误控制台:
org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 192.168.x.x, port x has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
Error Code: 0
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.2.v20100323-r6872): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 192.168.x.x, port x has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
Error Code: 0
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:397)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:151)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:207)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:195)
at prescricaoreceitas.AdminTools.DBConnectionDialogView.btnTestConnectionActionPerformed(DBConnectionDialogView.java:296)
at prescricaoreceitas.AdminTools.DBConnectionDialogView.access$0(DBConnectionDialogView.java:260)
at prescricaoreceitas.AdminTools.DBConnectionDialogView$1.actionPerformed(DBConnectionDialogView.java:121)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.WaitDispatchSupport$2.run(Unknown Source)
at java.awt.WaitDispatchSupport$4.run(Unknown Source)
at java.awt.WaitDispatchSupport$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.WaitDispatchSupport.enter(Unknown Source)
at java.awt.Dialog.show(Unknown Source)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at java.awt.Dialog.setVisible(Unknown Source)
at prescricaoreceitas.INFOReceitasMainView.formComponentShown(INFOReceitasMainView.java:1372)
at prescricaoreceitas.INFOReceitasMainView.access$12(INFOReceitasMainView.java:1314)
at prescricaoreceitas.INFOReceitasMainView$8.componentShown(INFOReceitasMainView.java:670)
at java.awt.AWTEventMulticaster.componentShown(Unknown Source)
at java.awt.Component.processComponentEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Window.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.2.v20100323-r6872): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 192.168.x.x, port x has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
Error Code: 0
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:319)
at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:138)
at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:584)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:228)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:369)
... 79 more
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 192.168.x.x, port x has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:187)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:238)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2197)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:446)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1338)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1022)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:858)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:98)
... 83 more
控制台中没有日志文件或错误来帮助我们吗?它没有给出任何错误,只是说无法连接数据库。这是一个错误!你能给我们看看留言吗?这就是你要找的吗?很好。您确定这两台计算机都有权访问服务器和网络吗?(端口、IP、防火墙等)