我想尝试用Java中的JDBC建立数据库连接。我在Eclipse上编写代码。但是在我的项目中,无法使用mysql JDBC连接器jar文件建立JDBC连接。构建路径和WEB / lib文件夹。我尝试了所有方法,但仍然是我的问题。
我的数据库连接类为;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
public class DB {
public static Connection getCon(){
Connection con = null ;
try
{
Class.forName("com.mysql.jdbc.Connection").newInstance();
con=(Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/
library","root","123456");
}
catch(ClassNotFoundException e)
{
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
catch(IllegalAccessException e)
{
System.out.println("Error: access problem while loading!");
System.exit(2);
}
catch(InstantiationException e)
{
System.out.println("Error: unable to instantiate driver!");
System.exit(3);
}
catch (SQLException e)
{
e.printStackTrace();
}
return con;
}
}
我的堆栈跟踪是;
java.lang.Error: Unresolved compilation problems:
Unhandled exception type ClassNotFoundException
Unhandled exception type InstantiationException
Unhandled exception type IllegalAccessException
at com.eLibrary.dao.DB.getCon(DB.java:12)
at com.eLibrary.dao.LibrarianDao.view(LibrarianDao.java:49)
at com.eLibrary.servlets.ViewLibrarian.doGet(ViewLibrarian.java:34)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:231)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:193)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:491)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:764)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1388)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
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)