jsp投票信息一次性设置实例源码



jsp投票信息设置源码实例

<%@ page contentType=”text/html; charset=gb2312″ language=”java” import=”java.sql.*” errorPage=”" %>
<%@ page import=”java.util.*”%>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>
<link href=”css/style.css” rel=”stylesheet”>
<title>投票信息一次性设置</title>
</head>
<script language=”javascript” type=”">
function Mycheck(){
if (form2.name.value==”")
{ alert(“请输入投票信息!”);return false;}
form2.submit();
}
</script>

<body>
<table width=”570″ height=”275″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”0″>
<tr>
<td align=”right” background=”bg.gif”>
<table width=”80%” height=”104″ border=”0″ cellpadding=”0″ cellspacing=”0″>
<tr>
<td width=”10%”>&nbsp;</td>
<td width=”79%” valign=”top”>
<%if(session.getAttribute(“list”)!=null){%>
<table width=”315″ border=”0″ cellpadding=”0″ cellspacing=”0″>
<form name=”form1″ method=”post” action=”dealwith.jsp?action=insertAll”> <tr>
<td height=”25″ colspan=”2″><b>您最喜爱的图书评选:</b></td>
</tr>
<%
List list=(List)session.getAttribute(“list”);
String allName=”";
for(int i=0;i<list.size();i++){
allName=(String)list.get(i);
%>
<tr>
<td width=”230″ height=”25″>选项名称: </td>
<td width=”85″ height=”25″><input type=”text” name=”allName” value=”<%=allName%>”></td>
</tr>
<%}%>
<tr>
<td height=”25″ colspan=”2″><input type=”submit” name=”Submit” value=”保存投票信息” class=”btn_grey”></td>
</tr> </form>
</table>
<%}%>

<table width=”315″ border=”0″ cellpadding=”0″ cellspacing=”0″>
<form name=”form2″ method=”post” action=”dealwith.jsp?action=insertOne” onsubmit=”return Mycheck()”>
<tr>
<td height=”25″><b>请输入投票选项,并单击【添加投票选项】按钮!</b></td>
</tr>
<tr>
<td height=”25″><input type=”text” name=”name”></td>
</tr>
<tr>
<td height=”25″> <input type=”submit” name=”Submit2″ value=”添加投票选项” class=”btn_grey”>
</td>
</tr>
</form>
</table></td>
<td width=”11%”>&nbsp;</td>
</tr>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p></td>
</tr>
</table>
</body>
</html>

投票信息处理jsp页面源码实例:

<%@page contentType=”text/html; charset=gb2312″ language=”java” import=”java.sql.*” errorPage=”"%>
<%@page import=”java.util.*”%>
<jsp:useBean id=”connection” scope=”request” class=”com.JDBConnection”/>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>
<%
request.setCharacterEncoding(“gb2312″);
String action = request.getParameter(“action”);
if (action.equals(“insertAll”)) {
List list = (List) session.getAttribute(“list”);
String allName = “”;
for(int i=0;i<list.size();i++){
allName=(String)list.get(i);
String sql=”insert into tb_book values (‘”+allName+”‘)”;
connection.executeUpdate(sql);
}
connection.closeConnection();
session.invalidate();
%>
<script language=”javascript” type=”text/javascript”>
alert(“添加成功”);parent.location.href=’index.jsp’;
</script>
<% }
if (action.equals(“insertOne”)) {
List list = null;
if (session.getAttribute(“list”) == null) {
list = new ArrayList();
}
else {
list = (List) session.getAttribute(“list”);
}
list.add(request.getParameter(“name”));
session.setAttribute(“list”, list);
response.sendRedirect(“index.jsp”);
}
%>

操作数据库的javabean源码实例:

package com;
import java.sql.*;

public class JDBConnection {
private final String dbDriver = “com.microsoft.jdbc.sqlserver.SQLServerDriver”; //连接sql数据库的方法
private final String url = “jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_database02″;
private final String userName = “sa”;
private final String password = “”;
private Connection con = null;


public JDBConnection() {
try {
Class.forName(dbDriver).newInstance(); //加载数据库驱动
} catch (Exception ex) {
System.out.println(“数据库加载失败”);
}
}

//创建数据库连接
public boolean creatConnection() {
try {
con = DriverManager.getConnection(url, userName, password);
con.setAutoCommit(true);

} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println(“creatConnectionError!”);
}
return true;
}

//对数据库的增加、修改和删除的操作
public boolean executeUpdate(String sql) {

if (con == null) {
creatConnection();
}
try {
Statement stmt = con.createStatement();
int iCount = stmt.executeUpdate(sql);
System.out.println(“操作成功,所影响的记录数为” + String.valueOf(iCount));
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println(“executeUpdaterError!”);
}
return true;
}

//对数据库的查询操作
public ResultSet executeQuery(String sql) {
ResultSet rs;
try {
if (con == null) {
creatConnection();
}
Statement stmt = con.createStatement();
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
return null;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println(“executeQueryError!”);
return null;
}
return rs;
}

//关闭数据库的操作
public void closeConnection() {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
System.out.println(“Failed to close connection!”);
} finally {
con = null;
}
}
}

}