高分求助,JSP,MyEclipse,SQL SERVER,数据传输的问题

阿勒。 2015-12-31 11:26:46
高分求助,求高人给我讲解,一个网页上往数据库中添加数据的流程(数据是怎么从我们网页上添加到数据库的),点击提交按钮按钮以后具体是怎么运作的,要细节详细,
这个添加界面的源码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
<meta http-equiv="description" content="This is my page" />

<link rel="stylesheet" type="text/css" href="/xuanke/css/base.css" />

<script language="javascript">
function check()
{
if(document.formAdd.xuehao.value=="")
{
alert("请输入学号");
return false;
}
document.formAdd.submit(); 这是什么意思,
}
</script>
</head>

<body leftmargin="2" topmargin="9" background='/xuanke/img/allbg.gif'>
<form action="/xuanke/xuesheng?type=xueshengAdd" name="formAdd" method="post">
<table width="98%" border="0" cellpadding="2" cellspacing="1" bgcolor="#D1DDAA" align="center" style="margin-top:8px">
<tr bgcolor="#E7E7E7">
<td height="14" colspan="3" background="/xuanke/img/tbg.gif"> 学生管理 </td>
</tr>
<tr align='center' bgcolor="#FFFFFF" onMouseMove="javascript:this.bgColor='red';" onMouseOut="javascript:this.bgColor='#FFFFFF';" height="22">
<td width="25%" bgcolor="#FFFFFF" align="right">
学号:
</td>
<td width="75%" bgcolor="#FFFFFF" align="left">
<input type="text" name="xuehao" size="60"/>
</td>
</tr>
<tr align='center' bgcolor="#FFFFFF" onMouseMove="javascript:this.bgColor='red';" onMouseOut="javascript:this.bgColor='#FFFFFF';" height="22">
<td width="25%" bgcolor="#FFFFFF" align="right">
姓名:
</td>
<td width="75%" bgcolor="#FFFFFF" align="left">
<input type="text" name="xingming" size="60"/>
</td>
</tr>
<tr align='center' bgcolor="#FFFFFF" onMouseMove="javascript:this.bgColor='red';" onMouseOut="javascript:this.bgColor='#FFFFFF';" height="22">
<td width="25%" bgcolor="#FFFFFF" align="right">
性别:
</td>
<td width="75%" bgcolor="#FFFFFF" align="left">
<input type="radio" name="xingbie" value="男" checked="checked"/>男
    
<input type="radio" name="xingbie" value="女"/>女
</td>
</tr>
<tr align='center' bgcolor="#FFFFFF" onMouseMove="javascript:this.bgColor='red';" onMouseOut="javascript:this.bgColor='#FFFFFF';" height="22">
<td width="25%" bgcolor="#FFFFFF" align="right">
年龄:
</td>
<td width="75%" bgcolor="#FFFFFF" align="left">
<input type="text" name="nianling" size="60"/>
</td>
</tr>
<tr align='center' bgcolor="#FFFFFF" onMouseMove="javascript:this.bgColor='red';" onMouseOut="javascript:this.bgColor='#FFFFFF';" height="22">
<td width="25%" bgcolor="#FFFFFF" align="right">
班级:
</td>
<td width="75%" bgcolor="#FFFFFF" align="left">
<input type="text" name="banji" size="60"/>
</td>
</tr>
<tr align='center' bgcolor="#FFFFFF" onMouseMove="javascript:this.bgColor='red';" onMouseOut="javascript:this.bgColor='#FFFFFF';" height="22">
<td width="25%" bgcolor="#FFFFFF" align="right">
登陆账号:
</td>
<td width="75%" bgcolor="#FFFFFF" align="left">
<input type="text" name="loginname" size="60"/>
</td>
</tr>
<tr align='center' bgcolor="#FFFFFF" onMouseMove="javascript:this.bgColor='red';" onMouseOut="javascript:this.bgColor='#FFFFFF';" height="22">
<td width="25%" bgcolor="#FFFFFF" align="right">
登陆密码:
</td>
<td width="75%" bgcolor="#FFFFFF" align="left">
<input type="text" name="loginpw" size="60"/>
</td>
</tr>
<tr align='center' bgcolor="#FFFFFF" onMouseMove="javascript:this.bgColor='red';" onMouseOut="javascript:this.bgColor='#FFFFFF';" height="22">
<td width="25%" bgcolor="#FFFFFF" align="right">
 
</td>
<td width="75%" bgcolor="#FFFFFF" align="left">
<input type="button" value="提交" onclick="check()"/> 
<input type="reset" value="重置"/> 
</td>
</tr>
</table>
</form>
</body>
</html>

文件:xuesheng_servlet.java 这个应该是一个有关的文件,也请说明一下,数据是怎么添加到数据库的,一定得详细,万分感谢

package servlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.DB;

import model.TAdmin;
import model.Tkecheng;
import model.Tlaoshi;
import model.Txuesheng;

public class xuesheng_servlet extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException
{
String type=req.getParameter("type");

if(type.endsWith("xueshengAdd"))
{
xueshengAdd(req, res);
}
if(type.endsWith("xueshengMana"))
{
xueshengMana(req, res);
}
if(type.endsWith("xueshengDel"))
{
xueshengDel(req, res);
}
}

public void xueshengAdd(HttpServletRequest req,HttpServletResponse res)
{
String xuehao=req.getParameter("xuehao");
String xingming=req.getParameter("xingming");
String xingbie=req.getParameter("xingbie");
String nianling=req.getParameter("nianling");
String banji=req.getParameter("banji");
String loginname=req.getParameter("loginname");
String loginpw=req.getParameter("loginpw");
String del="no";

String sql="insert into t_xuesheng values(?,?,?,?,?,?,?,?)";
Object[] params={xuehao,xingming,xingbie,nianling,banji,loginname,loginpw,del};
DB mydb=new DB();
mydb.doPstm(sql, params);
mydb.closed();
这里仔细说明一下,DB mydb ......这些都是什么

req.setAttribute("message", "操作成功");
req.setAttribute("path", "xuesheng?type=xueshengMana");

String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}

public void xueshengMana(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
List xueshengList=new ArrayList();
String sql="select * from t_xuesheng where del='no'";
Object[] params={};
DB mydb=new DB();
try
{
mydb.doPstm(sql, params);
ResultSet rs=mydb.getRs();
while(rs.next())
{
Txuesheng xuesheng=new Txuesheng();

xuesheng.setId(rs.getInt("id"));
xuesheng.setXuehao(rs.getString("xuehao"));
xuesheng.setXingming(rs.getString("xingming"));
xuesheng.setXingbie(rs.getString("xingbie"));

xuesheng.setNianling(rs.getString("nianling"));
xuesheng.setBanji(rs.getString("banji"));
xuesheng.setLoginname(rs.getString("loginname"));
xuesheng.setLoginpw(rs.getString("loginpw"));
xuesheng.setDel(rs.getString("del"));

xueshengList.add(xuesheng);
}
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
mydb.closed();

req.setAttribute("xueshengList", xueshengList);
req.getRequestDispatcher("admin/xuesheng/xueshengMana.jsp").forward(req, res);
}



public void xueshengDel(HttpServletRequest req,HttpServletResponse res)
{

String sql="update t_xuesheng set del='yes' where id="+Integer.parseInt(req.getParameter("id"));
Object[] params={};
DB mydb=new DB();
mydb.doPstm(sql, params);
mydb.closed();

req.setAttribute("message", "操作成功");
req.setAttribute("path", "xuesheng?type=xueshengMana");

String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}

public void dispatch(String targetURI,HttpServletRequest request,HttpServletResponse response)
{
RequestDispatcher dispatch = getServletContext().getRequestDispatcher(targetURI);
try
{
dispatch.forward(request, response);
return;
}
catch (ServletException e)
{
e.printStackTrace();
}
catch (IOException e)
{

e.printStackTrace();
}
}
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}

public void destroy()
{

}
}
...全文
213 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Gxy839134958 2015-12-31
  • 打赏
  • 举报
回复
先连上一个数据库,然后通过insert语语句将数据导入数据库!

50,549

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧