Yet another jdbc:mysql no suitable driver, Spring+Hibernate [duplicate]
I'm trying to get a MySQL database connection running in a Spring/Hibernate project in NetBeans. However, I always get a
java.sql.SQLException: No suitable driver found for "jdbc:mysql://127.0.0.1/test"
exception when deploying. None of the solutions I've read so far solve my problem.
applicationContext.xml:
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="${database.url}"
p:username="${database.username}"
p:password="${database.password}" />
开发者_StackOverflow中文版
where database.* is stored in a properties file. The jdbc connector .jar file is listed in libraries (ie. the classpath is set). When I expand it, com.mysql.jdbc.Driver is listed.
I have tried the natively installed connector and downloaded the newest version I could find (5.1.15). I can connect a jdbc connector that I have configured earlier using the same driver by clicking "connect" within the serviced tab in NetBeans. When using this sample code from outside the IDE, it also works:
import java.sql.*;
import java.util.Properties;
public class DBDemo
{
private static final String dbClassName = "com.mysql.jdbc.Driver";
private static final String CONNECTION = "jdbc:mysql://127.0.0.1/test";
public static void main(String[] args) throws ClassNotFoundException
{
System.out.println(dbClassName);
Class.forName(dbClassName);
Properties p = new Properties();
p.put("user","root");
p.put("password","root");
Connection c = DriverManager.getConnection(CONNECTION,p);
System.out.println("It works !");
c.close();
}
}
Any ideas on what I could be doing wrong?
If the driver was missing, you'd rather have seen a ClassNotFoundException
.
This particular one means that no one of the loaded drivers accepts the given URL. I'd say, those quotes look suspicious, they normally don't appear in this exception. Where does ${database.url}
come from? Does it contain quotes? Get rid of them.
Have you deployed your application on web-container ? If yes, then are you sure that mysql db driver is packaged within WEB-INF/lib directory (in deployment).
The expanded jar that you see under library in Netbeans might not mean it would be able to your web-app on deployment site.
精彩评论