如何在OS X上使用jTDS驱动程序连接到SQL Server?

时间:2016-05-12 22:46:05

标签: java hibernate jdbc jtds

我可以从我的mac上的虚拟操作系统(windows 8)连接到sqlserver,但我无法连接到mac上的sqlserver。

我的连接字符串如下所示。

server = (String) modelmain.getProp().getProperty("testserver1");
db = (String) modelmain.getProp().getProperty("testdb1");
user = (String) modelmain.getProp().getProperty("testuser1");
pass = (String) modelmain.getProp().getProperty("testpass1");

String getDatabaseJtdsUrl = "jdbc:jtds:sqlserver://" + server + "/" + db + ";"; 

我的变量如下所示。

server = "MyServer:1433"
db= "Mydb"
user = "dbuser"
pass = "userpassword"

我给用户名并传递hibernate的属性方法。顺便说一句,我使用休眠。

Configuration cfg = new Configuration();
cfg.setProperty("hibernate.connection.driver_class",
            "net.sourceforge.jtds.jdbc.Driver");
cfg.setProperty("hibernate.connection.url", getDatabaseJtdsUrl);
cfg.setProperty("hibernate.connection.username", user);
cfg.setProperty("hibernate.connection.password", pass);

如何在mac osx上使用jtds驱动程序连接到sqlserver?

错误如下。

SessionFactory initial creation error.org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Exception in thread "Thread-1" java.lang.ExceptionInInitializerError
    at com.ozpas.dao.HibernateUtilOzpasentegre.<clinit>(HibernateUtilOzpasentegre.java:75)
    at com.ozpas.entegre.model.ModelConfig.prepareMapconfig(ModelConfig.java:41)
    at com.ozpas.entegre.model.ModelConfig.<init>(ModelConfig.java:23)
    at com.ozpas.entegre.model.ModelConfig.getInstance(ModelConfig.java:29)
    at com.ozpas.dao.ConnectionFactoryMikro.<init>(ConnectionFactoryMikro.java:20)
    at com.ozpas.dao.ConnectionFactoryMikro.<clinit>(ConnectionFactoryMikro.java:15)
    at com.ozpas.entegre.model.ModelCari.getlistCarileruppercase(ModelCari.java:1584)
    at com.ozpas.entegre.model.ModelCachevar.prepareCachemusteribilgileri(ModelCachevar.java:39)
    at com.ozpas.entegre.controller.MainCr$2.run(MainCr.java:88)
Caused by: 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 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at com.ozpas.dao.HibernateUtilOzpasentegre.<clinit>(HibernateUtilOzpasentegre.java:70)
    ... 8 more
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)
    ... 22 more
Caused by: java.sql.SQLException: Unknown server host name 'MYSERVER'.
    at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:427)
    at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38)
    ... 37 more
Caused by: java.net.UnknownHostException: MYSERVER
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:288)
    at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:251)
    at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:331)
    ... 
 39 more

1 个答案:

答案 0 :(得分:0)

试试这个

Configuration cfg = new Configuration()
        .addClass(org.hibernate.auction.Item.class)
        .addClass(org.hibernate.auction.Bid.class)
        .setProperty("hibernate.dialect","org.hibernate.dialect.SQLServerDialect")
         cfg.setProperty("hibernate.connection.driver_class",
            "net.sourceforge.jtds.jdbc.Driver");
         cfg.setProperty("hibernate.connection.url", getDatabaseJtdsUrl);
         cfg.setProperty("hibernate.connection.username", user);
         cfg.setProperty("hibernate.connection.password", pass);

然后你必须获得SessionFactory和JDBC连接

    SessionFactory sf = cfg.buildSessionFactory();
    Session session = sf.openSession();

您可以使用xml文件进行配置,请参阅此answer