MVC模式是Java程序设计中的一种常用的设计模式,它将一个交互式应用程序分成相对独立而又协同工作的3个组成部分:
模型(Model):业务逻辑层。实现具体的业务逻辑、状态管理;
视图(View):表示层。与用户实现交互的界面,通常实现数据的输入和输出功能;
控制器(Controller):控制层。控制整个业务流程,实现View和Model之间的协同工作。
web工程目录
JavaBean
package com.mipo.beans;
public class Contact {
private int cid;
private String nam;
private String sex;
private String tel;
private String birth;
private String tnam;//数据表中无此字段,是为了方便后面显示数据,故加此属性
private int uid;
private int tid;
public int getCid() {
return cid;
}
public String getNam() {
return nam;
}
public String getSex() {
return sex;
}
public String getTel() {
return tel;
}
public int getUid() {
return uid;
}
public void setCid(int cid) {
this.cid = cid;
}
public void setNam(String nam) {
this.nam = nam;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setTel(String tel) {
this.tel = tel;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getBirth() {
return birth;
}
public int getTid() {
return tid;
}
public void setBirth(String birth) {
this.birth = birth;
}
public void setTid(int tid) {
this.tid = tid;
}
public String getTnam() {
return tnam;
}
public void setTnam(String tnam) {
this.tnam = tnam;
}
}
package com.mipo.beans;
public class Ctype {
private int tid;
private String tnam;
public int getTid() {
return tid;
}
public String getTnam() {
return tnam;
}
public void setTid(int tid) {
this.tid = tid;
}
public void setTnam(String tnam) {
this.tnam = tnam;
}
}
package com.mipo.beans;
public class Users {
private int uid;
private String nam;
private String pwd;
public String getNam() {
return nam;
}
public String getPwd() {
return pwd;
}
public int getUid() {
return uid;
}
public void setNam(String nam) {
this.nam = nam;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public void setUid(int uid) {
this.uid = uid;
}
}
package com.mipo.beans;
import java.util.ArrayList;
import java.util.List;
public class PageBean {
private int pagesize;//每页显示的记录条数
private int pagetotal;//总页数
private int p;//当前第p页
private int count;//总记录数
private List data;//存放本页数据的集合
//给该类设计一个无参的构造函数,其主要目的是对List对象进行实例化,同时可以设定默认每页显示的记录条数
public PageBean() {
pagesize = 3;//默认每页显示3条记录
data = new ArrayList();//List类型对象data不实例化,在使用时会报NullException异常
}
public int getPagesize() {
return pagesize;
}
public int getPagetotal() {
return pagetotal;
}
public int getP() {
return p;
}
public int getCount() {
return count;
}
public List getData() {
return data;
}
//首先执行——设定每页显示的记录条数
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
//其次执行——获取分页的总页数
public void setCount(int count) {
this.count = count;
//Math.ceil()返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数。
pagetotal = (int)(Math.ceil(count*1.0D/pagesize));
}
//最后执行——设定当前页的范围
public void setP(int p) {
if(p<1){
this.p = 1;
}else if(p>pagetotal){
this.p = pagetotal;
}else{
this.p = p;
}
}
public void setData(List data) {
this.data = data;
}
//给本页集合添加数据
public void addData(Object obj) {
data.add(obj);
}
}
package com.mipo.dao;
import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class ConFactory {
//获取连接
public static Connection getConnection(){
Connection con=null;
try{
Context initContext=new InitialContext();//得到初始化的上下文对象
DataSource ds=(DataSource)initContext.lookup("java:/comp/env/jdbc/abc");//根据配置的数据源名称得到数据源对象
con = ds.getConnection();//获取数据库连接
}catch(Exception e){}
return con;
}
//关闭连接
public static void close(Connection con){
try{
con.close();
}catch(Exception e){}
}
}
package com.mipo.dao;
import com.mipo.beans.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class DBC {
/**
* 登录:获取表单中用户名和密码与数据库中用户进行核对
* @param nam
* @param pwd
* @return
*/
public Users login(String nam, String pwd) {
Users u = new Users();
//创建连接对象
Connection conn = ConFactory.getConnection();
//查询用户
String sql = "select * from users where nam=? and pwd=?";
try {
//创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, nam);
ps.setString(2, pwd);
//在此 PreparedStatement 对象中执行 SQL 查询,并返回该查询生成的 ResultSet 对象。
ResultSet rs = ps.executeQuery();
//将光标从当前位置向前移一行。ResultSet 光标最初位于第一行之前;第一次调用 next 方法使第一行成为当前行;第二次调用使第二行成为当前行,依此类推。
rs.next();
u.setUid(rs.getInt("uid"));
u.setNam(rs.getString("nam"));
u.setPwd(rs.getString("pwd"));
} catch (Exception e) {
u = null;
} finally {
ConFactory.close(conn);
}
return u;
}
/**
* 查询当前登录者的所有联系人,并将查询结果进行分页
* 实现步骤:
* 1、查询登录者联系人个数
* 2、首先设置每页要显示的联系人个数,其次设置总的联系人个数,最后设置当前页为多少
* 3、在SQL语句中通过top关键字查询——第几页显示哪几条数据
* 4、创建contact对象,将contact对象中属性与查询结果一一匹配,将contact对象添加到用来存放本页数据的集合中
* @param uid
* @param p
* @return
*/
public PageBean myContact(int uid, int p) {
PageBean pb = new PageBean();
// 创建连接对象
Connection conn = ConFactory.getConnection();
// 查询记录总条数,并将值赋给变量mcc
String sql = "select count(1) mcc from contact where uid=" + uid;
try {
// 创建一个 Statement 对象来将 SQL 语句发送到数据库。不带参数的 SQL 语句通常使用 Statement 对象执行
// 如果多次执行相同的 SQL 语句,使用 PreparedStatement 对象可能更有效。
Statement st = conn.createStatement();
// 执行给定的 SQL 语句,该语句返回单个 ResultSet 对象
ResultSet rs1 = st.executeQuery(sql);
while (rs1.next()) {
pb.setPagesize(3);// 设置每页显示3条记录,首先调用
pb.setCount(rs1.getInt("mcc"));// 其次调用
pb.setP(p);// 最后调用,这三个方法一定注意调用顺序
}
rs1.close();// 关闭结果集
// 查询语句(太长了,通过这种方式写)
// 注意:SQL语句的格式必须与数据库中一致。注意空格,尤其字符串拼接时;当参数为字符串时,要加上''
sql = "select top " + pb.getPagesize() + " c.cid,c.nam,c.sex,c.tel,c.birth,t.tnam ";
sql += "from contact c,ctype t ";
sql += "where c.tid=t.tid and c.uid=" + uid + " and c.cid not in ";
sql += "(select top " + (pb.getP() - 1) * pb.getPagesize() + " c2.cid ";
sql += "from contact c2,ctype t2 ";
sql += "where c2.tid=t2.tid and c2.uid=" + uid + ") ";
System.out.println(sql);
ResultSet rs2 = st.executeQuery(sql);
while (rs2.next()) {
Contact c = new Contact();
c.setCid(rs2.getInt(1));
c.setNam(rs2.getString(2));
c.setSex(rs2.getString(3));
c.setTel(rs2.getString(4));
c.setBirth(rs2.getString(5));
c.setTnam(rs2.getString(6));
pb.addData(c);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConFactory.close(conn);
}
return pb;
}
/**
* 查询所有分组
* @return
*/
public List allType() {
List types = new ArrayList();
Connection conn = ConFactory.getConnection();
String sql = "select * from ctype";
try {
// 返回一个新的Statement对象,该对象将生成具有给定类型和并发性的ResultSet对象
Statement st = conn.createStatement(1005, 1008);
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
Ctype type = new Ctype();
type.setTid(rs.getInt(1));
type.setTnam(rs.getString(2));
types.add(type);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConFactory.close(conn);
}
return types;
}
/**
* 登陆者按条件进行模糊查询联系人,并将查询结果进行分页
* 实现步骤:
* 1、设置一个变量用来存放查询条件,对表单中数据进行判断,符合要求,则增加查询条件;否则查询条件为空
* 2、通过查询条件获得符合要求的联系人个数,SQL语句中用count(1)实现
* 3、首先设置每页要显示的联系人个数,其次设置符合条件的联系人总个数,最后设置当前页是第几页
* 4、在SQL语句中通过top关键字查询——第几页显示哪几条数据
* 5、创建contact对象,将contact对象中属性与查询结果一一匹配,将contact对象添加到用来存放本页数据的集合中
* @param ct
* @param p
* @return
*/
public PageBean findContact(Contact ct, int p) {
PageBean pb = new PageBean();
Connection conn = ConFactory.getConnection();
String condition = "";// 用于存放查询的条件
if (null != ct.getNam() && !ct.getNam().equals("") && !ct.getNam().equals("null")) {// 对联系人姓名进行模糊查询
condition += " and c.nam like '%" + ct.getNam() + "%'";
}
if (null != ct.getSex() && !ct.getSex().equals("")) {// 对联系人性别进行查询
condition += " and c.sex='" + ct.getSex() + "'";
}
if (null != ct.getBirth() && !ct.getBirth().equals("") && !ct.getBirth().equals("null")) {// 对联系人生日进行查询
condition += " and c.birth='" + ct.getBirth() + "'";
}
if (0 != ct.getTid()) {// 对组别进行查询
condition += " and c.tid=" + ct.getTid();
}
//将查询的个数值赋给变量mcc
String sql = "select count(1) mcc from contact c,ctype t ";
sql += "where c.tid=t.tid and c.uid=" + ct.getUid();
sql += condition;// 查询记录总条数的SQL语句
try {
Statement st = conn.createStatement();
ResultSet rs1 = st.executeQuery(sql);
while (rs1.next()) {
pb.setPagesize(3);// 设置每页显示3条记录,首先调用
pb.setCount(rs1.getInt("mcc"));// 获取总记录数量,其次调用
pb.setP(p);// 设定当前页的范围,最后调用,注意此三者的顺序
}
rs1.close();// 关闭结果集
sql = "select top " + pb.getPagesize() + " c.cid,c.nam,c.sex,c.tel,c.birth,t.tnam ";
sql += "from contact c,ctype t ";
sql += "where c.tid=t.tid and c.uid=" + ct.getUid() + " ";
sql += condition;// 父查询添加查询条件
sql += "and c.cid not in ";
sql += "(select top " + (pb.getP() - 1) * pb.getPagesize() + " c2.cid ";
sql += "from contact c2,ctype t2 ";
sql += "where c2.tid=t2.tid and c2.uid=" + ct.getUid() + " ";
condition = condition.replace("c", "c2");// 替换查询条件中的别名
sql += condition;// 子查询添加查询条件
sql += ") ";
System.out.println(sql);
st = conn.createStatement(1005, 1008);
ResultSet rs2 = st.executeQuery(sql);
while (rs2.next()) {
Contact c = new Contact();
c.setCid(rs2.getInt("cid"));
c.setNam(rs2.getString("nam"));
c.setSex(rs2.getString("sex"));
c.setTel(rs2.getString("tel"));
c.setBirth(rs2.getString("birth"));
c.setTnam(rs2.getString("tnam"));
pb.addData(c);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConFactory.close(conn);
}
return pb;
}
/**
* 单表查询,查询所有联系人,并将查询结果进行分页
* @param p
* @return
*/
public PageBean allContact(int p) {
PageBean pb = new PageBean();
Connection conn = ConFactory.getConnection();
String sql = "select count(1) acc from contact";
try {
Statement st = conn.createStatement();
ResultSet rs1 = st.executeQuery(sql);
while (rs1.next()) {
pb.setPagesize(3);
pb.setCount(rs1.getInt("acc"));
pb.setP(p);
}
rs1.close();
sql = "select top " + pb.getPagesize() + " * from contact ";
sql += "where cid not in ";
sql += "(select top " + (pb.getP() - 1) * pb.getPagesize() + " cid from contact)";
ResultSet rs2 = st.executeQuery(sql);
while (rs2.next()) {
Contact ct = new Contact();
ct.setCid(rs2.getInt("cid"));
ct.setNam(rs2.getString("nam"));
ct.setSex(rs2.getString("sex"));
ct.setTel(rs2.getString("tel"));
ct.setBirth(rs2.getString("birth"));
ct.setTid(rs2.getInt("tid"));
pb.addData(ct);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConFactory.close(conn);
}
return pb;
}
}
Servlet
package com.mipo.ser;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mipo.beans.*;
import com.mipo.dao.DBC;
public class Controler extends HttpServlet {
//初始化
public void init() throws ServletException {
}
//处理Get请求:表单提交(默认get方式)、地址栏访问和超链接都是方式。
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//将get请求交给doPost()进行处理
doPost(request, response);
}
//处理Post请求
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//通过设置参数来进行多次请求,不同的参数访问不同的页面,避免写很多servlet
String cmd = request.getParameter("cmd");
if (cmd.equals("login")) {
login(request, response);
} else if (cmd.equals("allContact")) {
allContact(request, response);
} else if (cmd.equals("myContact")) {
myContact(request, response);
} else if (cmd.equals("findContact")) {
findContact(request, response);
}
}
// 登录
public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DBC dbc = new DBC();
//获取表单中参数名对应的参数值
String nam = request.getParameter("nam");
String pwd = request.getParameter("pwd");
Users u = dbc.login(nam, pwd);
//如果用户存在且密码正确
if (u != null) {
//将users对象保存到Session中
request.getSession().setAttribute("u", u);
//重定向到servlet中
response.sendRedirect(request.getContextPath() + "/con?cmd=myContact");
} else
//否则,重定向到登陆页面
response.sendRedirect("index.jsp");
}
// 查询所有联系人并分页(单表查询)
public void allContact(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DBC dbc = new DBC();
int currentPage = 1;// 当前页,默认显示第一页
String pn = request.getParameter("p");// 获取页面中传递过来的页码
if (null != pn && !"".equals(pn)) {
currentPage = Integer.parseInt(pn);
}
PageBean pb = dbc.allContact(currentPage);
//将PageBean对象保存到request中
request.setAttribute("pb", pb);
//转发到allContact.jsp页面
request.getRequestDispatcher("allContact.jsp").forward(request, response);
}
// 查询当前登录者的联系人并分页(多表查询)
public void myContact(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DBC dbc = new DBC();
int currentPage = 1;// 当前页,默认显示第一页
String pn = request.getParameter("p");// 获取页面中传递过来的页码
// 第一次进入页面pn=null
if (null != pn && !"".equals(pn)) {
currentPage = Integer.parseInt(pn);
}
//获取Session中的users对象
Users user = (Users) request.getSession().getAttribute("u");
PageBean pb = dbc.myContact(user.getUid(), currentPage);
//将PageBean对象保存到request中
request.setAttribute("pb", pb);
//转发到myContact.jsp页面
request.getRequestDispatcher("myContact.jsp").forward(request, response);
}
// 对当前登陆者的联系人进行模糊查询并分页(多表查询)
public void findContact(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DBC dbc = new DBC();
int currentPage = 1;
String pn = request.getParameter("p");
if (null != pn && !"".equals(pn)) {
currentPage = Integer.parseInt(pn);
}
Contact ct = new Contact();
//获取表单中参数名对应的参数值
String nam = request.getParameter("nam");
String sex = request.getParameter("sex");
String birth = request.getParameter("birth");
//将参数值赋给contact对象中对应的属性
ct.setNam(nam);
ct.setSex(sex);
ct.setBirth(birth);
int tid = 0;// 默认没有选择组别
if (null != request.getParameter("tid")) {
tid = Integer.parseInt(request.getParameter("tid"));
}
ct.setTid(tid);
//获取Session中的users对象
Users user = (Users) request.getSession().getAttribute("u");
ct.setUid(user.getUid());
//将contact对象与当前页作为参数传到findContact()方法中
PageBean pb = dbc.findContact(ct, currentPage);
//所有分组
List types = dbc.allType();
request.setAttribute("pb", pb);
request.setAttribute("types", types);
//转发到findContact.jsp页面
request.getRequestDispatcher("findContact.jsp").forward(request, response);
}
//销毁
public void destroy() {
}
}
tools
package com.mipo.tools;
import java.io.IOException;
import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
public class Test extends HttpServlet {
public void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
try{
Context initContext=new InitialContext();//得到初始化的上下文对象
//根据配置的数据源名称得到数据源对象
DataSource ds=(DataSource)initContext.lookup("java:/comp/env/jdbc/abc");
Connection con = ds.getConnection();//获取数据库连接
con.close();
}catch(Exception e){e.printStackTrace();}
}
}
package com.mipo.tools;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UTFFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest)arg0;
HttpServletResponse res=(HttpServletResponse)arg1;
req.setCharacterEncoding("UTF-8");
res.setContentType("text/html;charset=UTF-8");
arg2.doFilter(req,res);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>JNDI-Test</display-name>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>com.mipo.tools.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>cen</servlet-name>
<servlet-class>com.mipo.ser.Controler</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cen</servlet-name>
<url-pattern>/con</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encoder</filter-name>
<filter-class>com.mipo.tools.UTFFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoder</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<resource-ref>
<res-ref-name>jdbc/abc</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
index.jsp
<%@ page contentType="text/html;charset=UTF-8"%>
<html>
<head>
<title>通讯录</title>
<link rel="stylesheet" type="text/css" href="common.css">
</head>
<body>
<center>
<h1>通讯录</h1>
<!-- con与web.xml中<servlet-mapping>的<url-pattern>对应 -->
<form name="frm" action="con" method=post>
<!-- 隐藏域,用来传递参数 -->
<input type=hidden name="cmd" value="login">
<table cellspacing=1 width=300>
<tr>
<th class=right>用户名</th>
<td class=left><input type=text name="nam"></td>
</tr>
<tr>
<th class=right>密码</th>
<td class=left><input type=password name="pwd"></td>
</tr>
<tr>
<th colspan=2>
<input type=reset value="重 置">
<input type=submit value="登 录"></th>
</tr>
</table>
</form>
</center>
</body>
</html>
myContact.jsp
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="java.util.*"%>
<%@ page import="com.mipo.beans.*"%>
<html>
<head>
<title>我的联系人</title>
<link rel="stylesheet" type="text/css" href="common.css">
</head>
<script language="javascript" src="time.js" charset="UTF-8"></script>
<body>
<center>
<h3>我的联系人</h3>
<a href=con?cmd=findContact>查找联系人</a>
<table cellspacing=1 width=450>
<tr>
<th>姓名</th>
<th>性别</th>
<th>电话</th>
<th>出生日期</th>
<th>组别</th>
<th>操作</th>
</tr>
<%
PageBean pb=(PageBean)request.getAttribute("pb");
List tacts=pb.getData();
for(int i=0;i<tacts.size();i++){
Contact c=(Contact)tacts.get(i);
%>
<tr>
<td><%=c.getNam() %></td>
<td><%=c.getSex() %></td>
<td><%=c.getTel() %></td>
<td><%=c.getBirth() %></td>
<td><%=c.getTnam() %></td>
<td>
<a href="#">修改</a>
<a href="#">删除</a>
</td>
</tr>
<%}%>
</table><br>
共<%=pb.getCount()%>条记录,
第<%=pb.getP()%>页/共<%=pb.getPagetotal()%>页
<a href="con?cmd=myContact&p=1">首页</a>
<a href="con?cmd=myContact&p=<%=pb.getP()-1%>">上一页</a>
<a href="con?cmd=myContact&p=<%=pb.getP()+1%>">下一页</a>
<a href="con?cmd=myContact&p=<%=pb.getPagetotal()%>">尾页</a>
跳到第:
<!-- onchange 在元素值改变时触发。onchange 属性适用于:<input>、<textarea> 以及 <select> 元素。 -->
<select id="secpage" οnchange=toPage(this.value)>
<script language="javascript">
function toPage(a){
/* Location 对象存储在 Window 对象的 Location 属性中,表示那个窗口中当前显示的文档的 Web 地址。 */
location="con?cmd=myContact&p="+a;
}
for(i=1;i<=<%=pb.getPagetotal()%>;i++){
document.write("<option value="+i+">"+i+"</option>");
}
document.getElementById("secpage").value=<%=pb.getP()%>
</script>
</select>页
</center>
</body>
</html>
findContact.jsp
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="java.util.*"%>
<%@ page import="com.mipo.beans.*"%>
<html>
<head>
<title>查找联系人</title>
<link rel="stylesheet" type="text/css" href="common.css">
</head>
<script language="javascript" src="time.js" charset="UTF-8"></script>
<body>
<center>
<h3>我的联系人</h3>
<form name="frm" action="con" method="post">
<!-- 隐藏域,用来传递参数 -->
<input type=hidden name="cmd" value="findContact">
<table width=450 cellspacing=1>
<tr>
<th class=right>姓名</th>
<td class=left><input type=text name="nam" value="<%=request.getParameter("nam")%>"></td>
<th class=right>出生日期</th>
<td class=left>
<input type=text name="birth" readonly οnclick=setday(this) value="<%=request.getParameter("birth")%>">
</td>
</tr>
<tr>
<th class=right>性别</th>
<td class=left>
<select name="sex">
<option value="">请选择</option>
<!-- 如果request中获取的参数值为"男",则男被选中(selected) -->
<option value="男" <%="男".equals(request.getParameter("sex"))?"selected":""%>>男</option>
<option value="女" <%="女".equals(request.getParameter("sex"))?"selected":""%>>女</option>
</select>
</td>
<th class=right>分组</th>
<td class=left>
<select name="tid">
<option value="0">请选择</option>
<%
List types=(List)request.getAttribute("types");
for(int i=0;i<types.size();i++){
Ctype t=(Ctype)types.get(i);
%>
<option value="<%=t.getTid()%>"
<%=(t.getTid()+"").equals(request.getParameter("tid"))?"selected":""%>
><%=t.getTnam()%></option>
<%}%>
</select>
</td>
</tr>
<tr>
<th colspan=4><input type=submit value="查 询"></th>
</tr>
</table>
</form>
<table cellspacing=1 width=450>
<tr>
<th>姓名</th>
<th>性别</th>
<th>电话</th>
<th>出生日期</th>
<th>组别</th>
<th>操作</th>
</tr>
<%
PageBean pb=(PageBean)request.getAttribute("pb");
List tacts=pb.getData();
for(int i=0;i<tacts.size();i++){
Contact c=(Contact)tacts.get(i);
%>
<tr>
<td><%=c.getNam() %></td>
<td><%=c.getSex() %></td>
<td><%=c.getTel() %></td>
<td><%=c.getBirth() %></td>
<td><%=c.getTnam() %></td>
<td>
<a href="#">修改</a>
<a href="#">删除</a>
</td>
</tr>
<%}%>
</table><br>
共<%=pb.getCount()%>条记录,
第<%=pb.getP()%>页/共<%=pb.getPagetotal()%>页
<!-- 这种链接方式不会清空request中的内容 -->
<a href="javascript:toPage(1);">首页</a>
<a href="javascript:toPage(<%=pb.getP()-1%>);">上一页</a>
<a href="javascript:toPage(<%=pb.getP()+1%>);">下一页</a>
<a href="javascript:toPage(<%=pb.getPagetotal()%>);">尾页</a>
跳到第:
<!-- onchange 在元素值改变时触发 -->
<select id="secpage" οnchange=toPage(this.value)>
<script language="javascript">
function toPage(a){
/* javascript提交表单,通过这种方式又可以获取表单中的数据,通过request可以获取表单中数据,
而myContact中通过超链接跳转会清空request中的内容 */
document.frm.action="con?p="+a;
document.frm.submit();
}
for(i=1;i<=<%=pb.getPagetotal()%>;i++){
document.write("<option value="+i+">"+i+"</option>");
}
document.getElementById("secpage").value=<%=pb.getP()%>;
</script>
</select>页
<%-- 这种方式会清空request的内容,当你点击下一页时,会显示所有我的联系人,因为此时查询条件不存在,查询是所有
<a href="con?cmd=findContact&p=1">首页</a>
<a href="con?cmd=findContact&p=<%=pb.getP()-1%>">上一页</a>
<a href="con?cmd=findContact&p=<%=pb.getP()+1%>">下一页</a>
<a href="con?cmd=findContact&p=<%=pb.getPagetotal()%>">尾页</a>
跳到第:
<select id="secpage" οnchange=toPage(this.value)>
<script language="javascript">
function toPage(a){
location="con?cmd=findContact&p="+a;
}
for(i=1;i<=<%=pb.getPagetotal()%>;i++){
document.write("<option value="+i+">"+i+"</option>");
}
document.getElementById("secpage").value=<%=pb.getP()%>
</script>
</select>页
--%>
</center>
</body>
</html>
allContact.jsp
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="java.util.*"%>
<%@ page import="com.mipo.beans.*"%>
<html>
<head>
<title>所有联系人</title>
<link rel="stylesheet" type="text/css" href="common.css">
</head>
<script language="javascript" src="time.js" charset="UTF-8"></script>
<body>
<center>
<h3>所有联系人</h3>
<table cellspacing=1 width=450>
<tr>
<th>姓名</th>
<th>性别</th>
<th>电话</th>
<th>出生日期</th>
<th>组别</th>
<th>操作</th>
</tr>
<%
PageBean pb=(PageBean)request.getAttribute("pb");
List tacts=pb.getData();
for(int i=0;i<tacts.size();i++){
Contact c=(Contact)tacts.get(i);
%>
<tr>
<td><%=c.getNam() %></td>
<td><%=c.getSex() %></td>
<td><%=c.getTel() %></td>
<td><%=c.getBirth() %></td>
<td><%=c.getTnam() %></td>
<td>
<a href="#">修改</a>
<a href="#">删除</a>
</td>
</tr>
<%}%>
</table><br>
共<%=pb.getCount()%>条记录,
第<%=pb.getP()%>页/共<%=pb.getPagetotal()%>页
<a href="con?cmd=allContact&p=1">首页</a>
<a href="con?cmd=allContact&p=<%=pb.getP()-1%>">上一页</a>
<a href="con?cmd=allContact&p=<%=pb.getP()+1%>">下一页</a>
<a href="con?cmd=allContact&p=<%=pb.getPagetotal()%>">尾页</a>
跳到第:
<!-- onchange 在元素值改变时触发。onchange 属性适用于:<input>、<textarea> 以及 <select> 元素。 -->
<select id="secpage" οnchange=toPage(this.value)>
<script language="javascript">
function toPage(a){
/* Location 对象存储在 Window 对象的 Location 属性中,表示那个窗口中当前显示的文档的 Web 地址。 */
location="con?cmd=allContact&p="+a;
}
for(i=1;i<=<%=pb.getPagetotal()%>;i++){
document.write("<option value="+i+">"+i+"</option>");
}
document.getElementById("secpage").value=<%=pb.getP()%>
</script>
</select>页
</center>
</body>
</html>
common.css
body{
font-size:10pt;
}
table{
font-size:10pt;
border-width:0px;
background-color:#666666;
}
th{
height:25px;
background-color:#cccccc;
}
tr{
height:25px;
background-color:white;
text-align:center;
}
td{
text-align:center;
}
input{
border:1px black groove;
height:20px;
}
.nob{
border:0px;
}
textarea{
border:1px black solid;
}
.left{
text-align:left;
padding-left:5px;
}
.right{
text-align:right;
padding-right:5px;
}
.txt{
text-align:left;
vertical-align:top;
}
time.js
//more javascript from http://www.smallrain.net
//==================================================== 参数设定部分 =======================================================
var bMoveable=true; //设置日历是否可以拖动
var _VersionInfo="Version:2.0" //版本信息
//==================================================== WEB 页面显示部分 =====================================================
var strFrame;
document.writeln('<iframe bgcolor="#000000" id=meizzDateLayer Author=wayx frameborder=0 style="position: absolute; width: 186; height: 247; z-index: 9998; display: none"></iframe>');
strFrame='<style>';
strFrame+='INPUT.button{BORDER-RIGHT: #B3C9E1 1px solid;BORDER-TOP: #B3C9E1 1px solid;BORDER-LEFT: #B3C9E1 1px solid;';
strFrame+='BORDER-BOTTOM: #ff9900 1px solid;BACKGROUND-COLOR: #EDF2F8;font-family:宋体;}';
strFrame+='TD{FONT-SIZE: 9pt;font-family:宋体;}';
strFrame+='</style>';
strFrame+='<scr' + 'ipt>';
strFrame+='var datelayerx,datelayery; /*存放日历控件的鼠标位置*/';
strFrame+='var bDrag; /*标记是否开始拖动*/';
strFrame+='function document.onmousemove() /*在鼠标移动事件中,如果开始拖动日历,则移动日历*/';
strFrame+='{if(bDrag && window.event.button==1)';
strFrame+=' {var DateLayer=parent.document.all.meizzDateLayer.style;';
strFrame+=' DateLayer.posLeft += window.event.clientX-datelayerx;/*由于每次移动以后鼠标位置都恢复为初始的位置,因此写法与div中不同*/';
strFrame+=' DateLayer.posTop += window.event.clientY-datelayery;}}';
strFrame+='function DragStart() /*开始日历拖动*/';
strFrame+='{var DateLayer=parent.document.all.meizzDateLayer.style;';
strFrame+=' datelayerx=window.event.clientX;';
strFrame+=' datelayery=window.event.clientY;';
strFrame+=' bDrag=true;}';
strFrame+='function DragEnd(){ /*结束日历拖动*/';
strFrame+=' bDrag=false;}';
strFrame+='</scr' + 'ipt>';
strFrame+='<div style="z-index:9999;position: absolute; left:0; top:0;" onselectstart="return false"><span id=tmpSelectYearLayer Author=wayx style="z-index: 9999;position: absolute;top: 3; left: 19;display: none"></span>';
strFrame+='<span id=tmpSelectMonthLayer Author=wayx style="z-index: 9999;position: absolute;top: 3; left: 78;display: none"></span>';
strFrame+='<table style="FILTER:dropshadow(color=#EDEDF8,offx=3.3,offy=3.3,positive=1);" cellSpacing="0" cellPadding="0" width="100%" border="0"><tr><td>';
// 控件边框颜色
strFrame+='<table border=1 cellspacing=0 cellpadding=0 width=182 height=160 bgColor="#FFFFFF" borderColorLight=#7197CA borderColorDark="#ffffff" Author="wayx">';
strFrame+=' <tr Author="wayx"><td width=182 height=23 Author="wayx" bgcolor=#FFFFFF><table border=0 cellspacing=1 cellpadding=0 width=180 Author="wayx" height=23>';
strFrame+=' <tr align=center Author="wayx"><td width=16 align=center bgcolor=#B6CAE4 style="font-size:12px;cursor: hand;color: #ffffff" ';
strFrame+=' οnclick="parent.meizzPrevM()" title="向前翻 1 月" Author=meizz><b Author=meizz><</b>';
strFrame+=' </td><td width=60 align=center style="font-size:12px;cursor:default" Author=meizz ';
strFrame+='οnmοuseοver="style.backgroundColor=\'#D7E1F0\'" οnmοuseοut="style.backgroundColor=\'white\'" ';
strFrame+='οnclick="parent.tmpSelectYearInnerHTML(this.innerText.substring(0,4))" title="点击这里选择年份"><span Author=meizz id=meizzYearHead></span></td>';
strFrame+='<td width=48 align=center style="font-size:12px;cursor:default" Author=meizz οnmοuseοver="style.backgroundColor=\'#D7E1F0\'" ';
strFrame+=' οnmοuseοut="style.backgroundColor=\'white\'" οnclick="parent.tmpSelectMonthInnerHTML(this.innerText.length==3?this.innerText.substring(0,1):this.innerText.substring(0,2))"';
strFrame+=' title="点击这里选择月份"><span id=meizzMonthHead Author=meizz></span></td>';
strFrame+=' <td width=16 bgcolor=#B6CAE4 align=center style="font-size:12px;cursor: hand;color: #ffffff" ';
strFrame+=' οnclick="parent.meizzNextM()" title="向后翻 1 月" Author=meizz><b Author=meizz>></b></td></tr>';
strFrame+=' </table></td></tr>';
strFrame+=' <tr Author="wayx"><td width=180 height=18 Author="wayx">';
strFrame+='<table border=1 cellspacing=0 cellpadding=0 bgcolor=#618BC5 ' + (bMoveable? 'οnmοusedοwn="DragStart()" οnmοuseup="DragEnd()"':'');
strFrame+=' BORDERCOLORLIGHT=#3677b1 bgcolor=#5168C8 BORDERCOLORDARK=#FFFFFF width="100%" height=25 Author="wayx" style="cursor:' + (bMoveable ? 'move':'default') + '">';
strFrame+='<tr Author="wayx" valign="middle" align="center"><td style="font-size:12px;color:#FFFFFF" Author=meizz><b>日</b></td>';
strFrame+='<td style="font-size:12px;color:#FFFFFF" Author=meizz><b>一</b></td><td style="font-size:12px;color:#FFFFFF" Author=meizz><b>二</b></td>';
strFrame+='<td style="font-size:12px;color:#FFFFFF" Author=meizz><b>三</b></td><td style="font-size:12px;color:#FFFFFF" Author=meizz><b>四</b></td>';
strFrame+='<td style="font-size:12px;color:#FFFFFF" Author=meizz><b>五</b></td><td style="font-size:12px;color:#FFFFFF" Author=meizz><b>六</b></td></tr>';
strFrame+='</table></td></tr><!-- Author:F.R.Huang(meizz) http://www.meizz.com/ mail: meizz@hzcnc.com 2002-10-8 -->';
strFrame+=' <tr Author="wayx"><td width="100%" height=120 Author="wayx">';
strFrame+=' <table border=1 cellspacing=2 cellpadding=0 borderColorDark=#ffffff bgColor=#FFFFFF borderColorLight=#83A4D1 width="100%" height=120 Author="wayx">';
var n=0; for (j=0;j<5;j++){ strFrame+= ' <tr align=center Author="wayx">'; for (i=0;i<7;i++){
strFrame+='<td width=25 height=25 id=meizzDay'+n+' style="font-size:12px" Author=meizz οnclick=parent.meizzDayClick(this.innerText,0)></td>';n++;}
strFrame+='</tr>';}
strFrame+=' <tr align=center Author="wayx">';
for (i=35;i<39;i++)strFrame+='<td width=25 height=25 id=meizzDay'+i+' style="font-size:12px" Author=wayx οnclick="parent.meizzDayClick(this.innerText,0)"></td>';
strFrame+=' <td colspan=3 align=right Author=meizz><span οnclick=parent.closeLayer() style="font-size:12px;cursor: hand"';
strFrame+=' Author=meizz title="' + _VersionInfo + '"><u>关闭</u></span> </td></tr>';
strFrame+=' </table></td></tr><tr Author="wayx"><td Author="wayx">';
strFrame+=' <table border=0 cellspacing=1 cellpadding=0 width=100% Author="wayx" bgcolor=#FFFFFF>';
strFrame+=' <tr Author="wayx"><td Author=meizz align=left><input Author=meizz type=button class=button style="cursor:hand" value="<<" title="向前翻 1 年" οnclick="parent.meizzPrevY()" ';
strFrame+=' οnfοcus="this.blur()" style="font-size: 12px; height: 20px"><input Author=meizz class=button title="向前翻 1 月" type=button ';
strFrame+=' value="< " style="cursor:hand" οnclick="parent.meizzPrevM()" οnfοcus="this.blur()" style="font-size: 12px; height: 20px"></td><td ';
strFrame+=' Author=meizz align=center><input Author=meizz style="cursor:hand" type=button class=button value=Today οnclick="parent.meizzToday()" ';
strFrame+=' οnfοcus="this.blur()" title="当前日期" style="font-size: 12px; height: 20px; cursor:hand"></td><td ';
strFrame+=' Author=meizz align=right><input Author=meizz type=button class=button value=" >" style="cursor:hand" οnclick="parent.meizzNextM()" ';
strFrame+=' οnfοcus="this.blur()" title="向后翻 1 月" class=button style="font-size: 12px; height: 20px"><input ';
strFrame+=' Author=meizz type=button class=button style="cursor:hand" value=">>" title="向后翻 1 年" οnclick="parent.meizzNextY()"';
strFrame+=' οnfοcus="this.blur()" style="font-size: 12px; height: 20px"></td>';
strFrame+='</tr></table></td></tr></table></td></tr></table></div>';
window.frames.meizzDateLayer.document.writeln(strFrame);
window.frames.meizzDateLayer.document.close(); //解决ie进度条不结束的问题
//==================================================== WEB 页面显示部分 ======================================================
var outObject;
var outButton; //点击的按钮
var outDate=""; //存放对象的日期
var odatelayer=window.frames.meizzDateLayer.document.all; //存放日历对象
function setday(tt,obj) //主调函数
{
if (arguments.length > 2){alert("对不起!传入本控件的参数太多!");return;}
if (arguments.length == 0){alert("对不起!您没有传回本控件任何参数!");return;}
var dads = document.all.meizzDateLayer.style;
var th = tt;
var ttop = tt.offsetTop; //TT控件的定位点高
var thei = tt.clientHeight; //TT控件本身的高
var tleft = tt.offsetLeft; //TT控件的定位点宽
var ttyp = tt.type; //TT控件的类型
while (tt = tt.offsetParent){ttop+=tt.offsetTop; tleft+=tt.offsetLeft;}
dads.top = (ttyp=="image")? ttop+thei : ttop+thei+6;
dads.left = tleft;
outObject = (arguments.length == 1) ? th : obj;
outButton = (arguments.length == 1) ? null : th; //设定外部点击的按钮
//根据当前输入框的日期显示日历的年月
var reg = /^(\d+)-(\d{1,2})-(\d{1,2})$/;
var r = outObject.value.match(reg);
if(r!=null){
r[2]=r[2]-1;
var d= new Date(r[1], r[2],r[3]);
if(d.getFullYear()==r[1] && d.getMonth()==r[2] && d.getDate()==r[3]){
outDate=d; //保存外部传入的日期
}
else outDate="";
meizzSetDay(r[1],r[2]+1);
}
else{
outDate="";
meizzSetDay(new Date().getFullYear(), new Date().getMonth() + 1);
}
dads.display = '';
event.returnValue=false;
}
var MonHead = new Array(12); //定义阳历中每个月的最大天数
MonHead[0] = 31; MonHead[1] = 28; MonHead[2] = 31; MonHead[3] = 30; MonHead[4] = 31; MonHead[5] = 30;
MonHead[6] = 31; MonHead[7] = 31; MonHead[8] = 30; MonHead[9] = 31; MonHead[10] = 30; MonHead[11] = 31;
var meizzTheYear=new Date().getFullYear(); //定义年的变量的初始值
var meizzTheMonth=new Date().getMonth()+1; //定义月的变量的初始值
var meizzWDay=new Array(39); //定义写日期的数组
function onclick(){
with(window.event)
{ if (srcElement.getAttribute("Author")==null && srcElement != outObject && srcElement != outButton)
closeLayer();
}
}
function onkeyup() //按Esc键关闭,切换焦点关闭
{
if (window.event.keyCode==27){
if(outObject)outObject.blur();
closeLayer();
}
else if(document.activeElement)
if(document.activeElement.getAttribute("Author")==null && document.activeElement != outObject && document.activeElement != outButton)
{
closeLayer();
}
}
function meizzWriteHead(yy,mm) //往 head 中写入当前的年与月
{
odatelayer.meizzYearHead.innerText = yy + " 年";
odatelayer.meizzMonthHead.innerText = mm + " 月";
}
function tmpSelectYearInnerHTML(strYear) //年份的下拉框
{
if (strYear.match(/\D/)!=null){alert("年份输入参数不是数字!");return;}
var m = (strYear) ? strYear : new Date().getFullYear();
if (m < 1000 || m > 9999) {alert("年份值不在 1000 到 9999 之间!");return;}
var n = m - 50;
if (n < 1000) n = 1000;
if (n + 26 > 9999) n = 9974;
var s = " <select Author=meizz name=tmpSelectYear style='font-size: 12px' "
s += "οnblur='document.all.tmpSelectYearLayer.style.display=\"none\"' "
s += "οnchange='document.all.tmpSelectYearLayer.style.display=\"none\";"
s += "parent.meizzTheYear = this.value; parent.meizzSetDay(parent.meizzTheYear,parent.meizzTheMonth)'>\r\n";
var selectInnerHTML = s;
for (var i = n; i < n + 80; i++)
{
if (i == m)
{selectInnerHTML += "<option Author=wayx value='" + i + "' selected>" + i + "年" + "</option>\r\n";}
else {selectInnerHTML += "<option Author=wayx value='" + i + "'>" + i + "年" + "</option>\r\n";}
}
selectInnerHTML += "</select>";
odatelayer.tmpSelectYearLayer.style.display="";
odatelayer.tmpSelectYearLayer.innerHTML = selectInnerHTML;
odatelayer.tmpSelectYear.focus();
}
function tmpSelectMonthInnerHTML(strMonth) //月份的下拉框
{
if (strMonth.match(/\D/)!=null){alert("月份输入参数不是数字!");return;}
var m = (strMonth) ? strMonth : new Date().getMonth() + 1;
var s = " <select Author=meizz name=tmpSelectMonth style='font-size: 12px' "
s += "οnblur='document.all.tmpSelectMonthLayer.style.display=\"none\"' "
s += "οnchange='document.all.tmpSelectMonthLayer.style.display=\"none\";"
s += "parent.meizzTheMonth = this.value; parent.meizzSetDay(parent.meizzTheYear,parent.meizzTheMonth)'>\r\n";
var selectInnerHTML = s;
for (var i = 1; i < 13; i++)
{
if (i == m)
{selectInnerHTML += "<option Author=wayx value='"+i+"' selected>"+i+"月"+"</option>\r\n";}
else {selectInnerHTML += "<option Author=wayx value='"+i+"'>"+i+"月"+"</option>\r\n";}
}
selectInnerHTML += "</select>";
odatelayer.tmpSelectMonthLayer.style.display="";
odatelayer.tmpSelectMonthLayer.innerHTML = selectInnerHTML;
odatelayer.tmpSelectMonth.focus();
}
function closeLayer() //这个层的关闭
{
document.all.meizzDateLayer.style.display="none";
}
function IsPinYear(year) //判断是否闰平年
{
if (0==year%4&&((year%100!=0)||(year%400==0))) return true;else return false;
}
function GetMonthCount(year,month) //闰年二月为29天
{
var c=MonHead[month-1];if((month==2)&&IsPinYear(year)) c++;return c;
}
function GetDOW(day,month,year) //求某天的星期几
{
var dt=new Date(year,month-1,day).getDay()/7; return dt;
}
function meizzPrevY() //往前翻 Year
{
if(meizzTheYear > 999 && meizzTheYear <10000){meizzTheYear--;}
else{alert("年份超出范围(1000-9999)!");}
meizzSetDay(meizzTheYear,meizzTheMonth);
}
function meizzNextY() //往后翻 Year
{
if(meizzTheYear > 999 && meizzTheYear <10000){meizzTheYear++;}
else{alert("年份超出范围(1000-9999)!");}
meizzSetDay(meizzTheYear,meizzTheMonth);
}
function meizzToday() //Today Button
{
var today;
meizzTheYear = new Date().getFullYear();
meizzTheMonth = new Date().getMonth()+1;
today=new Date().getDate();
//meizzSetDay(meizzTheYear,meizzTheMonth);
if(outObject){
outObject.value=meizzTheYear + "-" + meizzTheMonth + "-" + today;
}
closeLayer();
}
function meizzPrevM() //往前翻月份
{
if(meizzTheMonth>1){meizzTheMonth--}else{meizzTheYear--;meizzTheMonth=12;}
meizzSetDay(meizzTheYear,meizzTheMonth);
}
function meizzNextM() //往后翻月份
{
if(meizzTheMonth==12){meizzTheYear++;meizzTheMonth=1}else{meizzTheMonth++}
meizzSetDay(meizzTheYear,meizzTheMonth);
}
function meizzSetDay(yy,mm) //主要的写程序**********
{
meizzWriteHead(yy,mm);
//设置当前年月的公共变量为传入值
meizzTheYear=yy;
meizzTheMonth=mm;
for (var i = 0; i < 39; i++){meizzWDay[i]=""}; //将显示框的内容全部清空
var day1 = 1,day2=1,firstday = new Date(yy,mm-1,1).getDay(); //某月第一天的星期几
for (i=0;i<firstday;i++)meizzWDay[i]=GetMonthCount(mm==1?yy-1:yy,mm==1?12:mm-1)-firstday+i+1 //上个月的最后几天
for (i = firstday; day1 < GetMonthCount(yy,mm)+1; i++){meizzWDay[i]=day1;day1++;}
for (i=firstday+GetMonthCount(yy,mm);i<39;i++){meizzWDay[i]=day2;day2++}
for (i = 0; i < 39; i++)
{ var da = eval("odatelayer.meizzDay"+i) //书写新的一个月的日期星期排列
if (meizzWDay[i]!="")
{
//初始化边框
da.borderColorLight="#76A0CF";
da.borderColorDark="#76A0CF";
if(i<firstday) //上个月的部分
{
da.innerHTML="<font style=' color: #B5C5D2;'>" + meizzWDay[i] + "</font>";
da.title=(mm==1?12:mm-1) +"月" + meizzWDay[i] + "日";
da.οnclick=Function("meizzDayClick(this.innerText,-1)");
if(!outDate)
da.style.backgroundColor = ((mm==1?yy-1:yy) == new Date().getFullYear() &&
(mm==1?12:mm-1) == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate()) ?
"#E4E3F2":"#FFFFFF";
else
{
da.style.backgroundColor =((mm==1?yy-1:yy)==outDate.getFullYear() && (mm==1?12:mm-1)== outDate.getMonth() + 1 &&
meizzWDay[i]==outDate.getDate())? "#E8F5E7" : // 选中日期颜色
(((mm==1?yy-1:yy) == new Date().getFullYear() && (mm==1?12:mm-1) == new Date().getMonth()+1 &&
meizzWDay[i] == new Date().getDate()) ? "#E4E3F2":"#FFFFFF"); // 当前系统时间颜色
//将选中的日期显示为凹下去
if((mm==1?yy-1:yy)==outDate.getFullYear() && (mm==1?12:mm-1)== outDate.getMonth() + 1 &&
meizzWDay[i]==outDate.getDate())
{
//da.borderColorLight="#E4E3F2";
// da.borderColorDark="#E4E3F2"; // 选择日期边框颜色
}
}
}
else if (i>=firstday+GetMonthCount(yy,mm)) //下个月的部分
{
da.innerHTML="<font style=' color: #B5C5D2;'>" + meizzWDay[i] + "</font>";
da.title=(mm==12?1:mm+1) +"月" + meizzWDay[i] + "日";
da.οnclick=Function("meizzDayClick(this.innerText,1)");
if(!outDate)
da.style.backgroundColor = ((mm==12?yy+1:yy) == new Date().getFullYear() &&
(mm==12?1:mm+1) == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate()) ?
"#E4E3F2":"#FFFFFF";
else
{
da.style.backgroundColor =((mm==12?yy+1:yy)==outDate.getFullYear() && (mm==12?1:mm+1)== outDate.getMonth() + 1 &&
meizzWDay[i]==outDate.getDate())? "#E8F5E7" : // 选中日期颜色
(((mm==12?yy+1:yy) == new Date().getFullYear() && (mm==12?1:mm+1) == new Date().getMonth()+1 &&
meizzWDay[i] == new Date().getDate()) ? "#E4E3F2":"#FFFFFF"); // 当前系统时间
//将选中的日期显示为凹下去
if((mm==12?yy+1:yy)==outDate.getFullYear() && (mm==12?1:mm+1)== outDate.getMonth() + 1 &&
meizzWDay[i]==outDate.getDate())
{
da.borderColorLight="#E4E3F2";
da.borderColorDark="#E4E3F2"; // 选择日期边框颜色
}
}
}
else //本月的部分
{
da.innerHTML="<font style=' color: #3E5468;'>" + meizzWDay[i] + "</FONT>";
da.title=mm +"月" + meizzWDay[i] + "日";
da.οnclick=Function("meizzDayClick(this.innerText,0)"); //给td赋予onclick事件的处理
//如果是当前选择的日期,则显示亮蓝色的背景;如果是当前日期,则显示暗黄色背景
if(!outDate)
da.style.backgroundColor = (yy == new Date().getFullYear() && mm == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate())?
"#FFFFFF":"#FFFFFF";
else
{
da.style.backgroundColor =(yy==outDate.getFullYear() && mm== outDate.getMonth() + 1 && meizzWDay[i]==outDate.getDate())?
"#D5ECD2":((yy == new Date().getFullYear() && mm == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate())?
"#E4E3F2":"#F8F8FC"); // 前一个当前系统时间,后一个是本月时间低色
//将选中的日期显示为凹下去
if(yy==outDate.getFullYear() && mm== outDate.getMonth() + 1 && meizzWDay[i]==outDate.getDate())
{
//da.borderColorLight="#E4E3F2";
//da.borderColorDark="#E4E3F2"; // 选择日期边框颜色
}
}
}
da.style.cursor="hand"
da.οnmοuseοver=Function("this.backgroundColor='#000000';this.borderColorDark='#000099';this.borderColorLight='#000099';");
da.οnmοuseοut=Function("this.bgColor='#000000';this.borderColorDark='#9CBADE';this.borderColorLight='#9CBADE';");
}
else{da.innerHTML="";da.style.backgroundColor="";da.style.cursor="default";da.οnmοuseοver=Function("this.backgroundColor='#000000';this.borderColorDark='#000099';this.borderColorLight='#000099';");
da.οnmοuseοut=Function("this.bgColor='#000000';this.borderColorDark='#9CBADE';this.borderColorLight='#9CBADE';");}
}
}
function meizzDayClick(n,ex) //点击显示框选取日期,主输入函数*************
{
var yy=meizzTheYear;
var mm = parseInt(meizzTheMonth)+ex; //ex表示偏移量,用于选择上个月份和下个月份的日期
//判断月份,并进行对应的处理
if(mm<1){
yy--;
mm=12+mm;
}
else if(mm>12){
yy++;
mm=mm-12;
}
if (mm < 10){mm = "0" + mm;}
if (outObject)
{
if (!n) {//outObject.value="";
return;}
if ( n < 10){n = "0" + n;}
outObject.value= yy + "-" + mm + "-" + n ; //注:在这里你可以输出改成你想要的格式
closeLayer();
}
else {closeLayer(); alert("您所要输出的控件对象并不存在!");}
}
使用的SQLServer数据库
--创建通讯录数据库
if exists (select * from sysdatabases where name = 'addressList')
drop database addressList
create database addressList
on(
--主数据文件
name='addressListmdf',--数据库文件的逻辑名
filename='d:\SQL2008Workspace\addressList.mdf',--文件存放路径
size=3mb,--文件的初始大小
maxsize=5mb,--文件的最大大小
filegrowth=20%--文件增长率
)
log on(
--日志文件
name='addressListldf',--数据库日志的逻辑名
filename='d:\SQL2008Workspace\addressList.ldf',--文件存放路径,日志文件和主文件必须在同一个文件夹下面
size=2mb,--文件的初始大小
maxsize=5mb,--文件的最大大小
filegrowth=1mb--文件的增长速度
)
go
create table users(
uid int primary key identity(1,1),
nam nvarchar(20),
pwd varchar(20)
)
insert into users values('tom','123')
insert into users values('john','222')
select * from users
create table ctype(
tid int primary key identity(1,1),
tnam nvarchar(20)
)
insert into ctype values ('同事')
insert into ctype values ('家人')
insert into ctype values ('朋友')
insert into ctype values ('客户')
select * from ctype
create table contact(
cid int primary key identity(1,1),
nam nvarchar(20),
sex nvarchar(1),
tel varchar(20),
birth nvarchar(11),
tid int,--外键,组别
uid int--外键,用户ID
)
insert into contact values ('liby','男','12345621121','1990-2-21',1,1)
insert into contact values ('tiler','女','14321898891','1987-2-21',2,1)
insert into contact values ('sushi','男','14321890987','1987-5-19',3,2)
insert into contact values ('jack','男','14321896750','1980-11-18',4,2)
insert into contact values ('kll','男','12345621677','1990-12-21',2,1)
insert into contact values ('aaa','女','14321898891','1987-2-21',2,1)
insert into contact values ('bbb','女','14321890987','1987-5-19',3,2)
insert into contact values ('ccc','男','14321896750','1980-11-18',4,2)
insert into contact values ('sss','男','12345621121','1990-2-21',1,1)
insert into contact values ('ddd','女','14321898891','1987-2-21',2,1)
insert into contact values ('wwww','女','14321890987','1987-5-19',3,2)
insert into contact values ('juuu','男','14321896750','1980-11-18',4,2)
select * from contact
select count(1) from contact where uid=1
select top 3 c.cid,c.nam,c.sex,c.tel,c.birth,t.tnam from contact c,ctype t where c.tid=t.tid and c.uid=1 and c.cid not in (select top 0 c2.cid from contact c2,ctype t2 where c2.tid=t2.tid and c2.uid=1 )
use addressList
select top 3 c.cid,c.nam,c.sex,c.tel,c.birth,t.tnam from contact c,ctype t where c.tid=t.tid and c.uid=1 and c.cid not in (select top 0 c2.cid from contact c2,ctype t2 where c2.tid=t2.tid and c2.uid=1)
select top 3 c.cid,c.nam,c.sex,c.tel,c.birth,t.tnam from contact c,ctype t where c.tid=t.tid and c.uid=1 and c.sex='女'and c.cid not in (select top 3 c2.cid from contact c2,ctype t2 where c2.tid=t2.tid and c2.uid=1 and c2.sex='女')
运行结果