"调用Driver#connect"时出错使用Hibernate

时间:2014-03-16 18:37:07

标签: java hibernate

这是我的cfg.xml文件:

    <!-- Database connection settings -->
    <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=hiber;</property>
    <property name="connection.username">Costi-PC\Costi</property>
    <property name="connection.password"></property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

    <!-- Disable the second-level cache -->
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">create</property>

    <!-- Names the annotated entity class -->
    <mapping class="model.User" />

用户类和主类:

@Entity
    public class User {

        @Id
        private int userId;
        private String userName;

        public int getUserId() {..}
        public void setUserId(int userId) {..}
        public String getUserName() {..}
        public void setUserName(String userName) {..}

    }

    public class Main {

        public static void main(String[] args) {
           User user = new User();
           user.setUserId(1);
           user.setUserName("Costi");

           AnnotationConfiguration configuration = new AnnotationConfiguration();
           configuration.configure();

           SessionFactory sessionFactory = configuration.buildSessionFactory();

           Session session = sessionFactory.openSession();
           session.beginTransaction();
           session.save(user);
           session.getTransaction().commit();
           session.close();

           sessionFactory.close();

}

当我运行程序时,我收到此错误:

Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:132)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:118)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:140)
    at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:58)
    at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:75)
    at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
    at main.Main.main(Main.java:23)
Caused by: 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. 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:190)
    at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241)
    at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2243)
    at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
    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:55)
    ... 14 more

我正在使用sql server 2008。 我尝试了不同类型的cfg配置,但似乎都没有。 我相信问题来自connection.url。我试图更改端口(1433),但没有。 谁能说出问题是什么?

13 个答案:

答案 0 :(得分:3)

我知道这篇文章差不多有两年了,但对我而言,问题给了我正确答案。因为有相同问题的人会从Google收到这篇文章。我想,让我们添加我的故事。

同样的问题,同样的问题。对我来说,答案是SQL Server不接受来自外部的连接。 tcp / ip网络客户端配置是标准禁用的。

所以这对我来说是朝着正确方向的暗示。

答案 1 :(得分:2)

我更改了MySql的版本,

从5.1.8起

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.8</version>
    </dependency>    

至8.0.19

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.19</version>
    </dependency>

对我有用。 谢谢。

答案 2 :(得分:0)

尝试转到命令行,查看sqlserver是否确实正在侦听该端口。在Windows上,这将是命令:

netstat -nao | findstr 1433

然后在Windows(Ctr + Alt + Delete)进程列表中查看PID是否是预期的SQL Server。

如果服务器正在运行,请尝试使用SQL客户端GUI连接到它,请参阅此处examples。如果可以从外部客户端成功连接到数据库,请再次从Java程序中重试。

尝试查看本地防火墙设置是否可以查看防火墙是否是导致错误的原因,尽管默认情况下防火墙不会阻止localhost - 请查看此answer

答案 3 :(得分:0)

检查你的Sid in

  

hibernate.cfg.xml文件

对于Oracle Express Edition:

<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>

对于Oracle Enterprise Edition:

<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> 

答案 4 :(得分:0)

错误是由于AnnotationConfiguration并由它进行会话。 你可以尝试这个解决方案:

@Autowired
SessionFactory sessionFactory;
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(ce);

答案 5 :(得分:0)

将此功能与DB的所有权限

一起使用
 <property name="hibernate.connection.url">jdbc:mysql://ip:3306/dbname</property><property name="hibernate.connection.username">*******</property>
     <property name="hibernate.connection.password">*******</property> 

答案 6 :(得分:0)

由于某些依赖性问题,我也面临同样的问题。我解决了更改与pom.xml文件中当前版本的sql server兼容的sql依赖关系的问题。

答案 7 :(得分:0)

从jTDS1.2.8迁移到mssql-jdbc时出现相同的错误,对我来说,这有助于删除端口号并在hibernate.cfg.xml中的连接URL中指定命名实例

发件人:

<property name="hibernate.connection.url">jdbc:sqlserver://MYHOSTNAME:1433;databaseName=mydbname;instanceName=MYINSTANCENAME;</property>

收件人:

  <property name="hibernate.connection.url">jdbc:sqlserver://MYHOSTNAME;instanceName=MYINSTANCENAME;databaseName=mydbname;</property>

参考:https://docs.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url?view=sql-server-2017

使用命名和多个SQL Server实例时,要使用JDBC URL属性,请执行以下操作:

jdbc:sqlserver://localhost;instanceName=instance1;integratedSecurity=true;<more properties as required>;

答案 8 :(得分:0)

如果您使用的是虚拟服务器或未更新的服务器,则时区应该对其进行更新。

SQL控制台:

printf("%s \n", (i<9)? list[i-1] : list[9+(i%2)] );

答案 9 :(得分:0)

我遇到了同样的问题,只是我更改了mysql服务器连接器上的版本,但无法正常工作

    <!-- commented as it was not make to connect 
       <dependency> 
          <groupId>mysql</groupId> 
          <artifactId>mysql-connector-java</artifactId> 
          <version>5.1.34</version> 
       </dependency> 
   -->

添加了最新版本的mysql连接器

   <!-- added latest version of mysql connector -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.18</version>
            </dependency>

答案 10 :(得分:0)

配置休眠文件“ hibernate.cfg.xml”中的用户名或密码错误

答案 11 :(得分:0)

请确保您要连接的数据库已经在同名数据库中创建。

答案 12 :(得分:0)

如果您使用的是 Hibernate 4,请尝试切换到 Hibernate 5。

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.4.10.Final</version>
</dependency>

这帮助我解决了错误。