Form-based voting system-MySql, DAO, VO_MySQL

Source: Internet
Author: User
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);    %>    
     

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.