Form-based voting system-MySql, DAO, VO
I. System analysis
Only one page is provided, but two pages are recommended. one is used to display the voting page, and the other is used to accept the user's voting. the corresponding teacher will receive one vote,
After work, return to the previous interface.
Various pages and functions:
VoteDao. java connects to the database, queries the instructor's number, name, votes, modifies the instructor's votes, and adds the number of votes corresponding to the number to 1
Vote. java encapsulate the instructor's number, name, number of votes received
Display. jsp call VoteDao to query the instructor ID, name, number of votes, and display
Vote. jsp obtains the instructor ID array sent from the request. call VoteDao to add 1 to the number of votes corresponding to each number and return to display. jsp.
II. Database initialization
Property: teacherno teachername votenumber
Run the script in mySQL and use the source command.
use test;create table t_vote( teacherno varchar(20), teachername varchar(20), vote int );insert into t_vote values('1', 'lishengjing',1);insert into t_vote values('2', 'huangwenjun',1);insert into t_vote values('3', 'zhaosy',2);insert into t_vote values('4', 'chaoshehn',3);
3. Java code
Copy the MySQL driver to web-inf/lib, and copy bar.jpg to the img directory under WebRoot. It is best to use import java. SQL * when importing a package *;
Public class VoteDao {private Connection conn = null; public void initConnection () throws Exception {Class. forName ("com. mysql. jdbc. driver "); String url =" jdbc: mysql: // localhost: 3306/test "; conn = DriverManager. getConnection (url, "root", "root");} public ArrayList getAllVotes () throws Exception {ArrayList all = new ArrayList (); initConnection (); string SQL = "select * from t_vote"; Statement stat = conn. createS Tatement (); ResultSet rs = stat.exe cuteQuery (SQL); while (rs. next () {Vote vote = new Vote (); vote. setTeacherno (rs. getString ("teacherno"); vote. setTeachername (rs. getString ("teachername"); vote. setVotenumber (rs. getInt ("vote"); all. add (vote);} clossConnection (); return all;} public void updateVotes (String [] teacherno) throws Exception {initConnection (); string SQL = "update t_vote set vote = vote + 1 where teac Herno =? "; PreparedStatement ps = conn. prepareStatement (SQL); for (int I = 0; I
4. JSP code
Display. jsp
Vote, jsp
<% String [] teacherno = request.getParameterValues("teacherno") ; VoteDao vdao = new VoteDao(); vdao.updateVotes(teacherno); %>