【问题标题】:Unable to compile class for JSP error tomcat,mysql无法编译 JSP 错误 tomcat、mysql 的类
【发布时间】:2016-12-22 07:44:05
【问题描述】:

我正在尝试将我的 jsp 项目上传到 tomcat。

下图是我的jsp项目的结构。

当在 insert.html 的函数中尝试使用 insert.jsp 时,出现错误。

此代码只是向 mysql 服务器插入数据。

DBConnection.java:

package com.exam;

import java.sql.*;

public class DBConnection {

    public static Connection getCon() throws SQLException {
        // TODO Auto-generated method stub

        Connection con = null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/testdb";
            con=DriverManager.getConnection("url", "root", "kcclab");
            System.out.println("DB 연결 완료");
            con.close();
            return con;
        }
        catch(ClassNotFoundException cnfe){
            System.out.println("연결이 안되네요..."+cnfe.getMessage());
            return null;
        } 

    }

}

插入.jsp:

<%@page import="java.sql.SQLException" %>
<%@page import="java.sql.PreparedStatement" %>
<%@page language="java" contentType="text/html; charset=euc-kr"
pageEncoding="euc-kr" %>
<%@page import="java.sql.Connection" %>
<%@page import="com.exam.DBConnection" %>
<!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=euc-kr">
<title>Insert title here</title>
</head>
<body>
<%
    request.setCharacterEncoding("euc-kr");
    String id=request.getParameter("ID");
    String pwd=request.getParameter("pwd");

    Connection con=null;
    PreparedStatement pstmt=null;
    String sql="insert into members vlaues(?,?,sysdate)";
    int n=0;

    try{
        con=DBConnection.getCon();
        pstmt=con.prepareStatement(sql);
        pstmt.setString(1, id);
        pstmt.setString(2, pwd);

        n=pstmt.executeUpdate();
    }
    catch(SQLException se){
        System.out.println(se.getMessage());
    }
    finally{
        try{
            if(pstmt!=null) pstmt.close();
            if(con!=null) con.close();
        }
        catch(SQLException se){
            System.out.println(se.getMessage());
        }
    }
%>
<script type="text/javascript">
    if(<%=n%>>0){
        alert("���������� ȸ�����ԵǾ����ϴ�.");
        location.href="../index.html";
    }
    else{
        alert("ȸ�����Կ� �����߽��ϴ�.");
        history.go(-1);
    }
</script>
</body>
</html>

插入.html

<!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=euc-kr">
<title>Insert title here</title>
<style type="text/css">
    #regbox{width:300px;}
    #regbox label{display:block;    width:100px;    float:left; }
</style>
</head>
<body>
<form method="post" action="insert.jsp">
    <fieldset id="regbox">
        <legend>ȸ������</legend>
        <label for="id">���̵�</label>
        <input type="text" name="id"/><br/>
        <label for="pwd">��й�ȣ</label>
        <input type="password" name="pwd"/><br/>
        <input type="submit" value="����">
        <input type="reset" value="���"/>
    </fieldset>
</form>
</body>
</html>

我怎样才能避免这个错误?

【问题讨论】:

  • 你确定com.exam.DBConnection 在类路径中吗?

标签: java mysql jsp tomcat servlets


【解决方案1】:

据我了解,当您尝试导入类路径下不存在的类时,可能会发生这种情况。 你能看看你的tomcat webapps 目录吗? DBConnection.class 文件可能不存在于 WEB-INF/classes 文件夹下。

【讨论】:

    猜你喜欢
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    相关资源
    最近更新 更多