Tam*_*mer 92 mysql jdbc tomcat7 apache-commons-dbcp
我正在尝试创建与我的数据库的连接,当我使用main方法测试我的代码时,它可以无缝地工作.但是,当尝试通过Tomcat 7访问它时,它会失败并显示错误:
No suitable driver found for jdbc:mysql://localhost/dbname.
Run Code Online (Sandbox Code Playgroud)
我正在使用汇集.我在WEB-INF/lib和.classpath中放入了mysql连接器(5.1.15),dbcp(1.4)和pool(1.4.5)库.我正在使用Eclipse IDE.我的数据库驱动程序代码是:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.tomcat.dbcp.dbcp.ConnectionFactory;
import org.apache.tomcat.dbcp.dbcp.DriverManagerConnectionFactory;
import org.apache.tomcat.dbcp.dbcp.PoolableConnectionFactory;
import org.apache.tomcat.dbcp.dbcp.PoolingDriver;
import org.apache.tomcat.dbcp.pool.impl.GenericObjectPool;
public class DatabaseConnector {
public static String DB_URI = "jdbc:mysql://localhost/dbname";
public static String DB_USER = "test";
public static String DB_PASS = "password";
// Singleton instance
protected static DatabaseConnector _instance;
protected String _uri;
protected String _username;
protected String _password;
/**
* Singleton, so no public constructor
*/
protected DatabaseConnector(String uri, String username, String password) {
_uri = uri;
_username = username;
_password = password;
GenericObjectPool connectionPool = new GenericObjectPool(null);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
_uri, _username, _password);
PoolableConnectionFactory poolableConnectionFactory =
new PoolableConnectionFactory(connectionFactory, connectionPool,
null, null, false, true);
PoolingDriver driver = new PoolingDriver();
driver.registerPool("test", connectionPool);
}
/**
* Returns the singleton instance
*/
public static DatabaseConnector getInstance() {
if (_instance == null) {
_instance = new DatabaseConnector(DB_URI, DB_USER, DB_PASS);
}
return _instance;
}
/**
* Returns a connection to the database
*/
public Connection getConnection() {
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:apache:commons:dbcp:test");
} catch (SQLException e) {
throw new RuntimeException(e);
}
return con;
}
}
Run Code Online (Sandbox Code Playgroud)
我的堆栈跟踪开始:
Apr 5, 2011 9:49:14 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Login] in context with path [/Project]
threw exception
java.lang.RuntimeException: java.sql.SQLException:
No suitable driver found for jdbc:mysql://localhost/dbname
Run Code Online (Sandbox Code Playgroud)
导致此错误的原因是什么?
unc*_*ons 56
尝试将驱动程序jar放在服务器lib文件夹中.($ CATALINA_HOME/lib中)
我相信甚至在实例化应用程序之前就需要设置连接池.(至少这是它在Jboss中的工作方式)
Eri*_*ski 43
你得到这个错误的原因:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/dbname
Run Code Online (Sandbox Code Playgroud)
是因为你忘了用java应用程序注册你的mysql jdbc驱动程序.
这就是你写的:
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:apache:commons:dbcp:test");
} catch (SQLException e) {
throw new RuntimeException(e);
}
Run Code Online (Sandbox Code Playgroud)
应该是这样的:
Connection con = null;
try {
//registering the jdbc driver here, your string to use
//here depends on what driver you are using.
Class.forName("something.jdbc.driver.YourFubarDriver");
con = DriverManager.getConnection("jdbc:apache:commons:dbcp:test");
} catch (SQLException e) {
throw new RuntimeException(e);
}
Run Code Online (Sandbox Code Playgroud)
您必须阅读特定mysql jdbc驱动程序的手册,以找到放在Class.forName("...")参数中的确切字符串.
JDBC v.4不需要Class.forName
Class.forName("something.jdbc.driver.YourFubarDriver")如果使用最新的(JDBC v.4)驱动程序,则不再需要从Java 6开始.有关详细信息,请阅读:http://onjava.com/pub/a/onjava/2006/08/02/jjdbc-4-enhancements-in-java-se-6.html
ybe*_*ira 37
使用Tomcat7和mysql-connector-java-5.1.26我遇到了同样的问题,我把它放在我的$ CATALINA_HOME/lib和WEB-INF/lib中,以防万一.但是在得到连接之前我使用这两个语句中的任何一个之前它都找不到它:
DriverManager.registerDriver(new com.mysql.jdbc.Driver ());
要么
Class.forName("com.mysql.jdbc.Driver");
然后我从$ CATALINA_HOME/lib中删除mysql-connector-java-5.1.26,连接仍然有效.
小智 8
当运行tomcat out of eclipse时它不会选择lib设置CATALINA_HOME/lib,有两种方法可以修复它.在eclipse服务器视图中双击Tomcat服务器,它将打开tomcat插件配置,然后:
添加来自 Maven 的工件。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我在 $CATALINA_HOME/lib 和 WEB-INF/lib 中都有 mysql jdbc 库,但我仍然收到此错误。我需要 Class.forName("com.mysql.jdbc.Driver"); 使其发挥作用。
| 归档时间: |
|
| 查看次数: |
454893 次 |
| 最近记录: |