简体   繁体   English

Java Spring 错误:无法获取 JDBC 连接

[英]Java Spring Error: Could not get JDBC Connection

I am doing a small exercise using Spring framework and JDBC, and I got the following error:我正在使用 Spring 框架和 JDBC 做一个小练习,我得到了以下错误:

    Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: No suitable driver found for jdbc:mysql:3306//localhost/springcore
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:573)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:812)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:868)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:876)
    at springcore.springcore.EmployeeJDBCTemplate.create(EmployeeJDBCTemplate.java:23)
    at springcore.springcore.EmployeeApp.main(EmployeeApp.java:23)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql:3306//localhost/springcore
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:173)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:164)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:149)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:119)
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)

My pom.xml:我的 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>springcore</groupId>
  <artifactId>springcore</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

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

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

      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.0.RELEASE</version>
        </dependency>
        <!-- MySQL database driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.9</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.2.0.RELEASE</version>
        </dependency>
      </dependencies>
    </project>

and Beans.xml:和 Beans.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">

   <!-- Initialization for data source -->
   <bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql:3306//localhost/springcore"/>
      <property name="username" value="root"/>
      <property name="password" value=""/>
   </bean>
<!-- Definition for employeeJDBCTemplate bean -->
   <bean id="employeeJDBCTemplate" 
      class="springcore.springcore.EmployeeJDBCTemplate">
      <property name="dataSource"  ref="dataSource" />    
   </bean>
</beans>

Database: MySQL, I use Xampp and phpmyadmin to manage the database: IDE, esclipse, Maven project!数据库:MySQL,我用Xampp和phpmyadmin管理数据库:IDE,esclipse,Maven项目!

Am I missing libraries or something?我是不是缺少图书馆之类的?

Please tell me where am I getting wrong: :D Thanks in advance!请告诉我哪里出错了::D 提前致谢!

are u sure the connection string correct? 您确定连接字符串正确吗?

default url 默认网址

jdbc:mysql://localhost:3306/dbname

Clean And Build Your Projrct.. 清理并构建您的项目。

Tomcat server Terminal and Restart. Tomcat服务器终端并重新启动。

take your URL correct. 正确输入您的网址。

  JDBC_DRIVER = "com.mysql.jdbc.Driver"; DB_URL = "jdbc:mysql://localhost/EMP"; conn = DriverManager.getConnection(DB_URL,USER,PASS); 

When I typed in "localhost", it somehow didn't work but this:当我输入“localhost”时,它不知何故不起作用,但是这个:

jdbc:mysql://127.0.0.1:3306/dbname

seemed to work for me....似乎对我有用....

Please refer the below article to construct JDBC URL to frame in different ways for mysql.请参考下面的文章构造JDBC URL 以不同方式为mysql 构建。

Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.驱动class:mysql数据库的驱动class为com.mysql.jdbc.Driver。 Connection URL: The connection URL for the mysql database is jdbc:mysql://localhost:7101/empDB连接URL:mysql数据库的连接URL为jdbc:mysql://localhost:7101/empDB

Class.forName("com.mysql.jdbc.Driver");  
Connection con=DriverManager.getConnection(  
"jdbc:mysql://localhost:7101/empDB","root","root");  
//here empDB is database name, root is username and password 

where jdbc is the API, mysql is the database, localhost is the server name on which mysql is running, we may also use IP address, 7101 is the port number and empDB is the database name.其中 jdbc 是 API,mysql 是数据库,localhost 是运行 mysql 的服务器名称,我们也可以使用 IP 地址,7101 是端口号,empDB 是数据库名称。 We may use any database, in such case, we need to replace the empDB with our database name.我们可以使用任何数据库,在这种情况下,我们需要用我们的数据库名称替换 empDB。

MySQL Connector Database URL The following is the database connection URL syntax for MySQL Connector: MySQL 连接器数据库 URL 以下是 MySQL 连接器的数据库连接 URL 语法:

jdbc:mysql://[host][,failoverhost...]
    [:port]/[database]
    [?propertyName1][=propertyValue1]
    [&propertyName2][=propertyValue2].

https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-jdbc-url-format.html https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-reference-jdbc-url-format.html

https://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html https://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html

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

相关问题 Spring框架的MySQL连接错误(org.springframework.jdbc.CannotGetJdbcConnectionException:无法获取JDBC连接;) - Mysql connection error for Spring framework (org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; ) Spring 2.5无法获得JDBC连接 - Spring 2.5 Could not get JDBC Connection 无法获得JDBC连接 - Could not get JDBC connection Java Spring JDBC SQL Server连接错误 - Java Spring jdbc sql server connection error Java Derby DB错误“线程“ index-stat-thread”中的异常”和“无法获取JDBC连接”错误 - Java Derby DB error 'Exception in thread “index-stat-thread”' and “Could not get JDBC Connection” error Spring JDBC错误连接 - Spring JDBC error connection 无法在JUnit中获得JDBC连接 - Could not get JDBC connection in JUnit 无法获得JDBC连接。 嵌套异常为java.sql.SQLException:ORA-28040:Spring Batch中没有匹配的身份验证协议 - Could not get JDBC Connection; nested exception is java.sql.SQLException: ORA-28040: No matching authentication protocol in Spring Batch 在本地主机上连接Oracle时出错:无法获得JDBC连接? - Error connecting Oracle on localhost: Could not get JDBC Connection? 无法在 Spring MVC 中建立 JDBC 连接 - Could not establish JDBC connection in Spring MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM