0

I am new to Java Web Development and recently I started a course and I have for an assignment to connect to an MySQL database and display it. I have Eclipse Kepler with Java 7.55 and as server I have GlassFish 4.0 and MySQL 6.0.9. I have the jdbc connector added to the project (WEB-INF/lib) and I also have the connector to the JRE System Library and Web App Libraries.

I have another dynamic web project I made with File Upload and there I was able to add the different external jar files as mentioned above and it all worked fine. I did the same for my below issue, but I still get an error. I also tried re-installing Eclipse and the server and the MySQL jdbc connector, but without any help. I deleted the project and re-build it, but still nothing.

The jdbc connector was added anywhere in every library I can think of adding it.

I have created a new project so that I am able to connect to MySQL Database

But when I run my below code(from the .jsp file) I get the error:

> HTTP Status 500 - Internal Server Error
> 
> --------------------------------------------------------------------------------
> 
> type Exception report
> 
> messageInternal Server Error
> 
> description The server encountered an internal error that prevented it from fulfilling this request.
> 
> exception 
> javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
> 
> root cause 
> java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
> 


And in the console I get this description:

*2014-05-13T21:12:13.195+0300|Warning: StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1761)
    at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1611)
    at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:187)
    at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:125)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:190)
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:52)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:744)*

The .jsp code is:

<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<% 
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/users", "root", "emperor05");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page - Database Connection</title>
</head>
<body>
    <h2>Database Connection</h2>
</body>
</html>

Sorry if it is a repost of a question, but I was not able to find the right answer?

Help?

Thank you in advance.

Tanata05
  • 39
  • 2
  • 8
  • Try putting that JAR in the Glassfish server /lib rather than your project WEB-INF/lib. Perhaps the Java EE class loader needs to see it earlier to create the pool. – duffymo May 13 '14 at 18:33
  • Your answer helped me. @duffymo . Do you know how to mark your comment as an accepted answer ? – Tanata05 May 13 '14 at 18:48
  • Nope, not possible. You can only accept answers. I'll make it an answer if you wish. – duffymo May 13 '14 at 19:14
  • possible duplicate of [ClassNotFoundException: com.mysql.jdbc.Driver. JDBC MySQL driver for web application](http://stackoverflow.com/questions/8348000/classnotfoundexception-com-mysql-jdbc-driver-jdbc-mysql-driver-for-web-applica) – Mark Rotteveel May 14 '14 at 12:08
  • The normal way of using JDBC in Java EE is to define a datasource in the Java EE server and reference that using JNDI or dependency injection to obtain the connection. – Mark Rotteveel May 14 '14 at 12:09

1 Answers1

1

Try putting that JAR in the Glassfish server /lib rather than your project WEB-INF/lib. Perhaps the Java EE class loader needs to see it earlier to create the pool.

Priyantha
  • 4,839
  • 6
  • 26
  • 46
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 1
    duffymo, please note that tanata05 is creating their connection straight into their JSP page, without using any connection pool. Including the driver into their WEB-INF/lib should have been enough, maybe Tanata05 had any problem with their .war packaging? – Jorge_B May 14 '14 at 09:57
  • 1
    I didn't notice that. I'd recommend against that. Scriptlet code should not be put in JSPs. And connections ought to be pooled and managed by the app server. – duffymo May 14 '14 at 11:51
  • @Jorge_B I added the mysql connector in the glassfish lib and modules folders and after that when I refreshed the project it was running succesfully. – Tanata05 May 14 '14 at 16:48