简体   繁体   中英

unable to have jdbc connection in jsp file

hey all i am trying to have simple jdbc connection in my jsp file the file is as under:

<%@ 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">

<%@page import="java.sql.DriverManager"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello world.
<% 

try{
    Class.forName("com.mysql.jdbc.Driver");
    DriverManager.getConnection("jdbc:mysql://localhost:3306/saurabh","root","");
    System.out.println("Connection successful");
}catch(Exception e){
    e.printStackTrace();
}

%>
</body>
</html>

whenever i try to run this code i get following console output in eclipse:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
    at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:128)
    at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:66)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at org.apache.jsp.myJspDemo_jsp._jspService(myJspDemo_jsp.java:68)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)

one last thing when i tried to have the same kind of connection from a simple java file then this was working. so what's the problem with jsp file.

PS: I am working on Ubuntu 10.10 distro and with Eclipse 3.5. And i have also added the jar file to the java build path of Project. And this is working just fine on windows.

So what to do??

Thanks in Advance :)

You need to deploy the mysql driver to your servlet container (Tomcat / Jetty ) so that the JSPs can find them.

But as @crafstman said in the comment, it is very bad practice to put DB related stuff in the display code. Be it for security reasons, be it for performance.

在将MySQL驱动程序JAR部署到服务器之前,应将其放置在项目的WEB-INF/lib目录中。

Put your MySQL driver to tomcat/lib or You can put the MySQL driver to yourapplicationproject/WebContent/WEB-INF/lib

Putting the database connection into jsp is not good practice. I will be better if you put the database connection in to Servlet code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM