数据不是在jsp中插入数据库而while循环不在程序中执行

时间:2014-04-08 05:25:12

标签: java mysql jsp

我有问题while while我在数据库中插入数据,但它不在数据库中。并且页面在第一页的成功页面上重定向所有东西填写后显示学生的注册号然后选择缺席或出现然后提交而不是数据存储在数据库中我也尝试在控制台屏幕上打印数据它上升到外部但是在里面不去..请帮助解决这个问题。

attendance.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<%@page language="java" import="java.sql.*"%>
<%@page import="com.connection.*"%>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title>Towards Excellence</title>
    <link rel="stylesheet" href="css/userstyle.css" type="text/css" media="all" />
    <script src="js/droplist.js" type="text/javascript"></script>
</head>
<body>
<!-- Shell -->
<div id="shell">

    <!-- Header -->
    <div id="header">

        <div class="right">
            <p>Welcome <a href="profilef.jsp"><strong><%=session.getAttribute("usertype")%></strong></a></p>
            <p class="small-nav"><a href="index.jsp">view website</a> / <a href="logout.jsp">Logout</a></p>
        </div>
    </div>
    <!-- End Header -->

    <!-- Navigation -->
    <div id="navigation">
        <ul>
            <li><a href="#">Attendance</a>
                <ul>
                    <li><a href="attendance.jsp">Take Attendance</a></li>
                    <li><a href="viewattendancef.jsp">View Attendance</a></li>
                </ul>
            </li>
            <li><a href="#">Profile</a>
                <ul>
                    <li><a href="profilef.jsp">View Profile</a></li>
                    <li><a href="editprofilef.jsp">Edit Profile</a></li>
                </ul>
            </li>
            <li><a href="viewstudent.jsp">View Student</a></li>
            <li><a href="#">Examination</a>
                <ul>
                    <li><a href="examination.jsp">View Examination</a></li>
                    <li><a href="addexam.jsp">Add Exam details</a></li>
                </ul>
            </li>
            <li><a href="#">Schedule</a>
                <ul>
                    <li><a href="circulars.jsp">View Schedule</a></li>
                    <li><a href="addschedule.jsp">Add Schedule</a></li>
                </ul>
            </li>
            <li><a href="sms.jsp">Send SMS</a></li>
        </ul>
    </div>
    <!-- End Navigation -->
    <!-- Content -->
    <div id="content">
     <div>
     <form action="attendance" method="post">
        <table>
            <tr>
            <td>Date:</td>
            <td>
            <input type="date" name="adate" required/>
            </td>
            </tr>
            <tr>
            <td>Branch:</td>
            <td>
            <select name="branch" id="branch" onchange="setSem();">
              <option value="Computer Engineering">Computer</option>
              <option value="Information Technology">Information Technology</option>
              <option value="Mechanical Engineering">Mechanical</option>
              <option value="Civil Engineering">civil</option>
            </select>
            </td>
            </tr><tr>
            <td>Semester:</td>
            <td>
            <select name="semester" id="semester" onchange="setSubject();">
              <option value="">Select Semester</option>
            </select>
            </td>
            </tr><tr>
            <td>Subject:</td>
            <td>
            <select name="subject"  id="subject">
              <option value="">Select Subject</option>
            </select>
            </td>
            </tr>
        </table>
        <input class="submit" type="submit" value="submit" />
     </form>
     </div>
    </div>
    <!-- End Content -->
</div>
<!-- End Shell -->

<!-- Footer -->
<div id="footer">
    Copyright &copy;
    </div>
<!-- End Footer -->
</body>
</html>

attendance.java

package com.attendance;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class attendance
 */
@WebServlet("/attendance")
public class attendance extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public attendance() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String date = request.getParameter("adate");
         String branch = request.getParameter("branch");
         String semester = request.getParameter("semester");
         String subject = request.getParameter("subject");
         HttpSession session=request.getSession();
         session.setAttribute("date", date);
         session.setAttribute("branch", branch);
         session.setAttribute("semester", semester);
         session.setAttribute("subject", subject);
         RequestDispatcher rd=request.getRequestDispatcher("attendancedata.jsp");
         rd.forward(request, response);
    }

}

attendancedata.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<%@page language="java" import="java.sql.*"%>
<%@page import="com.connection.*"%>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title>Towards Excellence</title>
    <link rel="stylesheet" href="css/userstyle.css" type="text/css" media="all" />
    <script src="js/droplist.js" type="text/javascript"></script>
</head>
<body>
<!-- Shell -->
<div id="shell">

    <!-- Header -->
    <div id="header">
        <div class="right">
            <p>Welcome <a href="profilef.jsp"><strong><%=session.getAttribute("usertype")%></strong></a></p>
            <p class="small-nav"><a href="index.jsp">view website</a> / <a href="logout.jsp">Logout</a></p>
        </div>
    </div>
    <!-- End Header -->

    <!-- Navigation -->
    <div id="navigation">
        <ul>
            <li><a href="#">Attendance</a>
                <ul>
                    <li><a href="attendance.jsp">Take Attendance</a></li>
                    <li><a href="viewattendance.jsp">View Attendance</a></li>
                </ul>
            </li>
            <li><a href="#">Profile</a>
                <ul>
                    <li><a href="profilef.jsp">View Profile</a></li>
                    <li><a href="editprofile.jsp">Edit Profile</a></li>
                </ul>
            </li>
            <li><a href="viewstudent.jsp">View Student</a></li>
            <li><a href="#">Examination</a>
                <ul>
                    <li><a href="examination.jsp">View Examination</a></li>
                    <li><a href="addexam.jsp">Add Exam details</a></li>
                </ul>
            </li>
            <li><a href="#">Schedule</a>
                <ul>
                    <li><a href="schedule.jsp">View Schedule</a></li>
                    <li><a href="addschedule.jsp">Add Schedule</a></li>
                </ul>
            </li>
            <li><a href="sms.jsp">Send SMS</a></li>
        </ul>
    </div>
    <!-- End Navigation -->
    <!-- Content -->

      <div id="content">
     <div>
    <form action="AttendanceData" method="post">

     <%
    String semester,branch;
    semester=(String) session.getAttribute("semester");
    branch=(String) session.getAttribute("branch");
    Connection con=null;
    ResultSet rs=null;
    PreparedStatement pst=null;
    int i=0;
    con = connectiondb.condb();
    try{
        if(semester!=null && branch!=null)
        {
            //st=con.createStatement();
            String query= "select s_uid from student_tbl where s_year= '"+ semester +"' and s_branch= '" + branch + "'";
            pst=con.prepareStatement(query);
            //pst.setString(1, semester);
            //pst.setString(2, branch);
            rs=pst.executeQuery(query);
            session.setAttribute("uid", rs);
            %>
            <table>
            <tr>
            <td>Enrollment NO</td>
            <td>Present</td>
            <td>Absent</td>
            </tr>
        <%

        while(rs.next())
        {
            //String uid=rs.getString("s_uid");

            %>
            <tr>
            <td>
            <input type="text" name="<%= rs.getString("s_uid") %>" value="<%= rs.getString("s_uid") %>" readonly />
            </td>
            <td>
            <input type="radio" name="s_status<%=i%>" value="p"/>
            </td>
            <td>
            <input type="radio" name="s_status<%=i%>" value="a"/>       
            </td>
            </tr>
            <% i++;
            }
            %></table> 
            <%
            }
        }
        catch(Exception e)
                {
                    out.println(e);
                }
            %>
        <input class="submit" type="submit" value="submit" />
        </form>
     </div>
    </div>
    <!-- End Content -->
</div>
<!-- End Shell -->

<!-- Footer -->
<div id="footer">
    Copyright &copy;
    </div>
<!-- End Footer -->
</body>
</html>

AttendanceData.java

package com.attendance;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Servlet implementation class AttendanceData
 */
@WebServlet("/AttendanceData")
public class AttendanceData extends HttpServlet 
{
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    {
        // TODO Auto-generated method stub
        HttpSession session=request.getSession();
        ResultSet rs=(ResultSet) session.getAttribute("uid");
        String date=(String) session.getAttribute("date");
        String branch=(String) session.getAttribute("branch");
        String semester=(String) session.getAttribute("semester");
        String subject=(String) session.getAttribute("subject");
            int i=-1;
                    try {
                        while(rs.next()){
                        System.out.println(date+branch+semester+subject);
                        String s_uid=request.getParameter("uid");
                        String s_status=request.getParameter("s_status"+i); 
                        System.out.println(date+branch+semester+subject+s_uid+s_status);
                        AttendanceGetSet ats=new AttendanceGetSet(date, branch, semester, subject, s_uid, s_status);
                        AttendanceDAO ad=new AttendanceDAO();
                        ad.storeData(ats);
                        }
                    } catch (SQLException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                RequestDispatcher rd=request.getRequestDispatcher("success.jsp");
                 try {
                    rd.forward(request, response);
                } catch (ServletException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    }
}

AttendanceDAO.java

package com.attendance;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;

public class AttendanceDAO {
public void storeData(AttendanceGetSet ad){
    try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sms","root", "root");
        PreparedStatement psmt=con.prepareStatement("insert into attendance_tbl (date, s_branch, s_year, subject, s_status, s_uid ) values(?, ?, ?, ?, ?, ?)");
        psmt.setString(1, ad.getDate());
        psmt.setString(2, ad.getBranch());
        psmt.setString(3, ad.getSemester());
        psmt.setString(4, ad.getSubject());
        psmt.setString(5, ad.getS_status());
        psmt.setString(6, ad.getS_uid());
        psmt.executeUpdate();
        psmt.clearParameters();
        psmt.close();
    }
    catch(SQLException s){}
    catch(ClassNotFoundException nf){}
}
}

AttendanceGetSet.java

package com.attendance;

public class AttendanceGetSet {

        String date;
        String branch;
        String semester;
        String subject;
        String s_uid;
        String s_status;
        public String getDate() {
            return date;
        }
        public void setDate(String date) {
            this.date = date;
        }
        public String getBranch() {
            return branch;
        }
        public void setBranch(String branch) {
            this.branch = branch;
        }
        public String getSemester() {
            return semester;
        }
        public void setSemester(String semester) {
            this.semester = semester;
        }
        public String getSubject() {
            return subject;
        }
        public void setSubject(String subject) {
            this.subject = subject;
        }
        public String getS_uid() {
            return s_uid;
        }
        public void setS_uid(String s_uid) {
            this.s_uid = s_uid;
        }
        public String getS_status() {
            return s_status;
        }
        public void setS_status(String s_status) {
            this.s_status = s_status;
        }
        public AttendanceGetSet(String date, String branch, String semester,
                String subject, String s_uid, String s_status) {
            super();
            this.date = date;
            this.branch = branch;
            this.semester = semester;
            this.subject = subject;
            this.s_uid = s_uid;
            this.s_status = s_status;
        }


    }

0 个答案:

没有答案