0
使用JDBC驅動程序管理器連接到MySQL DB時,連接成功並按預期檢索結果集。但是,試圖利用任何org.springframework.jdbc.datasource.DriverManagerDataSource或 org.springframework.jdbc.datasource.SimpleDriverDataSource通過Spring配置文件來連接時加載的JDBC MySQL驅動程序但未能通過Spring配置連接到MySQL
JDBC驅動程序com.mysql.jdbc.Driver被加載,但不能使與MySQL的連接。
測試應用等級: package 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)
你錯過這裏Y:'JDBC:msql' – Jens
@Jens什麼小姐,對不起,聽不懂 – user2166213
的jdbc:mSQL的必須爲jdbc:M ** Y * * SQL:value =「jdbc:msql:// localhost:3306/FR」 - > value =「jdbc:m ** y ** sql:// localhost:3306/FR」 – Jens