【问题标题】:Loaded JDBC MySQL Driver but failed to connect to MySQL via Spring config已加载 JDBC MySQL 驱动程序,但无法通过 Spring 配置连接到 MySQL
【发布时间】:2017-08-07 10:50:54
【问题描述】:

使用 JDBC Driver Manager 连接到 MySQL DB 时,连接成功并按预期检索结果集。但是当尝试使用 org.springframework.jdbc.datasource.DriverManagerDataSource 或 org.springframework.jdbc.datasource.SimpleDriverDataSource

JDBC 驱动程序 com.mysql.jdbc.Driver 已加载,但无法与 MySQL 建立连接。

测试应用类: 包 com.xxxx.xxxx.xxxx;

import com.xxxx.xxxx.xxxx.dao.TimeslotDAO;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class App {

    public static void main(String[] args) {
        //testClassic(); // This works and gets the results.
        testSpring();  // Fails with SQLException.
    }

    private static void testSpring(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("Spring-Module.xml");
        TimeslotDAO timeSlot = (TimeslotDAO) ctx.getBean("timeslotDAO");
        timeSlot.populateTimeSlotsCache();
    }

    private static void testClassic(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
        }catch(Exception e){
            e.printStackTrace();
        }
        try {
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/FR", "root", "xxxx");
            if(conn!=null) {
                String sql = "select * from TB_AUTO_xxx_SLOT_xxx " +
                        "where i_book_id =2639";
                PreparedStatement stmt =  conn.prepareStatement(sql);
                ResultSet rs = stmt.executeQuery();
                while(rs.next()){
                    System.out.println(rs.getLong(1));
                }
                if(rs!=null) rs.close();
                if(stmt!=null)stmt.close();
                if(conn!=null)conn.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }

    }
}

数据源的Spring配置:

<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
       <property name="url" value="jdbc:msql://localhost:3306/FR" />
       <property name="username" value="root"/>
       <property name="password" value="xxxx"/>

</bean>

日志中的异常:

INFO: Loaded JDBC driver: com.mysql.jdbc.Driver
java.sql.SQLException: No suitable driver found for jdbc:msql://localhost:3306/FR
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:208)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:174)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:165)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:149)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:119)
    at com.xxxx.xxxx.xxxx.dao.impl.TimeslotDAOImpl.populateTimeSlotsCache(TimeslotDAOImpl.java:23)
    at com.xxxx.xxxx.xxxx.App.testSpring(App.java:22)
    at com.xxxx.xxxx.xxxx.App.main(App.java:16)

【问题讨论】:

  • 你在这里想念你:jdbc:msql
  • @Jens 错过了什么,抱歉没看懂
  • jdbc:msql 必须是 jdbc:mySQL: value="jdbc:msql://localhost:3306/FR" --> value="jdbc:m ysql://localhost:3306/FR"
  • 感谢您的帮助。
  • 不客气

标签: java mysql spring sqlexception


【解决方案1】:

您的连接字符串不正确。您在输入msql 而不是mysql 的连接字符串中有错字。连接字符串应如下所示:

jdbc:mysql://localhost:3306/FR

【讨论】:

    猜你喜欢
    • 2017-01-15
    • 1970-01-01
    • 2014-03-30
    • 2014-07-28
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    相关资源
    最近更新 更多