简体   繁体   English

无法在jsp文件中建立jdbc连接

[英]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: 嘿,我试图在我的jsp文件中建立简单的jdbc连接,该文件如下所示:

<%@ 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: 每当我尝试运行此代码时,都会在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. 最后一件事,当我尝试从一个简单的java文件中获得相同类型的连接时,此方法就起作用了。 so what's the problem with jsp file. 那么jsp文件有什么问题。

PS: I am working on Ubuntu 10.10 distro and with Eclipse 3.5. PS:我正在研究Ubuntu 10.10发行版和Eclipse 3.5。 And i have also added the jar file to the java build path of Project. 而且我还将jar文件添加到Project的java构建路径中。 And this is working just fine on windows. 这在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. 您需要将mysql驱动程序部署到servlet容器(Tomcat / Jetty),以便JSP可以找到它们。

But as @crafstman said in the comment, it is very bad practice to put DB related stuff in the display code. 但是,正如@crafstman在评论中说的那样,将与DB相关的内容放入显示代码中是非常不好的做法。 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 将您的MySQL driver放入tomcat/lib或您可以将MySQL驱动程序放入您的yourapplicationproject/WebContent/WEB-INF/lib

Putting the database connection into jsp is not good practice. 将数据库连接放入jsp中不是一个好习惯。 I will be better if you put the database connection in to Servlet code 如果您将数据库连接放入Servlet code我会更好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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