1。這裏會鏈接數據庫--JDBC的學習實例java
一共有3個頁面。mysql
2.第一個頁面是一個form表單,第二個頁面是處理數據,第三個頁面是顯示頁面sql
vote.jsp數據庫
<body bgcolor="green"> 選擇你要投票的人: <form action="vote_end.jsp"> <input type="radio" name="pp" value="a"/>周杰倫<img src="img/a.jpg"/> <br><input type="radio" name="pp" value="b"/>張 傑<img src="img/b.jpg"/> <br><input type="radio" name="pp" value="c"/>范冰冰<img src="img/c.jpg"/> <br><input type="radio" name="pp" value="d"/>趙 薇<img src="img/d.jpg"/> <br><input type="radio" name="pp" value="e"/>黃曉明<img src="img/e.jpg"/> <br><br><input type="submit" value="提交"/> </form> </body>
vote_end2.jspsession
<body bgcolor="red"> <center> <% String sess = request.getSession().getId(); String sess2 = null; out.print("恭喜你,投票成功。<br>"); String pp = request.getParameter("pp"); String people = null; if(pp.equals("a")){ people="'周杰倫'"; }else if(pp.equals("b")){ people="'張傑'"; }else if(pp.equals("c")){ people="'范冰冰'"; }else if(pp.equals("d")){ people="'趙薇'"; }else if(pp.equals("e")){ people="'黃曉明'"; } Class.forName("com.mysql.jdbc.Driver"); Connection connection=DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=123&useUnicode=true&characterEncoding=gbk"); Statement statement = connection.createStatement(); //查看是否投過票 ResultSet rs2 = statement.executeQuery("SELECT * FROM sess"); while(rs2.next()){ sess2 = rs2.getString("id"); if(sess2.equals(sess)){ %> <jsp:forward page="vote_no.jsp"/> <% } } //查找數據庫 ResultSet rs = statement.executeQuery("SELECT * FROM people where name="+people); rs.next(); int count = rs.getInt("count"); count = count+1; //更新數據庫 statement.executeUpdate("UPDATE people SET count="+count+" where name="+people); //投票session號保存到數據庫 statement.executeUpdate("insert into sess values('"+sess+"')"); //顯示數據庫 ResultSet rss = statement.executeQuery("SELECT * FROM people"); out.print("<table border=1>"); out.print("<tr>"); out.print("<th>姓名</th>"); out.print("<th>票數</th>"); out.print("</tr>"); while(rss.next()) { out.print("<tr>"); out.print("<td>"+rss.getString(1)+"</td>"); out.print("<td>"+rss.getString(2)+"</td>"); out.print("</tr>"); } out.print("</table>"); rs.close(); statement.close(); connection.close(); %> </center> </body>
vote_end.jspjsp
<body bgcolor="red"> <center> <% out.print("恭喜你,投票成功。<br>"); String pp = request.getParameter("pp"); String people = null; if(pp.equals("a")){ people="'周杰倫'"; }else if(pp.equals("b")){ people="'張傑'"; }else if(pp.equals("c")){ people="'范冰冰'"; }else if(pp.equals("d")){ people="'趙薇'"; }else if(pp.equals("e")){ people="'黃曉明'"; } Class.forName("com.mysql.jdbc.Driver"); Connection connection=DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=123&useUnicode=true&characterEncoding=gbk"); Statement statement = connection.createStatement(); //查找數據庫 ResultSet rs = statement.executeQuery("SELECT * FROM people where name="+people); rs.next(); int count = rs.getInt("count"); count = count+1; //更新數據庫 statement.executeUpdate("UPDATE people SET count="+count+" where name="+people); //顯示數據庫 ResultSet rss = statement.executeQuery("SELECT * FROM people"); out.print("<table border=1>"); out.print("<tr>"); out.print("<th>姓名</th>"); out.print("<th>票數</th>"); out.print("</tr>"); while(rss.next()) { out.print("<tr>"); out.print("<td>"+rss.getString(1)+"</td>"); out.print("<td>"+rss.getString(2)+"</td>"); out.print("</tr>"); } out.print("</table>"); rs.close(); statement.close(); connection.close(); %> </center> </body>