導航:首頁 > 編程語言 > jspmysql投票系統

jspmysql投票系統

發布時間:2023-07-14 11:28:25

Ⅰ 教你輕松應對jsP連接MySQL資料庫問題


當您面臨JSP連接MySQL資料庫問題,你首先需要在MySQL資料庫里創建一username表,表裡面創建兩個字元型的欄位,欄位名分別為:uid,pwd,然後插入幾條測試數據。
以下用兩種方式來實現JSP連接MySql資料庫
第一種方式,用JSP實現:
%@ page contentType="text/html;
charset=gb2312" language="java"
import="java.sql.*"%
meta http-equiv="Content-Type" content="text/html; charset=gb2312"
%//*******************************
****************
JDBC_ODBC連接MySql資料庫,不需要設置數據源***********
**********************/
//********** 資料庫連接代碼開始 ******/
//以下幾項請自行修改String server="localhost";
//MYSQL 伺服器的地址String dbname="test";
//MYSQL 資料庫的名字String user="root";
//MYSQL 資料庫的登錄用戶名String pass="chfanwsp";
//MYSQL 資料庫的登錄密碼String port="3306";
//SQL Server 伺服器的埠號,
默認為1433//資料庫連接字元串
String url ="jdbc:mysql://"+server+":"+port+"/"+dbname+"?
user="+user+"password="+pass+"useUnicode
=truecharacterEncoding=GB2312";
//載入驅動程序Class.forName("org.gjt.mm.mysql.Driver").newInstance();
//建立連接Connection conn= DriverManager.getConnection(url);
//創建語句對象Statement stmt=conn.createStatement
(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// **** 資料庫連接旅磨代碼結束 ******* String sql="select * from username";
ResultSet rs=stmt.executeQuery(sql);
//rs.first();while(rs.next()){out.print("用戶名:");
out.print(rs.getString("uid")+" 密碼:");
out.println(rs.getString("pwd")+"br");}
rs.close();stmt.close();conn.close();%
第二種方式,用JavaBean來實現:
DBConnMySql.java編譯以後的Class文件應該放在Web-INFclassesconn目錄下。
package conn; //導入包import java.sql.*;
//導入資料庫操作的類public class DBConnMySql
//構造方法,拆察斗初始化{private Connection conn;
//連接對象private Statement stmt;
//語句對象private ResultSet rs;
//結果集對象private String MySqldriver;
//MYSQL Server驅動程序字元串private String MySqlURL;
//MYSQL Server連接字元串//*********************************
用 org.gjt.mm.mysql.Driver 驅動
* 該方沒臘法取得連接所需各種參數,組成連接字元串,
然後再建立連接* server;dbname,user,pass,port
分別表示MYSQL 伺服器的地址,
* 資料庫,用戶名,密碼,埠
**********************************/
public Connection getConnToMySql
(String server,String dbname,String user,String pass,String port)
{//MYSQl驅動程序MySqldriver = "org.gjt.mm.mysql.Driver";
MySqlURL = "jdbc:mysql://";
//連接字元串一部分try{//完整的連接字元串MySqlURL
=MySqlURL+server+":"+port+"/"+dbname+"?user=
"+user+"password="+pass+"useUnicode
=truecharacterEncoding=GB2312";
Class.forName(MySqldriver);conn
= DriverManager.getConnection(MySqlURL);}
catch(Exception e){System.out.println
("操作資料庫出錯,請仔細檢查");
//System.err.println(e.getMessage());}return conn;}
//關閉資料庫連接public void close(){try{//rs.close();
//stmt.close();conn.close();}catch(SQLException
sqlexception){sqlexception.printStackTrace();}}}
這個文件只是實現了資料庫的連接,下面再寫一個測試文件,就是用SQL語句從資料庫里查詢出記錄,以驗證我們資料庫的連接是否成功。
connmysql.jsp文件源代碼如下:
meta http-equiv="Content-Type" content="text/html;
charset=gb2312"%@ page contentType="text/html;
charset=gb2312" language="java" import="java.sql.*" %
jsp:useBean id="DBConn" scope="page" class="conn.DBConnMySql"/
% //以下幾項請自行修改String server="localhost";
//MYSQL 伺服器的地址String dbname="test";
//MYSQL 資料庫的名字String user="root";
//MYSQL 資料庫的登錄用戶名String pass="chfanwsp";
//MYSQL 資料庫的登錄密碼String port="3306";
//SQL Server 伺服器的埠號,默認為1433Connection
conn=DBConn.getConnToMySql
(server,dbname,user,pass,port);
Statement stmt=conn.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
String sql="select * from username";
String sql1="insert into username (uid,pwd) values
('夢想年華','夢想年華')";stmt.executeUpdate(sql1);
ResultSet rs=stmt.executeQuery(sql);while(rs.next())
{out.print("用戶名:");out.print(rs.getString("uid")+" 密碼:");
out.println(rs.getString("pwd")+"br");}
//rs.close();//stmt.close();//conn.close();DBConn.close();%

Ⅱ jsp 投票系統怎麼實現投的票計數啊投得票數的多少放入資料庫中嗎

恩,要用到資料庫的。給你寫個頁面的代碼,不過讀取資料庫和更新資料庫的代碼就要你自己寫了。這兒只是這個jsp中變化的,頁面關了數值都會重置。所以要用到資料庫,傳值和調用並顯示就看你寫了,
<html>
<head>

<title>My JSP 'main.jsp' starting page</title>

<script type="text/javascript">
var i=0;
var j=0;
function addi(){
++i;
document.getElementById("_good").value=i+" 滿意";
}
function addj(){
++j;
document.getElementById("_bad").value=j+" 不滿意";
}
</script>

</head>

<body>
<center>
<table>
<tr><td><input type="button" id="_good" onmouseup="addi()" value="0 滿意"/></td>
<td><input type="button" id="_bad" onmouseup="addj()" value="0 不滿意"/></td></tr>
</table>

</center>
</body>
</html>

//要全部代碼可以說下

Ⅲ jsp投票系統求源代碼!可以連資料庫的!

<p style=line-height: 150%><SPAN style="FONT-SIZE: 12px"><STRONG>這是用文本文件作為存儲載體的投票系統:</STRONG></SPAN>
<p style=line-height: 150%><SPAN style="FONT-SIZE: 12px"><STRONG>vote.java:</STRONG></SPAN>
<SPAN style="FONT-SIZE: 12px"><STRONG></STRONG> <p style=line-height: 150%>
// Java Document
package vote;
import java.io.*;
import java.util.*;
public class vote extends Object
{
public String filePath="";
public int n;
private File voteFile;
private BufferedReader fileRead;
private PrintWriter fileWrite;
public String systemMessage="";
private String voteStr[]=new String[10];
public int voteNum[]=new int[10];
public void createFile()
throws FileNotFoundException
{
voteFile=new File(filePath);
if(!voteFile.exists())
{
fileWrite=new PrintWriter(new FileOutputStream(filePath));
for(int i=0;i<n;i++) fileWrite.println("0");
fileWrite.close();
}
}
public void writeFile()
throws FileNotFoundException
{
fileWrite=new PrintWriter(new FileOutputStream(filePath));
for(int i=0;i<n;i++)
{
fileWrite.println(voteNum[i]);
}
fileWrite.close();
}
public void readFile()
throws FileNotFoundException
{
fileRead=new BufferedReader(new FileReader(filePath));
for(int i=0;i<n;i++)
{
try
{voteStr[i]=fileRead.readLine();
}
catch(IOException f)
{
voteStr[i]="0";}
voteNum[i]=Integer.parseInt(voteStr[i]);
}
try
{
fileRead.close();
}
catch(IOException d)
{
systemMessage=d.toString();
}
}
}
<p style=line-height: 150%><SPAN style="LINE-HEIGHT: 15pt"><SPAN style="FONT-SIZE: 12px"><STRONG>vote.jsp:</STRONG>

<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<%@ page import="java.util.*"%>
<%@ page import="java.lang.*"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="vote" scope="request" class="vote.vote"/>
<%
String vote1=request.getParameter("lang");
vote.n=4;
vote.filePath="vote.txt";
vote.createFile();
vote.readFile();
if(vote1.compareTo("0")==0)
vote.voteNum[0]++;
if(vote1.compareTo("1")==0)
vote.voteNum[1]++;
if(vote1.compareTo("2")==0)
vote.voteNum[2]++;
if(vote1.compareTo("3")==0)
vote.voteNum[3]++;
vote.writeFile();
%>
<script language="javascript">
alert("感謝你投了寶貴的一票");
self.location="index.jsp";
</script></SPAN></SPAN>
<p style=line-height: 150%><STRONG> </STRONG><SPAN style="LINE-HEIGHT: 15pt"><SPAN style="FONT-SIZE: 12px"><STRONG>see.jsp:</STRONG>

<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<%@ page import="java.util.*"%>
<%@ page import="java.lang.*"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="vote" scope="request" class="vote.vote"/>
<%
String vote1=request.getParameter("lang");
vote.n=4;
vote.filePath="vote.txt";
vote.createFile();
vote.readFile();
int total=0;
float voteFlo[]=new float[5];
for(int i=0;i<4;i++) total+=vote.voteNum[i];
for(int i=0;i<4;i++) voteFlo[i]=150*((float)vote.voteNum[i]/(float)total);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>查看調查</title>
<link href="t1.css" rel="stylesheet" type="text/css">
</head>
<body>

<table width="30%" border="0" class="t1">
<tr>
<td colspan="2"><div align="center">調查結果</div></td>
</tr>
<tr>
<td width="18%">JSP</td>
<td width="82%"><img src="bar.gif" width=<%=voteFlo[0]%> height=8> <%=vote.voteNum[0]%></td>
</tr>
<tr>
<td>ASP</td>
<td><img src="bar.gif" width=<%=voteFlo[1]%> height=8> <%=vote.voteNum[1]%></td>
</tr>
<tr>
<td>PHP</td>
<td><img src="bar.gif" width=<%=voteFlo[2]%> height=8> <%=vote.voteNum[2]%></td>
</tr>
<tr>
<td>其他</td>
<td><img src="bar.gif" width=<%=voteFlo[3]%> height=8> <%=vote.voteNum[3]%></td>
</tr>
<tr>
<td colspan="2"><div align="center"><a href="javascript:window.close();">關閉窗口</a></div></td>
</tr>
</table>
</body>
</html>
</SPAN></SPAN>
<p style=line-height: 150%><SPAN style="LINE-HEIGHT: 15pt"><SPAN style="FONT-SIZE: 12px"><SPAN style="LINE-HEIGHT: 15pt"><SPAN style="FONT-SIZE: 12px"><STRONG>index.jsp:</STRONG>
</SPAN></SPAN>
</SPAN></SPAN> <p style=line-height: 150%><SPAN style="LINE-HEIGHT: 15pt"><SPAN style="FONT-SIZE: 12px"><%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>投票</title>
<link href="t1.css" rel="stylesheet" type="text/css">
</head>
<script language="javascript">
function cw()
{window.open("see.jsp","mywindow","toolbar=no,left=150,top=200,width=270,height=350,menubar=no,systemMenu=no");
}
</script>
<body>
<table width="15%" height="250" align="left">
<tr>
<td><form name="form1" method="post" action="vote.jsp">
<table width="100%" height="250" border="1" align="center" bordercolor="#9966CC" class="t1">
<tr>
<td><div align="left">你所使用的開發語言</div></td>
</tr>
<tr>
<td><input type="radio" name="lang" value="0">
JSP</td>
</tr>
<tr>
<td><input type="radio" name="lang" value="1">
ASP</td>
</tr>
<tr>
<td><input type="radio" name="lang" value="2">
PHP</td>
</tr>
<tr>
<td><input type="radio" name="lang" value="3">
其他 </td>
</tr>
<tr>
<td><div align="center">
<input name="vote" type="image" src="poll.gif" width="40" height="20" border="0">
<a href="javascript:cw()"><img src="see.gif" width="40" height="20" border="0"></a></div></td>
</tr>
</table>
</form></td>
</tr>
</table>
</body>
</html>
</SPAN></SPAN></SPAN>

Ⅳ 用jsp做網上投票系統 代碼

我之前做過類似的投票小項目,在這里把源碼發給你。你自己好好的參考一下。--------------------------package com.tv.bean;public class TVBean {
private String tvName;
private int tvCount;

public String getTvName() {
return tvName;
}
public void setTvName(String tvName) {
this.tvName = tvName;
}
public int getTvCount() {
return tvCount;
}
public void setTvCount(int tvCount) {
this.tvCount = tvCount;
}
}------------------------------package com.tv.;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;import com.tv.bean.TVBean;
public class DBUtil {
private Connection con;
private PreparedStatement ps = null;
private ResultSet rs;
public void getCon(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=TV","sa","");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

public void closeCon(){
try {
if(rs !=null) rs.close();
if(ps != null) ps.close();
if(con != null) con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

public ArrayList getAll(){
ArrayList al = new ArrayList();
this.getCon();
String sql = "select * from TVInfo order by tvCount desc";
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
TVBean tb = new TVBean();
tb.setTvName(rs.getString(1));
tb.setTvCount(rs.getInt(2));
al.add(tb);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.closeCon();
}
return al;
}

public boolean updateByName(String name){
this.getCon();
String sql = "update TVInfo set tvCount=tvCount+1 where tvName='"+name+"'";
try {
ps = con.prepareStatement(sql);
int i = ps.executeUpdate();
if(i > 0) return true;
else return false;
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
this.closeCon();
}
}
}
---------------------------------------package com.tv.servlet;import java.io.IOException;
import java.util.ArrayList;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import com.tv..DBUtil;public class VoteServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
DBUtil = new DBUtil();
String [] name = request.getParameterValues("tvs");
int num = name.length;
for(int i =0; i < num; i++){
if(.updateByName(name[i])){
request.setAttribute("to", "投票成功!");
ArrayList al = .getAll();
HttpSession session = request.getSession();
session.setAttribute("al", al);
request.getRequestDispatcher("success.jsp").forward(request, response);
}else{
request.setAttribute("to", "投票失敗!請重新再試!");
}
}

} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
-----------------------------------<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri=" http://java.sun.com/jsp/jstl/core"%><html>
<head>
<title>電視投票</title>
</head> <body>
<center>
<form action="vote" method="post">
<table border="1" align="center">
<tr align="center">
<td>
<h2>
選擇您最喜歡的電視劇並投上一票
</h2>
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="tvs" value="詠春" />
詠春
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="tvs" value="金婚" />
金婚
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="tvs" value="士兵突擊" />
士兵突擊
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="tvs" value="少年張三豐" />
少年張三豐
</td>
</tr>
<tr>
<td align="center">
<input type="submit" value=" 提交 " />

<input type="reset" value=" 重置 " />
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
-------------------------------------第一段代碼為JavaBean;第二段代碼為資料庫連接類;第三段代碼為Servlet控制類;第四段代碼為JSP顯示頁面。希望能夠解決你的問題!

Ⅳ jsp投票系統

方法一 在 appliction 中藏一個值,可以 系統年月日作為鍵,值為計數器,每次投票去查看下當天內日期的鍵下的值是否容到達50 ,沒有到達計數器+1,進行投票,到達了則不執行投票.

方法二 在 appliction 中藏一個值,可以 固定鍵,值為計數器,每次投票去查看下當天日期的鍵下的值是否到達50 ,沒有到達計數器+1,進行投票,到達了則不執行投票. 每天0:00 將值設置為0

閱讀全文

與jspmysql投票系統相關的資料

熱點內容
安卓代碼讀取界面信息 瀏覽:714
數控車床車55度螺紋怎麼編程 瀏覽:391
word桌面找不到文件 瀏覽:585
數控車床編程怎麼做到的 瀏覽:639
哈爾濱市行政區劃代碼 瀏覽:716
如何顯示文件擴張 瀏覽:283
ps怎麼移植文件 瀏覽:443
傳奇補丁文件說明 瀏覽:513
奧維地圖怎麼設置網路疊加 瀏覽:407
linux載入庫文件so失敗 瀏覽:714
大家都想要的app 瀏覽:58
程序員比特幣 瀏覽:568
mysqlfrm是什麼文件 瀏覽:665
win10點擊文件夾聲音消失 瀏覽:963
蘋果手機空白文件名 瀏覽:833
linuxmv覆蓋合並文件夾 瀏覽:932
不開訪客網路怎麼聯網 瀏覽:631
win10的軟體卸載 瀏覽:779
crv汽車自帶網路怎麼打開 瀏覽:973
食品原材料到什麼網站購買 瀏覽:649

友情鏈接