Java 投票(自动加一)
数据库建立:
SQL> create table vote(
2 id number,
3 name varchar(200),
4 num number
5 );
//index.jsp
pageEncoding="utf-8"%>
投票Connection conn1 = null;
Statement stmt1 = null;
ResultSet rs1 = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn1 = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl", "zhang",
"zhang");
stmt1 = conn1.createStatement();
String url = "select * from vote order by id desc";
rs1 = stmt1.executeQuery(url);
while (rs1.next()) {
out.print(rs1.getString("name")+":");
%>
" >支持
out.print("
");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs1 != null) {
rs1.close();
}
if (stmt1 != null) {
stmt1.close();
}
if (conn1 != null) {
conn1.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
%>
//turn.jsp
pageEncoding="utf-8"%>
turn//int add = 1;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String myid = request.getParameter("id");
//if ("".equals(myid) || myid == null) {
//out.println("您如果对感兴趣的话,请填写!");
//out.print("
");
//} else {
Boolean flag = false;
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl", "zhang",
"zhang");
stmt = conn.createStatement();
int addl = Integer.valueOf(myid);
//++addl;
//String lid=addl+"";
String url = "update vote set num=num+1 where id=" + myid + "";
//String url = "update adder set num=num+1 where id=" + myid + "";
//out.print("url"+url);
stmt.execute(url);
out.print("5555555555");
response.sendRedirect("../service/index.jsp");
return;
//String urll = "select * from adder";
//rs = stmt.executeQuery(urll);
//while (rs.next()) {
//flag = true;
//}
} catch (Exception e) {
e.printStackTrace();
}
//}
%>