2017-01-11 69 views
0

我有Eclipse和Hibernate連接到Azure Sql Server(Microsoft Azure for Students)。我上傳到一個git項目,完美地在Windows上的學校pc上工作,但這裏在我的Mac上無法連接到服務器......唯一的區別是Hibernate Library & JDBC庫,它的代碼是相同的。 :(Azure Sql服務器和Hibernate Java Mac

輸出:

Jan 11, 2017 4:03:51 PM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {5.1.3.Final} 
Jan 11, 2017 4:03:51 PM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000206: hibernate.properties not found 
Jan 11, 2017 4:03:51 PM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
Jan 11, 2017 4:03:52 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity 
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time. 
Jan 11, 2017 4:03:52 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> 
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 
Jan 11, 2017 4:03:52 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) 
Jan 11, 2017 4:03:52 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 
INFO: HHH10001005: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://usedproy.database.windows.net:1433;database=used;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;] 
Jan 11, 2017 4:03:52 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 
INFO: HHH10001001: Connection properties: {[email protected], password=****} 
Jan 11, 2017 4:03:52 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 
INFO: HHH10001003: Autocommit mode: false 
Jan 11, 2017 4:03:52 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init> 
INFO: HHH000115: Hibernate connection pool size: 20 (min=1) 
Failed to create sessionFactory object.org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] 
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:264) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:228) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207) 
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51) 
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207) 
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352) 
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111) 
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83) 
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418) 
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692) 
    at probandohibernate.PersonFactory.<clinit>(PersonFactory.java:31) 
    at probandohibernate.ProbandoHibernate.insert(ProbandoHibernate.java:55) 
    at probandohibernate.ProbandoHibernate.main(ProbandoHibernate.java:23) 
Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect 
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:115) 
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:101) 
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:123) 
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:41) 
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58) 
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:106) 
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:40) 
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:19) 
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:138) 
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:110) 
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:74) 
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207) 
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145) 
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66) 
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) 
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:254) 
    ... 15 more 
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: SQL Server did not return a response. The connection has been closed. ClientConnectionId:c2231f63-42b7-473b-8cab-e16e8473cb31 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1667) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1654) 
    at com.microsoft.sqlserver.jdbc.TDSReader.readPacket(IOBuffer.java:4844) 
    at com.microsoft.sqlserver.jdbc.TDSCommand.startResponse(IOBuffer.java:6154) 
    at com.microsoft.sqlserver.jdbc.TDSCommand.startResponse(IOBuffer.java:6106) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2907) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2234) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220) 
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827) 
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012) 
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38) 
    ... 30 more 
Exception in thread "main" java.lang.NullPointerException 
    at probandohibernate.ProbandoHibernate.insert(ProbandoHibernate.java:82) 
    at probandohibernate.ProbandoHibernate.main(ProbandoHibernate.java:23) 

休眠CONF:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
             "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
<session-factory name="Hibernate"> 
    <property name="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</property> 
    <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property> 
    <property name="hibernate.connection.url">jdbc:sqlserver://***.database.windows.net:1433;database=****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;</property> 
    <property name="hibernate.connection.username">***@***</property> 
    <property name="hibernate.connection.password">***</property> 
    <property name="show_sql">true</property> 
    <property name="hibernate.hbm2ddl.auto">update</property> 
    <mapping class="probandohibernate.modelo.Empleados"/> 
</session-factory> 
</hibernate-configuration> 

的Java Class:

package probandohibernate.modelo; 

/** 
* 
* @author leo 
*/ 

import java.sql.Date; 
import javax.persistence.*; 
/** 
* 
* @author John 
*/ 
@Entity 
@Table(name = "Empleados") 
public class Empleados { 

    @Id 
    @GeneratedValue(strategy= GenerationType.IDENTITY) //Editado 
    private Long id; 
    @Column(name = "Nombre") 
    private String nombre; 
    @Column(name = "Apellidos") 
    private String apellido; 
    @Column(name = "FechaNacimiento") 
    private Date fechaNacimiento; 
    @Column(name = "Movil") 
    private String movil; 

    public Empleados() { 
    } 

    public Empleados(String nombre, String apellido, Date fechaNacimiento, String movil) { 
     this.nombre = nombre; 
     this.apellido = apellido; 
     this.fechaNacimiento = fechaNacimiento; 
     this.movil = movil; 

    } 

    public Long getId() { 
     return id; 
    } 

    public String getNombre() { 
     return nombre; 
    } 

    public String getApellido() { 
     return apellido; 
    } 

    public Date getFechaNacimiento() { 
     return fechaNacimiento; 
    } 

    public String getMovil() { 
     return movil; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public void setNombre(String nombre) { 
     this.nombre = nombre; 
    } 

    public void setApellido(String apellido) { 
     this.apellido = apellido; 
    } 

    public void setFechaNacimiento(Date fechaNacimiento) { 
     this.fechaNacimiento = fechaNacimiento; 
    } 

    public void setMovil(String movil) { 
     this.movil = movil; 
    } 

    @Override 
    public String toString() { 
     return "Empleado [id=" + id + ", nombre=" + nombre + ", apellido=" + apellido + ", fechaNacimiento=" 
       + fechaNacimiento + ", movil=" + movil + "]"; 
    } 



} 

休眠類別:

package probandohibernate; 

/** 
* 
* @author leo 
*/ 
import org.hibernate.SessionFactory; 
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.service.ServiceRegistry; 

import probandohibernate.modelo.Empleados; 

public class PersonFactory { 
    private static ServiceRegistry serviceRegistry; 
    private static final SessionFactory sessionFactory; 

    static { 
     try { 
Configuration configuration=new Configuration() 
      .configure(); // configures settings from hibernate.cfg.xml 
configuration.addAnnotatedClass(Empleados.class); 

    StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder(); 

    // If you miss the below line then it will complaing about a missing dialect setting 
    serviceRegistryBuilder.applySettings(configuration.getProperties()); 

    serviceRegistry = serviceRegistryBuilder.build(); 
    sessionFactory = configuration.buildSessionFactory(serviceRegistry); 
     } catch (Throwable ex) { 
      System.err.println("Failed to create sessionFactory object." + ex); 
      ex.printStackTrace(); 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 

    public static SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 
} 

private static void read(){ 
      Session session = null; 
      try { 
       try { 

       sessionFactory = PersonFactory.getSessionFactory(); 
       session = sessionFactory.openSession(); 

       @SuppressWarnings("unchecked") 
       List<Empleados> list = session.createQuery("from Empleados") 
         .list(); 

       for (Empleados p : list) 
        System.out.println(p); 


       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } finally { 
       session.close(); 
      } 
    } 

庫:

Libraries I use

更新:

我只是禁用塞拉利昂和Avast的的防火牆和得到這個例外。

Jan 13, 2017 12:40:54 AM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {5.1.0.Final} 
Jan 13, 2017 12:40:54 AM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000206: hibernate.properties not found 
Jan 13, 2017 12:40:54 AM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
Jan 13, 2017 12:40:54 AM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity 
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time. 
Jan 13, 2017 12:40:54 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> 
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 
Jan 13, 2017 12:40:54 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) 
Jan 13, 2017 12:40:54 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 
INFO: HHH10001005: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://usedproy.database.windows.net:1433;database=used;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;] 
Jan 13, 2017 12:40:54 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 
INFO: HHH10001001: Connection properties: {[email protected], password=****} 
Jan 13, 2017 12:40:54 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator 
INFO: HHH10001003: Autocommit mode: false 
Jan 13, 2017 12:40:54 AM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init> 
INFO: HHH000115: Hibernate connection pool size: 20 (min=1) 
Failed to create sessionFactory object.org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] 
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189) 
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51) 
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189) 
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352) 
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111) 
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83) 
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418) 
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692) 
    at probandohibernate.PersonFactory.<clinit>(PersonFactory.java:34) 
    at probandohibernate.ProbandoHibernate.insert(ProbandoHibernate.java:65) 
    at probandohibernate.ProbandoHibernate.main(ProbandoHibernate.java:30) 
Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect 
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:115) 
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:101) 
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:123) 
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:41) 
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58) 
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:106) 
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:40) 
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:19) 
    at org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:138) 
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:110) 
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:74) 
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189) 
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145) 
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66) 
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) 
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234) 
    ... 15 more 
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Connection reset ClientConnectionId:93c921a0-b8bf-48fe-b3da-0064cc309d63 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1667) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1654) 
    at com.microsoft.sqlserver.jdbc.TDSChannel.read(IOBuffer.java:1789) 
    at com.microsoft.sqlserver.jdbc.TDSReader.readPacket(IOBuffer.java:4838) 
    at com.microsoft.sqlserver.jdbc.TDSCommand.startResponse(IOBuffer.java:6154) 
    at com.microsoft.sqlserver.jdbc.TDSCommand.startResponse(IOBuffer.java:6106) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2907) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2234) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220) 
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827) 
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012) 
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38) 
    ... 30 more 
Exception in thread "main" java.lang.NullPointerException 
    at probandohibernate.ProbandoHibernate.insert(ProbandoHibernate.java:92) 
    at probandohibernate.ProbandoHibernate.main(ProbandoHibernate.java:30) 

- 更多信息:

如果幫助一些與JDBC不工作太

代碼JDBC的:

package es.iesnervion.model; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 

public class MakingStatement { 

    public static void main(String[] args) { 

      // Carga el driver 
      try { 

       // Carga la clase del driver 
       Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 

       // Define the data source for the driver 
       String sourceURL = "jdbc:sqlserver://******.database.windows.net:1433;database=***;user=***@usedproy;password=****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;"; 
       String miSelect = "SELECT ID, Nombre FROM Empleados"; 

       // Crear una connexi�n con el DriverManager 
       Connection connexionBaseDatos = 
       DriverManager.getConnection(sourceURL); 
       Statement sentencia = connexionBaseDatos.createStatement(); 
       ResultSet nombresProductos = sentencia.executeQuery(miSelect); 

       // Mostrar los datos del ResultSet 
       System.out.println("Productos       -> PVP"); 
       System.out.println("------------------------------------------"); 
       while (nombresProductos.next()) 
        System.out.println(nombresProductos.getString("Nombre")+ " -> " + 
        nombresProductos.getInt("ID")); 

       // Cerrar conexi�n 
        connexionBaseDatos.close(); 
      } 
       catch (ClassNotFoundException cnfe) { 
       System.err.println(cnfe); 
      } 
       catch (SQLException sqle) { 
       sqle.printStackTrace(); 
      } 
    } 
} 
+0

數據庫可能以某種方式配置爲僅接受來自學校IP的請求,因此當您嘗試從家中連接時,它不會回答?或者你家裏有防火牆阻止訪問端口1433出站? – Vampire

+0

數據庫有一個防火牆與withelist ip,但我家和學校的ip被引入。我可以用Visual Studio Code連接到數據庫,這樣我就可以實現連接。 :/ –

+0

@AdriánPol你學校的Windows PC和你的Mac之間的Hibernate和JDBC庫有什麼區別?它的版本不同嗎?還是其他人,比如在Windows上使用jTDS,在Mac上使用MSSQL JDBC? –

回答