您的位置:首页 > 产品设计 > UI/UE

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/

2016-05-31 01:39 961 查看
以前很少在eclipse中运行项目,昨天想跑一跑jetty+nginx一路报错,试试在tomcat中正常不,所以在eclipse中跑一个示例.

五月 31, 2016 12:12:10 上午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-8080"]
五月 31, 2016 12:12:10 上午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-nio-8009"]
五月 31, 2016 12:12:10 上午 org.apache.catalina.startup.Catalina start
信息: Server startup in 3828 ms
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/apo_htage
at java.sql.DriverManager.getConnection(DriverManager.java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
...
at java.lang.Thread.run(Thread.java:745)


驱动没加载?进行以下检查:

1.代码是否少了Class.forName

private final static String URL="jdbc:mysql://localhost:3306/databaseName" ;
private final static String UN="root";
private final static String PS="root";

public static Connection getInstance(){
Connection conn=null;
try {
//default:
conn=DriverManager.getConnection(URL,UN,PS);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}


2.项目中Libraries中是否有mysql.jar,主要检查

Apache Tomcat vX[Apache Tomcat]

Web App Libraries



如果少了某一个在项目右键菜单中Build Path > Configure Build Path > 右侧的Add Library



Server Runtime是添加Tomcat的

Web App Libraries是添加项目WEB-INF/lib下的

刚才我试了一下,只要以上都无误不用把mysql.jar 添加到Server中的Classpath中也不会报

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/


我昨天之所以产生此异常是因为我的java源码少了Class.forName,添加后如下

private final static String URL="jdbc:mysql://localhost:3306/databaseName" ;
private final static String UN="root";
private final static String PS="root";
static {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getInstance(){
Connection conn=null;
try {
//default:
conn=DriverManager.getConnection(URL,UN,PS);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}


如果以上都没问题?

1.java源码中有Class.forName

2.项目的Libraries中可以找到mysql.jar

3.连接的地址写的无误

还是报该异常就往下看! @20160531

@Since 20160530

我在tomcat中的lib放了mysql.jar了,一想现在用的是eclipse集成的tomcat7



知道问题了,eclipse集成的tomcat在哪呢?



右键打开菜单,open或者按F3



Server Locations是disabled,点机:open launch configuration



Arguments标签页中的VM arguments中已经可以看到全路径了。打开



没有lib文件夹,只能还回到Edit Configuration配置窗口中,点开Classpath标签页



选中Bootstrap Entries后,点机右侧的Add External Jars找到你本地的mysql.jar后添加即可。图中显示我已经加完.

再运行你的项目,就不会报:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/


当然前提是你的jdbc 驱动url写的没有错误.白渡也有说放到\jre\lib\ext目录下,个人不推荐!不能为了一个小错误破坏整个布署环境.如果不在eclipse中运行项目,直接在本机的tomcat中运行是不会出这种错误的.不论mysql.jar是放到tomcat/lib还是项目的WEB-INF\lib下都不会报

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql jdbc tomcat eclipse web