简体   繁体   English

使用 Hibernate 调用 Driver#connect 时出错

[英]“Error calling Driver#connect” using Hibernate

This is my cfg.xml file:这是我的 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" />

User class and Main class:用户类和主类:

@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();

}

When I run the program i get this error:当我运行程序时,我收到此错误:

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

I'm using sql server 2008. I tried different types of cfg configuration but none seems to work.我使用的是 sql server 2008。我尝试了不同类型的 cfg 配置,但似乎都不起作用。 I believe the problem is from connection.url.我相信问题来自 connection.url。 I tried to change the port (1433) but nothing.我试图更改端口 (1433) 但什么也没有。 Can anyone tell what's the problem ?谁能告诉我有什么问题?

I know this post is almost 2 years old, but for me the questions gave me, for me, the right answer.我知道这篇文章已经快 2 年了,但对我来说,这些问题给了我正确的答案。 And because people with the same question are getting this post from Google.因为有同样问题的人是从谷歌那里得到这篇文章的。 I thought, lets add my story.我想,让我们添加我的故事。

Same thing, same problem.同样的事情,同样的问题。 And for me the answer was that SQL Server didn't accept connections from the outside.对我来说,答案是 SQL Server 不接受来自外部的连接。 The tcp/ip network client configuration is standard disabled. tcp/ip 网络客户端配置是标准禁用的。

So it was for me a hint in the right direction.所以这对我来说是一个正确方向的提示。

I have Faced Same issue just i have changed the Version on mysql server connector than works fine我遇到了同样的问题,只是我更改了 mysql 服务器连接器上的版本,但工作正常

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

added latest version of mysql connector添加了最新版本的 mysql 连接器

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

I changed the version of MySql like,我更改了 MySql 的版本,例如,

From 5.1.8从 5.1.8

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

To 8.0.19到 8.0.19

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

It works for me.这个对我有用。 Thank you.谢谢你。

Use This with all Permission of DB在 DB 的所有权限下使用它

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

Try going to the command line and see if sqlserver is indeed listening to that port.尝试转到命令行并查看 sqlserver 是否确实在侦听该端口。 On windows this would be the command:在 Windows 上,这将是命令:

netstat -nao | findstr 1433

Then see if the PID is Sql Server as expected in the windows (Ctr+Alt+Delete) process list.然后在windows(Ctr+Alt+Delete)进程列表中查看PID是否为Sql Server,如预期。

If the server is running, try to connect to it using a SQL client GUI, see here for examples .如果服务器正在运行,请尝试使用 SQL 客户端 GUI 连接到它,请参见此处的示例 When you can connect to the database from an external client successfully, try again from the Java program.当您可以从外部客户端成功连接到数据库时,请从 Java 程序中重试。

Try to see also if your local firewall settings to see if the firewall is the cause for the error, although by default firewalls do not block localhost - have a look at this answer .还尝试查看您的本地防火墙设置是否是防火墙导致错误的原因,尽管默认情况下防火墙不会阻止 localhost - 看看这个答案

Check you Sid in检查你 Sid

hibernate.cfg.xml file hibernate.cfg.xml 文件

For Oracle Express Edition:对于 Oracle 快捷版:

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

For Oracle Enterprise Edition:对于 Oracle 企业版:

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

the error is due to AnnotationConfiguration and making session by it.错误是由于 AnnotationConfiguration 并通过它进行会话。 you can try this solution:你可以试试这个解决方案:

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

I also face same problem due to some dependency issue.由于某些依赖性问题,我也面临同样的问题。 i solve my problem to change sql depenedency which is compatible to my current version of sql server in pom.xml file.我解决了我的问题,以更改与 pom.xml 文件中我当前版本的 sql server 兼容的 sql 依赖。

I had a same error when migrating from jTDS1.2.8 to mssql-jdbc and for me it helped removing the port number and specifying the named instance in the connection URL within hibernate.cfg.xml从 jTDS1.2.8 迁移到 mssql-jdbc 时我遇到了同样的错误,对我来说它有助于删除端口号并在 hibernate.cfg.xml 中的连接 URL 中指定命名实例

From:从:

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

To:到:

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

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

When using Named and Multiple SQL Server Instances, to use a JDBC URL property, do the following:使用命名和多个 SQL Server 实例时,要使用 JDBC URL 属性,请执行以下操作:

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

if you are using a virtual server or server that is not updated the time zone should update it.如果您使用的是未更新的虚拟服务器或服务器,则时区应更新它。

SQL CONSOLE: SQL 控制台:

SET GLOBAL time_zone = '+3:00';

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

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

If you're using Hibernate 4, try switching to Hibernate 5.如果您使用的是 Hibernate 4,请尝试切换到 Hibernate 5。

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

This helped me resolve the error.这帮助我解决了错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate、JDBC:调用 Driver#connect 时出错 - Hibernate, JDBC: Error calling Driver#connect org.hibernate.exception.JDBCConnectionException:调用Driver#connect时出错 - org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect failed.org.hibernate.exception.JDBCConnectionException:使用PostGres Hibernate调用Driver#connect时出错 - failed.org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect with PostGres Hibernate 无法创建sessionFactory object.org.hibernate.exception.JDBCConnectionException:调用Driver#connect时出错 - Failed to create sessionFactory object.org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect 引起原因:org.hibernate.exception.JDBCConnectionException:调用Driver#connect时出错 - Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect 调用Driver#connect时出错,并且无法创建与数据库服务器的连接 - Error calling Driver#connect and Could not create connection to database server amazon aws ubuntu java mysql webservice“错误调用Driver#connect” - amazon aws ubuntu java mysql webservice “Error calling Driver#connect” 没有合适的司机。尝试使用Hibernate连接到Heroku上的postgresql数据库 - No suitable driver. Trying to connect to a postgresql database on Heroku using Hibernate 如何设置休眠配置属性以使用结构驱动程序连接到Mysql - How to set Hibernate Configuration properties to connect to Mysql using Fabric driver 在Hibernate中调用存储的proc时出错 - Error with calling stored proc in Hibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM