向大神们求助
最近在学习Servlet做了一个简单的登录页面 顺便练习了一下DBCP连接池 html 页面跳转到Servlet时控制台报 Cannot create JDBC driver of class '' for connect URL 'jdbc:mysql://localhost:3306/bookstore' 错误
org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'jdbc:mysql://localhost:3306/bookstore' at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452) at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371) at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044) at org.apache.commons.dbutils.AbstractQueryRunner.prepareConnection(AbstractQueryRunner.java:156) at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:285) at com.study.servlet.LoginServlet.doGet(LoginServlet.java:43) at javax.servlet.http.HttpServlet.service(HttpServlet.java:618) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659) at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source) Caused by: java.sql.SQLException: No suitable driver at java.sql.DriverManager.getDriver(Unknown Source) at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437) ... 29 more
1首先我先看了项目的 Libraries ,包导入了
2.以为是jar包版本问题又试了 驱动 5.08 和5.1.39 版本的 还是出现这个问题
3.为了验证代码问题 我又把主要的三行代码放在了main方法中运行 ,居然没出错,还 查出了sql结果集 :main方法代码如下:
public static void main(String[] args) { DataSource dataSource=DBCPUtils.getDataSource(); System.out.println(dataSource); QueryRunner qr=new QueryRunner(dataSource); //查询是否有改用户 String userName="张三"; String password="123456"; String sql="select * from user where userName=? and password=?"; Object [] params={userName,password}; try { Object[] list = qr.query(sql, new ArrayHandler() , params); if (list!=null) { //服务器行为 System.out.println("存在"); }else { System.out.println("不存在"); } } catch (SQLException e) { e.printStackTrace(); } }
4我放弃了用DBCP 又试了C3P0连接池 没有报错 ,SQL 语句正常执行
具体代码——以下
Servlet方法:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //连接数据库,执行SQL语句 //这里使用DBUtils操作数据库 DataSource dataSource=DBCPUtils.getDataSource(); System.out.println(dataSource); QueryRunner qr=new QueryRunner(dataSource); //创建输出流 PrintWriter out=response.getWriter(); request.setCharacterEncoding("utf-8"); String userName=request.getParameter("userName"); String password=request.getParameter("password"); //查询是否有该用户 String sql="select * from user where userName=? and password=?"; // Object [] params={userName,password}; try { User u = qr.query(sql,new BeanHandler<User>(User.class),userName,password); if (u!=null) { //服务器行为 request.getRequestDispatcher("/WEB13/html/Ok.html"); }else { response.sendRedirect("/WEB13/html/No.html"); } } catch (SQLException e) { e.printStackTrace(); } }
DBCPUtils代码:
public class DBCPUtils { private static DataSource dataSource; private static Connection conn; static{ try { InputStream is = DBCPUtils.class.getClassLoader().getResourceAsStream("db.properties"); Properties props=new Properties(); props.load(is); dataSource=BasicDataSourceFactory.createDataSource(props); } catch (Exception e) { e.printStackTrace(); } } public static DataSource getDataSource(){ return dataSource; } public static Connection getConnection(){ try { return dataSource.getConnection(); } catch (SQLException e) { throw new RuntimeException(e); } } }
db.properties (src路径下)
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/bookstore username=root password=root
问了别人 给我推荐了一个博客 http://blog.sina.com.cn/s/blog_e1235f680101fl9w.html
试了还是不行
BasicDataSourceFactory查找的配置文件中的数据库驱动字段应该是driverClassName
public class BasicDataSourceFactory implements ObjectFactory {
private final static String PROP_DRIVERCLASSNAME = "driverClassName";
大神看了你的建议 我把db.properties 中的driver 改成了driverClassName 运行成功没有报错
我有个疑问 就是配置文件的字段没有改动前把 下面的三句代码放到main方法测试 为什么没有出现org.apache.commons.dbcp.SQLNestedException 异常 还能查出结果集
1 DataSource dataSource=DBCPUtils.getDataSource(); 2 QueryRunner qr=new QueryRunner(dataSource); 3 Object[] list = qr.query(sql, new ArrayHandler() , params);
@欢呀:
// Create a JDBC driver instance
Driver driver = null;
try {
if (driverFromCCL == null) {
driver = DriverManager.getDriver(url);
} else {
// Usage of DriverManager is not possible, as it does not
// respect the ContextClassLoader
driver = (Driver) driverFromCCL.newInstance();
if (!driver.acceptsURL(url)) {
throw new SQLException("No suitable driver", "08001");
}
}
}
这段代码的意思是可以根据连接串url找驱动,url中指定了mysql
@faramita2016: 谢谢。看了你发的代码,我果断看了BasicDataSource源代码 ,感觉受益匪浅。
@欢呀: 客气,我也新手