JSP Learning Diary (3) JDBC Database Operation

xiaoxiao2021-03-06  29

An online voting system based on Access database.

Beforevote.jsp

<% @ Page ContentType = "Text / HTML; Charset = UTF-8"%> <% @ Page Import = "Java.sql. *"%>

<% // jdbc connection database has two ways: JDBC-ODBC bridge mode and dedicated JDBC connection method for a particular database. // This case uses JDBC-ODBC bridge mode. // ODBC data source name: votedsn // table Candidates ---- Store candidate's name: Name and ticket: count // Table Voters ---- Store the voter's IP: voterip //, Fill in the candidate's name and initial votes into the Candidates table. %>

<% stringBuffer CandidatesList = new stringbuffer (); connection con; // 加 加 c j; ("Sun.jdbc.odbc.jdbcodbcdriver"); // /Class.forname ("Oracle.jdbc.driver.Oracledriver");} catch (ClassNotFoundException E) {E.PrintStackTrace ();} // Connect the database and read the contents of the Candidates table Try {Con = DriverManager.getConnection JDBC: ODBC: Vote "," "" "); // con = DriverManager.getConnection (" JDBC: Oracle: Thin: @ 192.168.0.3: 1521: kWDB "," Okjohn "," Password "); // Dedicated JDBC mode SQL = con.createstatement (); rs = sql.executeQuery ("Select * from Candidates"); CandidatesList.Append ("

"); candidatesList.Append ("< Table Border> "); CandidatesList.Append (" "); CandidatesList.Append (" " " Name "); CandidatesList.Append (" " " voting Select "); CandidatesList.Append (" "); while (rs.next ()) {CandidatesList.Append (" "); string name = rs.getstring (" name "); CandidatesList.Append ("" Name ""); CANDI DatesList.Append (" "); CandidatesList.Append ("");} CandidatesList.Append ("< / Table> "); CandidatesList.Append (" "); CandidatesList.Append (" "); con.close (); out.print (CandidatesList);

} Catch (sqlexception ex) {ex.printstacktrace ();}%> 2. EXECVOTE.JSP

<% @ Page ContentType = "Text / HTML; Charset = UTF-8"%> <% @ page import = "java.sql. *"%> <% @ Page Import = "java.io. *"%>

<%! // Variables of the total number of votes int total = 0; synchronized void counttotal () {Total ;}%> <% boolean alowvote = true; // Get the name of the candidate String Candidate = request.getParameter (" Selection "); if (Candidate == NULL) {CANDIDATE ="? ";} byte [] temp = Candidate.getBytes (" ISO-8859-1 "); Candidate = new string (TEMP); // Get voters IP address string ip = (string) request.getRemoteAddr (); // load bridge try {class.forname ("sun.jdbc.odbc.jdbcodbcdriver");} catch (ClassNotFoundExcection E) {E.PrintStackTrace (); ("JDBC: Vote", "JDBC: ODBC: vote", "" " SQL = con.createstatement (); rs = sql.executeQuery ("Select * from voters where voterip = ' ip "'); int = 0; while (rs.next ()) {row ;} IF (row> = 1) {alowvote = false;}} catch (sqlexception e1) {e1.printStackTrace ();} // vote for vote processing if (Candidate.equals ("?")) {Out.print ("You No vote. ");} / * Else if (alowvote == false) {Out.print (" You have already voted. ");} * / Else {out.print (" You vote.

"); Counttotal (); try {r = SQL.ExecuteQuery (" Select Count from Candidates WHERE Name = ' Candidate "); rs.next (); int count = rs.Getint (" count ") COUNT ; SQL.ExecuteUpdate ("Update Candidates Set Count =" "Where Name = '" Candidate "'"); SQL.ExecuteUpdate ("Insert INTO VOTERS (Voterip) VALUES ('" ip " ') ");} Catch (sqlexception e) {E.PrintStackTrace ();} // Display ticket situation try {= SQL.ExecuteQuery (" Select * from Candidates "); stringbuffer CandidatesList = new stringbuffer (); CandidatesList. Append (

"); CandidatesList.Append (" "); CandidatesList.Append ("
Name Votes Votes "); While (rs.next ()) {CandidatesList.Append ("
" RS.GetString (" Name ") " "); INT count = rs.Getint ("count"); CandidatesList.Append ("" COUNT ""); double rate = (double) count / total; rate = rate * 100; // Keep two decimal int RATE2 = ((int) RATE) (Rate - (int) Rate)> = 0.5? 1: 0); // Since the four rounds of processing CA NDIDATESLIST.APpend ("" Rate2 "% "); CandidatesList.Append ("");} CandidatesList.Append (""); CandidatesList.Append ( "Total Votes:" Total); Out.Print (CandidatesList);} catch (sqlexception e) {E.PrintStackTrace ();}}%>

转载请注明原文地址:https://www.9cbs.com/read-39577.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.036, SQL: 9