1

感谢您阅读此问题。

下面的代码

我正在尝试让 jsp 使用数据库。该数据库目前已部分填充,我正在尝试将此信息返回到我的浏览器并打印出来。

不幸的是,当我尝试这个时,我得到一个错误,说编译器在我尝试在 newjsp.jsp 中调用 getStudentList() 的行上找不到符号“studentData”。

我已经从朋友的项目中复制了 newjsp 中的代码,它工作正常。

我希望你能帮忙!

谢谢,

植物182

** * ** * ** * StudentBean.java * ** * *

         package my.beans;


         public class StudentBean {


                 private String firstName;
                 private String lastName;
                 private String comment;
                 private String email;

                 public StudentBean(){

                 }

                 // get/set for First Name
                 public void setFirstName(String firstName) {
                         this.firstName = firstName;
                 }
                 public String getFirstName(){
                         return firstName;
                 }

                 // get/set for Last Name
                 public void setLastName(String lastName) {
                         this.lastName = lastName;
                 }
                 public String getLastName(){
                         return lastName;
                 }

                 // get/set for comment
                 public void setComment(String comment) {
                         this.comment = comment;
                 }
                 public String getComment(){
                         return comment;
                 }

                 // get/set for Email
                 public void setEmail(String email) {
                         this.email = email;
                 }
                 public String getEmail(){
                         return email;
                 }
         }

* ** * StudentDataBean.java * ** * ****

         package my.beans;

         import java.sql.SQLException;
         import javax.sql.rowset.CachedRowSet;
         import java.util.ArrayList;
         import com.sun.rowset.CachedRowSetImpl; // CachedRowSet implementation

         public class StudentDataBean {

            private CachedRowSet rowSet;

            // construct TitlesBean object
            public StudentDataBean() throws Exception
            {
               // load the MySQL driver
               Class.forName( "com.mysql.jdbc.Driver" );

               // specify properties of CachedRowSet
               rowSet = new CachedRowSetImpl();
               rowSet.setUrl( "jdbc:mysql://localhost:3306/test" );
               rowSet.setUsername( "root" );
               rowSet.setPassword( "" );

                   // obtain list of titles
               rowSet.setCommand(
                  "SELECT firstName, lastName, email, comment FROM guests" );
               rowSet.execute();
            } // end StudentDataBean constructor

            // return an ArrayList of StudnetBeans
            public ArrayList<StudentBean> getStudentList() throws SQLException
            {
               ArrayList<StudentBean> studentList = new ArrayList<StudentBean>();

               rowSet.beforeFirst(); // move cursor before the first row

               // get row data
               while ( rowSet.next() )
               {
                  StudentBean student = new StudentBean();

                  student.setFirstName( rowSet.getString( 1 ) );
                  student.setLastName( rowSet.getString( 2 ) );
                  student.setEmail( rowSet.getString( 3 ) );
                  student.setComment( rowSet.getString( 4 ) );
                  studentList.add( student );
               } // end while

               return studentList;
            } // end method getStudentList

            // insert a Student in student database
         public void addStudent( StudentBean student ) throws SQLException
         {
         rowSet.moveToInsertRow(); // move cursor to the insert row

          // update the three columns of the insert row
         rowSet.updateString( 1, student.getFirstName() );
         rowSet.updateString( 2, student.getLastName() );
         rowSet.updateString( 3, student.getEmail() );
         rowSet.updateString( 4, student.getComment() );
         rowSet.insertRow(); // insert row to rowSet
         rowSet.moveToCurrentRow(); // move cursor to the current row
         rowSet.acceptChanges(); // propagate changes to database
         } // end method addStudent
    } // end class StudentDataBean

** * ** * newjsp.jsp * ** * ** * ** * *

   <%@page import="java.util.ArrayList"%>
   <%@page contentType="text/html" pageEncoding="UTF-8"%>
   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

   <%@ page import="java.util.*" %>
   <%@ page import="my.beans.StudentDataBean"%>
   <%@ page import="my.beans.StudentBean"%>
   <jsp:useBean id="studentData" scope="request" class="my.beans.StudentDataBean"/>
   <html>
     <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
         <title>Words View</title>
           <style type="text/css">
           table, tr, td, th
           {
                text-algn: center;
                font-size: .9em;
                border: 3px groove;
                padding: 3px;
                background-color: #eee9e9;
           }
     </style>
     </head>
     <body>
     <h1>Student List</h1>
     <table border="1">
        <tr>
            <th>
                <h4>Email</h4>
            </th>
            </tr>
                <%
                    ArrayList<StudentBean> studentList = studentData.getStudentList();
                    Iterator studentListIterator = studentList.iterator();
                    StudentBean student;

                    while (studentListIterator.hasNext()){
                        student = (StudentBean) studentListIterator.next();
                %>
            <tr>
                <td><%= student.getEmail()%></td>

            </tr>
            <% }
            %>
     </table>
     </body>
     </html>
4

1 回答 1

1

可能是ide错误,重启netbeans。(如果你使用的是 netbeans……否则天知道。)爱汤姆 xx

于 2012-04-28T16:32:48.410 回答