无法通过JDBC连接到jdbc:mysql // localhost:3306 / database

时间:2018-08-31 22:22:57

标签: java hibernate maven jdbc ide

我全力以赴解决这个问题,但我仍然坚持,我不知道是什么...

mysql版本是5.7,休眠是5.2.8。最终,我的sql连接器是5.1.2。

使用STS IDE即时通讯

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>hibernate</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>hibernate</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>


  <dependencies>
  <dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jdbc</artifactId>
    <version>9.0.0.M21</version>
</dependency>
  <dependency>
    <groupId>com.sun.activation</groupId>
    <artifactId>javax.activation</artifactId>
    <version>1.2.0</version>
</dependency>
  <dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.4.0-b180725.0644</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.3.0.1</version>
</dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

     <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>ejb3-persistence</artifactId>
            <version>1.0.2.GA</version>
        </dependency>

    <!-- <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.5.6-Final</version>
    </dependency> -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.2</version>
    </dependency>

    <!-- <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>3.2.0.Final</version>
        </dependency> -->

    <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
       <version>1.7.5</version>
   </dependency>
   <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-simple</artifactId>
       <version>1.6.4</version>
   </dependency>

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

  </dependencies>

</project>

hibernate.cfg.xml

我认为问题出在这里,我想是版本,但在调查中我读到的是未安装JDBC,但我安装了与5.7 mysql兼容的连接器8.0.12.jar。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC 
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql//localhost:3306/database</property>
        <property name="hibernate.connection.username">username</property>
        <property name="hibernate.connection.password">password</property>

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


    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection_pool_size">true</property>

        <!-- SQL dialect -->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <!-- Echo all executed SQL to stdout -->
    <property name="hibernate.show_sql">true</property>

    <!-- Enable Hibernate's automatic session context management -->
        <property name="hibernate.current_session_context_class">thread</property>


        <mapping class="com.example.hibernate.model.SocialMedia" />
        <mapping class="com.example.hibernate.model.Course" />
        <mapping class="com.example.hibernate.model.Teacher" />
        <mapping class="com.example.hibernate.model.TeacherSocialMedia" />
    </session-factory>
</hibernate-configuration>

配置

SessionFactory sessionFactory;
           Configuration configuration = new Configuration();
          configuration.configure("hibernate.cfg.xml");
          sessionFactory = configuration.buildSessionFactory();
          Session session = sessionFactory.openSession();

错误...

Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

和...

Unable to make JDBC Connection [jdbc:mysql//localhost:3306/database]

2 个答案:

答案 0 :(得分:1)

您在连接URL中:之后错过了mysql。正确的应该看起来像jdbc:mysql://localhost:3306/database

答案 1 :(得分:0)

尝试使用,

@property