JSP case analysis (I)-Online Voting System-4-code analysis

Source: Internet
Author: User

The original source code does not have a myeclipse project, the database uses sqlserver2000, and the server uses tomcat.

I put all the code in myeclipse10, deployed to Apache-Tomcat-6.0.35, the database uses sqlserver2008, found that the original driver can also be used, you can also use (http://blog.csdn.net/flyuniverse_shell/article/details/7351016).

Source code successfully deployed on my local machine:

 

 

Key source code:

(Db. Java)

Package com. yxq. toolbean;

Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. statement;
Import java. SQL. resultset;
Import java. util. arraylist;
Import java. util. List;

Import com. yxq. valuebean. tempsingle;
Import com. yxq. valuebean. votesingle;

Public class dB {
Private string classname;

Private string URL;

Private string username;

Private string password;

Private connection con;

Private statement STM;

Private resultset RS;

Public dB (){
Classname = "com. Microsoft. JDBC. sqlserver. sqlserverdriver ";
Url = "JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = db_vote ";
Username = "sa ";
Password = "xiexie ";
}

/**
* @ Function load database driver
*/
Public void loaddrive (){
Try {
Class. forname (classname );
} Catch (classnotfoundexception e ){
System. Out. println ("failed to load the database driver! ");
E. printstacktrace ();
}
}
 
/**
* @ Function get database connection
*/
Public void getcon (){
Loaddrive ();
Try {
Con = drivermanager. getconnection (URL, username, password );
} Catch (exception e ){
System. Out. println ("failed to connect to the database! ");
E. printstacktrace ();
}
}
 
/**
* @ Function get statement object
*/
Public void getstm (){
Getcon ();
Try {
STM = con. createstatement ();
} Catch (exception e ){
System. Out. println ("An error occurred while obtaining the statement object! ");
E. printstacktrace ();
}
}

/**
* @ Function: query a data table and obtain the result set.
*/
Public void getrs (string SQL ){
Getstm ();
Try {
Rs = cmd.exe cutequery (SQL );
} Catch (exception e ){
System. Out. println ("An error occurred while querying the database! ");
E. printstacktrace ();
}
}
 
/**
* @ Function query data tables and obtain voting options
*/
Public list selectvote (string SQL ){
List votelist = NULL;
If (SQL! = NULL &&! SQL. Equals ("")){
Getrs (SQL );
If (RS! = NULL ){
Votelist = new arraylist ();
Try {
While (Rs. Next ()){
Votesingle = new votesingle ();
Votesingle. setid (mytools. inttostr (Rs. getint (1 )));
Votesingle. settitle (Rs. getstring (2 ));
Votesingle. setnum (mytools. inttostr (Rs. getint (3 )));
Votesingle. setorder (mytools. inttostr (Rs. getint (4 )));
Votelist. Add (votesingle );
}
} Catch (exception e ){
System. Out. println ("An error occurred while encapsulating data in the tb_vote table! ");
E. printstacktrace ();
} Finally {
Closed ();
}
}
}
Return votelist;
}

/**
* @ Function query the data table to obtain the last voting record of the specified IP Address
*/
Public tempsingle selecttemp (string SQL ){
Tempsingle = NULL;
If (SQL! = NULL &&! SQL. Equals ("")){
Getrs (SQL );
If (RS! = NULL ){
Try {
While (Rs. Next ()){
Tempsingle = new tempsingle ();
Tempsingle. setid (mytools. inttostr (Rs. getint (1 )));
Tempsingle. setvoteip (Rs. getstring (2 ));
Tempsingle. setvotemsel (Rs. getlong (3 ));
Tempsingle. setvotetime (Rs. getstring (4 ));
}
} Catch (exception e ){
System. Out. println ("An error occurred while encapsulating data in the tb_temp table! ");
E. printstacktrace ();
} Finally {
Closed ();
}
}
}
Return tempsingle;
}
 
/**
* @ Function update data table
*/
Public int Update (string SQL ){
Int I =-1;
If (SQL! = NULL &&! SQL. Equals ("")){
Getstm ();
Try {
I = cmd.exe cuteupdate (SQL );
} Catch (exception e ){
System. Out. println ("failed to update database! ");
E. printstacktrace ();
} Finally {
Closed ();
}
}
Return I;
}
/**
* @ Function close database connection
*/
Public void closed (){
Try {
If (RS! = NULL)
Rs. Close ();
If (STM! = NULL)
STM. Close ();
If (con! = NULL)
Con. Close ();
} Catch (exception e ){
System. Out. println ("failed to close the database! ");
E. printstacktrace ();
}
}
}

 

Voting algorithm source code:

(Dovote. jsp)

<% @ Page contenttype = "text/html; charset = UTF-8" %>
<% @ Page import = "com. yxq. valuebean. tempsingle" %>
<% @ Page import = "com. yxq. toolbean. mytools" %>
<% @ Page import = "Java. util. Date" %>

<JSP: usebean id = "mydb" class = "com. yxq. toolbean. DB"/>
<%
String mess = "";
String selectid = request. getparameter ("Ilike"); // get the user selected
If (selectid = NULL | selectid. Equals ("") {// no selection
Mess = "select vote! ";
}
Else {// selected
Boolean mark = false; // indicates whether to allow voting.
Long today = (new date ()). gettime (); // new date () to obtain the current time. Call the gettime () method of the date class to obtain the number of milliseconds from 00:00:00, January 1, January 1, 1970 to the current time.
Long last = 0; // time of the last vote (displayed in milliseconds)
String IP = request. getremoteaddr (); // obtain the user IP Address
String SQL = "select * From tb_temp where votemsel = (select max (votemsel) from tb_temp where voteip = '" + IP + "')"; // SQL statement, function: obtain the record of the current user's last vote from the data table
Tempsingle single = mydb. selecttemp (SQL );
If (single = NULL) // The current IP address does not exist in the tb_temp table.
Mark = true; // allow voting
Else {// if the current IP address exists, it determines whether the specified time has exceeded from the last vote to the present. The system specifies 60 minutes.
Last = single. getvotemsel (); // obtain the last voting time from the JavaBean (displayed in milliseconds)
String result = mytools. comparetime (today, last); // compare the current time with the time of the last vote
If (result. Equals ("yes") // returns "yes", indicating that the time difference has exceeded 60 minutes and voting is allowed.
Mark = true;
Else // otherwise, voting is not allowed
Mark = false;
}

String strtime = mytools. formatdate (today); // convert the current voting time (displayed in milliseconds) to the "Year-month-day hour: minute: Second" Format
If (Mark) {// allow voting
/** [1] record the user IP address and voting time **/
SQL = "insert into tb_temp values ('" + IP + "', '" + today + "', '" + strtime + "')";
Int I = mydb. Update (SQL );

/** [2] determine whether the user's IP address is successfully recorded **/
If (I <= 0) // ip record failed
Mess = "An error occurred while recording your IP address! ";
Else {// ip address recorded successfully
/** Number of updated votes **/
SQL = "Update tb_vote set vote_num = vote_num + 1 where id =" + selectid;
I = mydb. Update (SQL); // update successful
If (I> 0)
Mess = "voting takes effect! ";
Else // update failed
Mess = "failed to vote! ";
}
}
Else {// voting not allowed
Mess = "Sorry, by judging your IP address, you have already voted! <Br> last voting time: "+ single. getvotetime () +" <br> no more voting is allowed within 60 minutes! ";
}
}

Session. setattribute ("mess", Mess); // Save the prompt information to the session range.
Response. sendredirect ("messages. jsp"); // redirect the request to the messages. jsp page to prompt
%>

 

 

Related Article

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.