java.sql.SQLException:找不到合适的jdbc驱动程序:h2:tcp:// localhost /〜/ ZadatakDB当我尝试使用java Web应用程序连接到h2数据库时抛出.
我可以毫无问题地使用H2控制台,ping是成功的.
我还将h2-1.3.176 jar文件添加到Libraries AND到WEB-INF / lib.
这是我用来连接的Java方法:
private static Connection connectToDatabase() throws SQLException,
IOException {
String url = "jdbc:h2:tcp://localhost/~/ZadatakDB";
String username = "student";
String pw = "student";
Connection connection = DriverManager.getConnection(url,
username, pw);
return connection;
}
我能错过什么?
我还将添加StackTrace:
java.sql.SQLException: No suitable driver found for jdbc:h2:tcp://localhost/~/ZadatakDB
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at hr.web.aplikacija.DatabaseUtils.connectToDatabase(DatabaseUtils.java:20)
at hr.web.aplikacija.DatabaseUtils.insertForm(DatabaseUtils.java:38)
at hr.web.aplikacije.servlet.KorisnikServlet.doPost(KorisnikServlet.java:68)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
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:502)
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:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1517)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1474)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
解决方法:
H2数据库的JDBC驱动程序不在类路径中,或者驱动程序已卸载.使用它来加载驱动程序类:
Class.forName("org.h2.Driver");