javabean,提示 The method is undefined for the type StudentService

继续一个关于javabean的问题。我已经定义了getStuByName(String)方法没错,
但却提示错误:求助各路英雄啊~!!!/(ㄒoㄒ)/
PS:我用getStuByid()方法是没出错的~~
org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 25 in the jsp file: /showstubyname.jsp
The method getStuByName(String) is undefined for the type StudentService
图片描述

以下是具体代码
StudentService.java
图片描述
整个StudentService类代码
package cn.ac.ucas.service;

import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.ac.ucas.conn.Conn;
import cn.ac.ucas.model.Student;

public class StudentService {

private Connection conn;
private PreparedStatement pstmt;

public StudentService() {
    conn = new Conn().getConn();

}

public boolean addStu(Student student) {
    try {
        pstmt = conn.prepareStatement(
                "insert into Student(nickname,name,gender,birth,majority,course,interest,otherinfo)"
                        + "values(?,?,?,?,?,?,?,?)");
        pstmt.setString(1, student.getNickname());
        pstmt.setString(2, student.getName());
        pstmt.setByte(3, student.getGender());
        pstmt.setString(4, student.getBirth());
        pstmt.setString(5, student.getMajority());
        pstmt.setString(6, student.getCourses());
        pstmt.setString(7, student.getInterests());
        pstmt.setString(8, student.getOtherinfo());
        pstmt.executeUpdate();
        return true;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    }
}

public boolean updateStu(Student student){
    
    try {
        pstmt=conn.prepareStatement("update Student set nickname=?,name=?,gender=?,birth=?,majority=?,course=?,interest=?,otherinfo=? where id=?");
        pstmt.setString(1, student.getNickname());
        pstmt.setString(2, student.getName());
        pstmt.setByte(3, student.getGender());
        pstmt.setString(4, student.getBirth());
        pstmt.setString(5, student.getMajority());
        pstmt.setString(6, student.getCourses());
        pstmt.setString(7, student.getInterests());
        pstmt.setString(8, student.getOtherinfo());
        pstmt.setInt(9, student.getId());
        
        pstmt.executeUpdate();
        return true;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    }
}

public List getAllStudent() {
    List stuList = new ArrayList();
    try {
        pstmt = conn.prepareStatement("select * from Student");
        ResultSet rst = pstmt.executeQuery();
        while (rst.next()) {
            Student stu = new Student();
            stu.setId(rst.getInt(1));
            stu.setNickname(rst.getString(2));
            stu.setName(rst.getString(3));
            stu.setGender(rst.getByte(4));
            stu.setBirth(rst.getString(5));

            stu.setMajority(rst.getString(6));
            if (rst.getString(7) != null) {
                stu.setCourse(rst.getString(7).split("&&"));
            }
            if (rst.getString(8) != null) {
                stu.setInterest(rst.getString(8).split("&&"));
            }

            stu.setOtherinfo(rst.getString(9));

            stuList.add(stu);

        }

        return stuList;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}

public boolean deleteStu(int id) {

    try {
        pstmt = conn.prepareStatement("delete from Student where id=?");
        pstmt.setInt(1, id);
        pstmt.executeUpdate();
        return true;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    }

}

public Student getStuById(int id) {
    try {
        pstmt = conn.prepareStatement("select * from Student where id = ?");
        pstmt.setInt(1, id);
        ResultSet rst = pstmt.executeQuery();
        
        if(rst.next()){
            Student stu=new Student();
            stu.setId(rst.getInt(1));
            stu.setNickname(rst.getString(2));
            stu.setName(rst.getString(3));
            stu.setGender(rst.getByte(4));
            stu.setBirth(rst.getString(5));

            stu.setMajority(rst.getString(6));
            if (rst.getString(7) != null) {
                stu.setCourse(rst.getString(7).split("&&"));
            }
            if (rst.getString(8) != null) {
                stu.setInterest(rst.getString(8).split("&&"));
            }

            stu.setOtherinfo(rst.getString(9));
            
            return stu;
        }
        
        return null;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

}

public Student getStuByName(String name) {
    try {
        pstmt = conn.prepareStatement("select * from student where name = ?");
        pstmt.setString(1,name);
        ResultSet rst = pstmt.executeQuery();
        
        if(rst.next()){
            Student stu=new Student();
            stu.setId(rst.getInt(1));
            stu.setNickname(rst.getString(2));
            stu.setName(rst.getString(3));
            stu.setGender(rst.getByte(4));
            stu.setBirth(rst.getString(5));

            stu.setMajority(rst.getString(6));
            if (rst.getString(7) != null) {
                stu.setCourse(rst.getString(7).split("&&"));
            }
            if (rst.getString(8) != null) {
                stu.setInterest(rst.getString(8).split("&&"));
            }

            stu.setOtherinfo(rst.getString(9));
            
            return stu;
        }
        
        return null;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

}

}

selectstubyname.jsp

图片描述

showstubyname.jsp
<%@page import="cn.ac.ucas.model.*"%>
<%@page import="cn.ac.ucas.service.*"%>
<%@page import="java.util.*"%>
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查看学生信息</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<style type="text/css">
body {

width: 800px;

}
</style>
</head>
<jsp:include page="checklogin.jsp"></jsp:include>
<body>

<h1 class="stu_titles">查看学生信息</h1>
<jsp:useBean id="stus" class="cn.ac.ucas.service.StudentService"></jsp:useBean>
  <% 
         Student stu = stus.getStuByName(request.getParameter("name"));
         if(stu!=null)
         {
      %>
<div class="showstu">
    <table>
        <tr>
            <td>id</td>
            <td>昵称</td>
            <td>姓名</td>
            <td>性别</td>
            <td>出生日期</td>
            <td>专业</td>
            <td>选择课程</td>
            <td>兴趣爱好</td>
            <td>备注</td>
            <td>修改</td>
            <td>删除</td>
        </tr>
        
        <tr>
            <td><%=stu.getId()%></td>
            <td><%=stu.getNickname()%></td>
            <td><%=stu.getName()%></td>
            <td>
                <%
                    if (stu.getGender() == 1) {
                            out.println("男");
                        } else {
                            out.println("女");
                        }
                %>
            </td>
            <td><%=stu.getBirth()%></td>
            <td><%=stu.getMajority()%></td>
            <td><%=stu.getCourses()%></td>
            <td><%=stu.getInterests()%></td>
            <td><%=stu.getOtherinfo()%></td>
            <td><a href="updatestu.jsp?id=<%=stu.getId()%>">修改</a></td>
            <td><a href="deletestu.jsp?id=<%=stu.getId()%>">删除</a></td> 
        <tr>
        
    </table>
  <%
 }
  %>
</div>

</body>
</html>

**PS:
我用getStuById()是没出错的!**
具体效果图如下~
图片描述

图片描述

阅读 10.1k
1 个回答

怀疑是不是Eclipse没有编译你的代码,你可以试试清空编译的classes 后重新编译一下。另外不建议在 jsp 页面中写业务逻辑相关的 java 代码

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏