我无法在eclips中将mysql与jsp连接

时间:2016-02-18 13:59:25

标签: mysql jsp

我发布了我的代码以及错误和数据库名称记录。

我也完成了jdbc连接,仍然无法传递数据。我的代码或我的任何安装中都有错误吗?

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<%@ page import="java.sql.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>

</head>
<body>
<%
Connection con= null;
PreparedStatement ps = null;
ResultSet rs = null;

String url = "jdbc:mysql://localhost:3306/record";
String user = "root";
String password = "password";


try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("url", user, password);

String sql = "select usertype from userdetail";

ps = con.prepareStatement(sql);
rs = ps.executeQuery(); 


%>
<form method="post" action="login.jsp">
<center><h2 style="color:green">JSP Login Example</h2></center>
<table border="1" align="center">
<tr>
<td>Enter Your Name :</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>Enter Your Password :</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td>Select UserType</td>
<td><select name="usertype">
<option value="select">select</option>
<%
while(rs.next())
{
String usertype = rs.getString("usertype");
%>
<option value=<%=usertype%>><%=usertype%></option>
<% 
}
}
catch(SQLException sqe)
{
out.println("home"+sqe);
}
%>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="submit"/></td>
</table>
</form>
</body>
</html>

错误消息

HTTP Status 500 - An exception occurred processing JSP page /NewFile2.jsp at line 30

type Exception report

message An exception occurred processing JSP page /NewFile2.jsp at line 30

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /NewFile2.jsp at line 30

27: 
28: 
29: try {
30: Class.forName("com.mysql.jdbc.Driver");
31: con = DriverManager.getConnection("url", user, password);
32: 
33: String sql = "select usertype from userdetail";


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:412)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

root cause

javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:868)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:797)
    org.apache.jsp.NewFile2_jsp._jspService(NewFile2_jsp.java:143)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

root cause

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:128)
    org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:66)
    java.lang.Class.forName0(Native Method)
    java.lang.Class.forName(Unknown Source)
    org.apache.jsp.NewFile2_jsp._jspService(NewFile2_jsp.java:82)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:723)

注意Apache Tomcat / 6.0.43日志中提供了根本原因的完整堆栈跟踪。

1 个答案:

答案 0 :(得分:0)

尝试插入此内容:

DriverManager.registerDriver(new com.mysql.jdbc.Driver());

获取JDBC连接之前。