教师投票系统(JSP+MySQL)

display.jsp

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

 

<html>

  

  

  <body>

 

        <table align="center">

        <caption>欢迎给老师投票</caption>

        <tr bgcolor="yellow">

        <td>编号</td>

         <td>姓名</td>

          <td>得票数</td>

           <td>投票</td>

        </tr>

     

        

        <%

        

            Class.forName("com.mysql.jdbc.Driver");

 

            Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/school","root","laifu1990");

 

            Statement stat=conn.createStatement();

             String sql="select teacherno,teachername,vote from t_vote";

             ResultSet rs=stat.executeQuery(sql);

             while(rs.next())

             {

               String teacherno=rs.getString("teacherno");

               String teachername=rs.getString("teachername");

               int vote=rs.getInt("vote");

               

            %>

             

       

         <tr bgcolor="pink">

         <td><%=teacherno %></td>

         <td><%=teachername %></td>

         <td><img  src="img/bar.jpg" width="<%=vote %>>" height="10">

         <%=vote%></td>

         <td><a href="vote.jsp?teacherno=<%=teacherno%>">投票</a></td>

         </tr>

          <%

            }

               

          stat.close();

          conn.close();

          %>

          </table>

  </body>

</html>

 

 

 

vote.jsp

 

 

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

 

  <html>

  

  

  <body>

    <%

       String teacherno = request.getParameter("teacherno");

       Class.forName("com.mysql.jdbc.Driver");

 

      Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/school","root","laifu1990");

 

       String sql="update t_vote set vote=vote+1 where teacherno=?";

       PreparedStatement ps=conn.prepareStatement(sql);

       ps.setString(1, teacherno);

       ps.executeUpdate();

       ps.close();

       conn.close();

       

     %>

    <jsp:forward page="display.jsp"></jsp:forward>

  </body>

</html>

 

 

posted @ 2013-10-21 15:36  来福1990627  阅读(815)  评论(0编辑  收藏  举报