jsp连接SQL Server (二)

本文详细介绍了使用JSP连接数据库环境配置的方法,并提供了实现增删改查功能的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大笑jsp连接数据库环境配置及简单的增删改查(二)大笑

下面是各个功能页面的实现代码,由于未使用servlet,某些带*_deal.jsp字样的页面充当servlet的功能;

1.index.jsp ,首页,主要列出所有用户信息,以及增删改查的功能:

页面运行效果如下:


源代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.zmp.dao.*,com.zmp.entity.User,com.zmp.dao.impl.UserDaoImpl,java.util.ArrayList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>用户信息</title>
        <link rel="stylesheet" href="css/table.css" />
    </head>
    <body>
        <div id="head">
          用户信息
          <a href="index.jsp"><img src="images/home.png" alt="主页" id="headimg"/> </a>               
        </div>
        <%
            UserDaoImpl udi = new UserDaoImpl();
            ArrayList<User>  array = new ArrayList<User>();
            array = udi.listUser();
            String sno;
        %>
        <table id="customers" style="float: left;margin-left: 350px;">
            <tr>
                <th>学号</th>
                <th>姓名</th>
                <th>性别</th>
                <th>年龄</th>
                <th>系别</th>
                <th>操作</th>
            </tr>
            <%
                for(int i = 0; i < array.size(); i++) {
                	User user = null;
                	user = (User)array.get(i);
            %>
            <tr>
               <td><%=user.getSno() %></td>
               <td><%=user.getSname() %></td>
               <td><%=user.getSsex() %></td>
               <td><%=user.getSage() %></td>
               <td><%=user.getSdept() %></td>
               <td><a href="update.jsp?sno=<%=user.getSno()%>">
                       <img src="images/update.png" alt="修改" style="width:30px;height:33px;"/>
                   </a>
                   <a href="delete_deal.jsp?sno=<%=user.getSno()%>">
                       <img src="images/delete.png" alt="删除" style="width:30px;height:33px;"/>
                   </a>
               </td>
            </tr>
            <% } %>
       </table>
       <div id="right">
           <table id="customers" style="width: 100%;height: 150px;">
               <tr>
                   <th>功能:</th>
                   <td>
                       <a href="select.jsp">
                           <img src="images/select.png" alt="查找" style="width:50px;height:60px;"/>
                       </a>
                       <a href="add.jsp">
                           <img src="images/add.png" alt="添加" style="width:50px;height:60px;"/>
                       </a>
                   </td>
               </tr>
           </table>
       </div>
    </body>
</html>

2.delete_deal.jsp,处理点击index.jsp页面删除按钮的事件,如果删除成功,alert出删除成功的提示语句;如果删除失败,则alert出删除失败的提示语句;点击确定后都跳转到index.jsp页面:


源代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.zmp.dao.*,com.zmp.entity.User,com.zmp.dao.impl.UserDaoImpl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>删除信息</title>
    </head>
    <body>
        <%
            request.setCharacterEncoding("UTF-8");
            if(request.getParameter("sno") != null) {
            	UserDaoImpl udi = new UserDaoImpl();
                if(udi.deleteUser(request.getParameter("sno")) == 1) {
                	out.println("<script language='javascript'>alert('删除成功!');window.location.href='index.jsp';</script>" );
                } else {
                	out.println("<script language='javascript'>alert('删除失败!');window.location.href='index.jsp';</script>" );
                }
            } else {
            	out.println("<script language='javascript'>alert('删除失败!');window.location.href='index.jsp';</script>" );
            }
        %>
    </body>
</html>

3.select.jsp,查询信息页面,按学号查询,由于本页面比较简单,所以处理事件的代码也在此页面,运行效果如下:


源代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.zmp.dao.*,com.zmp.entity.User,com.zmp.dao.impl.UserDaoImpl" %>   
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>查询</title>
        <link rel="stylesheet" href="css/table.css" />
    </head>
    <body>
        <%
            User user = null;
            request.setCharacterEncoding("utf-8");
            if(request.getParameter("Sno") != null) {
                String sno = request.getParameter("Sno");
                UserDaoImpl udi = new UserDaoImpl();                
                user=udi.findUser(sno);
            }
        %>
        <div id="head">
          查找用户
          <a href="index.jsp"><img src="images/home.png" alt="主页" id="headimg"/> </a>               
        </div>
        <form action="select.jsp">
            <table id="customers" style="width:20%;">
                <tr>
                    <td>输入学号:</td>
                    <td><input type="text" name="Sno" /></td>
                </tr>
                <%
                    if(user != null) {%>
                    <tr>
                    <th>学号</th>
                    <td><%=user.getSno() %></td>
                </tr>
                <tr>
                    <th>姓名</th>
                    <td><%=user.getSname() %></td>
                </tr>
                <tr>
                    <th>性别</th>
                    <td><%=user.getSsex() %></td>
                </tr>
                <tr>
                    <th>年龄</th>
                    <td><%=user.getSage() %></td>
                </tr>
                <tr>
                    <th>系别</th>
                    <td><%=user.getSdept() %></td>
                </tr>
                        
                   <% } else {%>
                <tr>
                    <td colspan="2" style="color:red;font-size:14px;">*没有该生信息!</td>
                </tr>
                <%} %>
                <tr>
                    <td colspan="2"><input type="submit" value="查找" /></td>
                </tr>
            </table>
        </form>
    </body>
</html>

4.update.jsp修改页面,点击index.jsp页面的修改按钮进入update.jsp,运行效果如下:


源代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.zmp.dao.*,com.zmp.entity.User,com.zmp.dao.impl.UserDaoImpl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>修改信息</title>
        <link rel="stylesheet" href="css/table.css" />
    </head>
    <body>
        <div id="head">
         修改信息
          <a href="index.jsp"><img src="images/home.png" alt="主页" id="headimg"/> </a>               
        </div>
        <%
            request.setCharacterEncoding("UTF-8");
            User user = null;
            UserDaoImpl udi = new UserDaoImpl();
            if(request.getParameter("sno") != null) {
            	user = udi.findUser(request.getParameter("sno"));%>
                <form action="update_deal.jsp" method="post" id="update">
                	<table id="customers" style="width:20%;">
                    <tr>
                        <th>学号</th>
                        <td><%=user.getSno() %><input type="hidden" name="Sno" value="<%=user.getSno() %>"/></td>
                    </tr>
                    <tr>
                        <th>姓名</th>
                        <td><input type="text" name="Sname" value="<%=user.getSname() %>" /></td>
                    </tr>
                    <tr>
                        <th>性别</th>
                        <td><input type="text" name="Ssex"  value="<%=user.getSsex() %>"/></td>
                    </tr>
                    <tr>
                        <th>年龄</th>
                        <td><input type="text" name="Sage" value="<%=user.getSage() %>"/></td>
                    </tr>
                    <tr>
                        <th>系别</th>
                        <td><input type="text" name="Sdept" value="<%=user.getSdept() %>"/></td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <input type="submit" value="修改" />
                        </td>
                    </tr>
                    </table>
                </form>
         <% } %> 
    </body>
</html>

5.update_deal.jsp处理update.jsp页面,源代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.zmp.dao.*,com.zmp.entity.User,com.zmp.dao.impl.UserDaoImpl" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>修改信息处理</title>
    </head>
    <body>
        <%
           request.setCharacterEncoding("UTF-8");
           if(request.getParameter("Sno") != null) {
               User user = new User();
               UserDaoImpl udi = new UserDaoImpl();
               user.setSno(request.getParameter("Sno"));
               user.setSname(request.getParameter("Sname"));
               user.setSsex(request.getParameter("Ssex"));
               user.setSage(request.getParameter("Sage"));
               user.setSdept(request.getParameter("Sdept"));
               if(udi.updateUser(user) == 1) {          
                   out.println("<script language='javascript'>alert('修改成功!');window.location.href='index.jsp';</script>" );
                } else {
                	out.println("<script language='javascript'>alert('修改失败!');window.location.href='index.jsp';</script>" );
                }   
             } else {
            	out.println("<script language='javascript'>alert('修改失败!');window.location.href='index.jsp';</script>" );
            } 
        %> 
    </body>
</html>

6.add.jsp,添加信息页面,有index.jsp中添加按钮点击进入,运行效果如下: 


源代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="css/table.css" />
        <title>添加</title>
    </head>
    <body>
        <div id="head">
          添加用户
          <a href="index.jsp"><img src="images/home.png" alt="主页" id="headimg"/> </a>               
        </div>
        <form action="add_deal.jsp" method="post">
            <table id="customers" style="width:20%;">
                <tr>
                	<th>学号</th>
                	<td><input type="text" name="Sno"/></td>
                </tr>
                <tr>
                	<th>姓名</th>
                	<td><input type="text" name="Sname" /></td>
                </tr>
                <tr>
                	<th>性别</th>
                	<td><input type="text" name="Ssex" /></td>
                </tr>
                <tr>
                	<th>年龄</th>
                	<td><input type="text" name="Sage" /></td>
                </tr>
                <tr>
                	<th>系别</th>
                	<td><input type="text" name="Sdept" /></td>
                </tr>
                <tr>
                	<td colspan="2"><input type="submit" value="添加" /></td>
                </tr>
            </table>
        </form>
    </body>
</html>

7.add_deal.jsp,处理add.jsp页面: 
源代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.zmp.dao.*,com.zmp.entity.User,com.zmp.dao.impl.UserDaoImpl" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>添加处理</title>
    </head>
    <body>
         <%
             request.setCharacterEncoding("utf-8");
             if(request.getParameter("Sno") != null) {                
                 UserDaoImpl udi = new UserDaoImpl();
                 User user = new User(); //此处必须实例化,即User user = new User();若User user = null;则为空
                 user.setSno(request.getParameter("Sno"));
                 user.setSname(request.getParameter("Sname"));
                 user.setSsex(request.getParameter("Ssex"));
                 user.setSage(request.getParameter("Sage"));
                 user.setSdept(request.getParameter("Sdept"));
                 if(udi.addUser(user) == 1) {
                     out.println("<script language='javascript'>alert('添加成功!');window.location.href='index.jsp';</script>" );
                 } else {
                     out.println("<script language='javascript'>alert('添加失败!');window.location.href='index.jsp';</script>" );
                 }
             } else {
                 out.println("<script language='javascript'>alert('添加失败!');window.location.href='index.jsp';</script>" );
             }
      %>  
    </body>
</html>


8.最后,用到的table.css,代码如下:

#customers {
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    width: 50%;
    border-collapse: collapse;
    margin: 0 auto;
    text-align: center;
}

#customers td, #customers th {
	font-size: 1em;
    border: 1px solid #98bf21;
    padding: 3px 7px 2px 7px;
}

#customers th {
    font-size: 1.1em;
    text-align: center;
	padding-top: 5px;
	padding-bottom: 4px;
	background-color: #A7C942;
	color: #ffffff;
}

#customers tr.alt td {
    color: #000000;
    background-color: #EAF2D3;
}

#head {
	background-color: #98ba00;
	margin-top: 15px;
	margin-bottom:10px;
	width: 100%;
	height:80px;
	text-align: center;
	font-size: 30px;
	color: Black;
}

#head p{
	margin-top:10px;
	text-algin:center;
}

#right {
	height:150px;
	width: 250px;
	float: right;
	margin-right: 50px;
}

input[type="text"] {
	outline:#FF8C00 solid 1px;
}

input[type="submit"] {
	border: none;
	background-color: #98ba00;
	width: 60px;
	height: 35px;
}

#headimg {
	width: 30px;
	height: 40px;
	float:right;
	margin-right:60px;
	margin-top:30px;"
}

用到的按钮图片如下:


大哭大哭大哭哦了,本次教程完了,睡觉了,下午还有课!!!!!!!!大哭大哭大哭


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值