1

I want to try to do a database connection with the JDBC in Java.I write my codes on the Eclipse.But in my project the JDBC connection can not established with the mysql JDBC connector jar file.I added the jar file in build path and WEB/lib folder.I try to do all the ways but still my problem is the same.

My Database Connection class is ;

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;
}
}

My stack trace is;

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)
  • 1
    It's unclear how your app works. It's deployed on a Tomcat server? Standalone or embedded? As war file? – UninformedUser Nov 25 '18 at 18:20
  • 1
    Can you try it with maven? Add maven as framework support and add maven dependencies for mysql connector. – Emre Savcı Nov 25 '18 at 18:59
  • 1) According to the error your code doesn't handle certain exception and therefor wasn't compiled: fix that (or make sure you recompile, as your code does seem to handle them), 2) using `System.exit(..)` in a web application is a **bad** idea, 3) you shouldn't get connections this way in a web application: using a (connection pooling) datasource, 4) there is no reason what so ever to invoke `newInstance()` on the driver class (unless you use an extremely old and buggy version of MySQL Connector/J). – Mark Rotteveel Nov 26 '18 at 12:35
  • @EmreSavcı Suggesting Maven is irrelevant for the error shown. – Mark Rotteveel Nov 26 '18 at 12:37
  • @AKSW no it is a jar file and it is embedded. – İbrahim Uzuğ Nov 27 '18 at 21:55
  • @Mark Rotteveel @ Emre Savcı Thanks for your replies and I fixed the problem.The problem is because of the buggy version of an Eclipse.I deleted the project and re installed the Eclipse after that the Eclipse saw the *jar files and indexed.Thanks for your attention. – İbrahim Uzuğ Nov 27 '18 at 21:58

0 Answers0