免费论文网 首页

html增删改查代码

时间:2017-02-14 06:08:41 来源:免费论文网

篇一:增删改查代码

增:

调用的是hibernate的save方法。

Action中传值方式有两种,一种是以对象传值,一种是以属性传值,所以在Action中有两种方式来接收。

private HibernateTemplate hibernateTemplate;

一:以属性传值:

jsp页面:

用户名:<td><input type="text" id="name" ></td>

密码:<td><input type="password" id="psw"></td>

年龄:<td><input type="text" id="age"></td>

function addData()

{

var name= $("#name").val();

var psw = $("#psw").val();

var age= $("#age").val();

var param={"name":name,"psw":psw,"age":age};

$.post("adddata.action",param,function(data){

if(data="success")

{

alert("添加数据成功");

window.close();

window.opener.location.reload();

}

else

{

alert("添加数据失败");

}

});

}

需要注意的是:以属性传值的话Struts不会自动生成对象,需要自己手动来完成。如果是以对象来传值的话,会自动生成对象。

Framework frame = new Framework();

frame.setAge(age);

frame.setName(name);

frame.setPsw(psw);

frame.setCreatetime(new Date());

frameworkService.saveData(frame);

response.setCharacterEncoding("UTF-8");

response.getWriter().write("success");

response.getWriter().flush();

二:以对象传值

<td><input type="text" name="frame.name" ></td>

<td><input type="password"name="frame.psw" ></td>

<td><input type="text" name="frame.age"></td>

function addData()

{

$.post("adddata.action",$("form").serialize(),function(data){

if(data="success")

{

alert("添加数据成功");

window.close();

window.opener.location.reload();

}

else

{

alert("添加数据失败");

}

});

}

相同的道理:如果是对象传值的话,Action中也要用对象来接收。

private Framework frame;

frame.setCreatetime(new Date());

frameworkService.saveData(frame);

查:循环在页面上

利用hibernate的find方法传入一个hql参数返回一个list来操作。

DaoImp中代码

public List<Framework> getList(String hql)

{

return getHibernateTemplate().find(hql);

}

Service中代码:

public List<Framework> getList()

{

String hql = "from Framework";

return frameworkdao.getList(hql);

}

Action中代码:

private List<Framework> frames;

public String getFrameList(){

frames=frameworkService.getList();

return "success";

Jsp页面需要引入标签:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<c:forEach items="${frames}" var="f">

<td align="center">${f.id}</td>

<td align="center">${f.name}</td>

</c:forEach>

改:

需要先跳转到一个页面上,把id传到此页面上,获取到当前记录的信息,然后再用hibernate中update方法更新。

onclick="modify('${f.id}')"

function modify(id)

{

var url ="modify.action?frame.id="+id;

openWindow(url,600,400);

}

通过传入到Action中的id调用hibernate中的get方法获取到一个对象。

DaoImp中代码:

public Object getObject(Object object, int id)

{

return this.getHibernateTemplate().get(object.getClass(), id);

}

或者

public Framework getObject(int id)

{

return (Framework)this.getHibernateTemplate().get(Framework.class,id);

}

Action中代码:

public String modifyData()

{

frame = frameworkService.getObject(frame.getId());

return "success";

}

然后再把此对象的属性显示在修改的页面上。

jsp页面代码:

<td><input type="text" name="frame.name" value="${frame.name}"></td>

此时需要把id和时间放在隐藏域中。

<input type="hidden" name="frame.id" value="${frame.id}">

<input type="hidden" name="frame.createtime" value="${frame.createtime}">

然后调用hibernate的update方法即可保存。

删:

首先页面循环全选代码:

用js来做:

function checkAll()

{

var boxall = document.getElementsByName("boxall")[0];

var box = document.getElementsByName("box");

if(boxall.checked)

{

for(var i=0;i<box.length;i++)

{

box[i].checked = true;

}

}

else

{

for(var i=0;i<box.length;i++)

{

box[i].checked = false;

}

}

}

用jquery来写:

删除需要传入id来删除,此时需要用到字符串的拼接。把参数param传到Action中。 jsp页面方法代码如下:

function deleteId()

{

var count =0;

var param ="";

var arry = document.getElementsByName("box"); for(var i=0;i<arry.length;i++) { if(box[i].checked) {if(param=''){ param+="box="+arry[i].value;}else{ param+="&box="+arry[i].value;}count++; }} if(count==0) {} alert("请选择删除的数据"); return; if(confirm("确定删除吗?")) { $.post("delete.action",param,function(data) { if(data="success"){alert("删除数据成功"); window.location.reload(); } else {

alert("删除数据失败"); }

});

}

}

在Action中需要接受传过去的数组参数。 private String[] box;

篇二:用javascript实现信息的增删改查

用javascript实现信息的增删改查

Html页面:Demo2.html

<body>

<div align="center">

<h1>显示所有的用户界面</h1>

<div style="border: 1px red solid; margin-bottom: 100px; padding: 10px 10%;"><table border="1px" cellpadding="0" cellspacing="0" id="tusers"><thead>

<tr><th><input type="checkbox" name="chbk" id="chbk1" onclick="selectAll()"/></th>

<th>名称</th>

<th>性别</th>

<th>邮箱</th>

<th>出生日期</th>

<th>操作</th>

</tr>

</thead>

<tbody id="users">

</tbody>

</table>

<div id="pages"></div>

</div>

<div style="border: 1px blue solid;">

<form action="">

<table id="divs">

<tbody id="addUsers">

<tr>

<td>用户名:</td>

<td><input type="text" name="name" id="name"/></td> </tr>

<tr>

<td>性别:</td>

<td><select id="sex">

<option value="男">男</option>

<option value="女">女</option>

</select>

</td>

</tr>

<tr>

<td>邮箱</td>

<td><input type="text" name="email" id="email"/></td> </tr>

<tr>

<td>出生日期:</td>

<td>

<input type="text" id="bir" name="bir"/>

<input type=button value="添加日期" onclick="showCalender(this,document.all.bir)"/>

</td>

</tr>

<tr id="addu">

<td colspan="2"><input type="button" value="添加" onclick="addUser()" id="add"/></td>

</tr>

<tr id="addu1">

<td colspan="2"><input type="button" value="修改" id="upduser" /></td>

</tr>

</tbody>

</table>

</form>

</div>

</div>

</body>

<script>

window.onload = function(){

alert("onload");

document.getElementById("addu1").style.display = "none";

}

function selectAll(){

var users = document.getElementById("users");

var ips = users.getElementsByTagName("input");

var chbk = document.getElementById("chbk1");

for(var i=0;i<ips.length;i++){

ips[i].setAttribute("checked",chbk.getAttribute("checked"));}

}

function addUser(){

alert("add");

var name = document.getElementById("name").Value;

var sex = document.getElementById("sex").Value;

var email = document.getElementById("email").Value;

var bir = document.getElementById("bir").Value;

var tusers = document.getElementById("tusers").Value;

var tr1 = document.createElement("tr");

var tname = document.createElement("td"); var tsex = document.createElement("td"); var temail = document.createElement("td"); var tbir = document.createElement("td"); var toper = document.createElement("td"); var cbk1 = document.createElement("input"); cbk1.setAttribute("type","checkbox"); cbk1.setAttribute("name","chbk"); cbk.appendChild(cbk1); tname.appendChild(document.createTextNode(name)); tsex.appendChild(document.createTextNode(sex)); temail.appendChild(document.createTextNode(email)); tbir.appendChild(document.createTextNode(bir)); var adelete = document.createElement("a"); var aupdate = document.createElement("a"); adelete.setAttribute("href","#"); aupdate.setAttribute("href","#"); adelete.appendChild(document.createTextNode("删除|")); aupdate.appendChild(document.createTextNode("修改")); toper.appendChild(adelete); toper.appendChild(aupdate); tr1.appendChild(cbk); tr1.appendChild(tname); tr1.appendChild(tsex); tr1.appendChild(temail); tr1.appendChild(tbir); tr1.appendChild(toper); var users = document.getElementById("users"); users.appendChild(tr1); tusers.appendChild(users); adelete.onclick = function(){ users.removeChild(adelete.parentNode.parentNode); } aupdate.onclick function(){ document.getElementById(addu).style.display = "none"; document.getElementById(addu1).style.display = "block";

var utrs = utr.childNodes;document.getElementById("name").value = utrs[1].innerHTML;document.getElementById("sex").value = utrs[2].innerHTML;document.getElementById("email").value = utrs[3].innerHTML;document.getElementById("bir").value = utrs[4].innerHTML;var upUser = document.getElementById("upduser");upUser.onclick = function(){ utr.childNodes[1].innerHTML =document.getElementById("name").value; utr.childNodes[2].innerHTML =document.getElementById("sex").value; utr.childNodes[3].innerHTML =document.getElementById("email").value; utr.childNodes[4].innerHTML =document.getElementById("bir").value; document.getElementById("addu1").style.display = "none"; document.getElementById("addu").style.display = "block";} } testPage() } var indexPage = document.createElement("a"); var upPage = document.createElement("a"); var downPage = document.createElement("a"); var endPage = document.createElement("a"); var nowpage = 1; function testPage(){ var tbodyUsers = document.getElementById("users"); var trUsers = document.getElementById("tr"); var countRecord = trUsers.length; var PAGESIZE = 2; var countPage = (countRecord%PAGESIZE ==0?countRecord/PAGESIZE:Math.ceil(countRecord/PAGESIZE)); var pages=document.getElementById("pages"); if(!pages.hasChildNodes()){getPages(nowpage); } index.onclik=function(){noepage=1;

} upPage.onclick=function(){if(nowpage-1>1){ nowpage-=1;}else{ nowpage=1; indexPageInfo(countRecord,trUsers);}var startindex =(nowpage-1)*PAGESIZE;var endindex=startindex+PAGESIZE;PageInfo(startindex,endindex,countRecord,trUsers); } downPage.onclick=function(){ if(nowpage+1>=countPage){ nowpage=countPage;}else{ nowpage=+1;}var startindex =(nowpage-1)*PAGESIZE;var endindex=startindex+PAGESIZE;PageInfo(startindex,endindex,countRecord,trUsers); } endPage.onclick=function(){if(nowpage>1){ var startindex =(nowpage-1)*PAGESIZE; for(var i=0;i<countRecord;i++){ if(i<startindex){trUsers[i].style.display="none"; }else{trUsers[i].style.display="block"; } }}else{ indexPageInfo(countRecord,trUsers);} } } function indexPageInfo(countRecord, trUsers) { if (countRecord <= 2) {for ( var i = 0; i < PAGESIZE; i++) { trUsers[i].style.display = "block";} } else {

篇三:表单的增删改查示例

代码<script language="javascript">

functionmycheck(){

if(myform.cont1.value==""){ //jqueryjs基本语法

alert("内容1不能为空!!");

myform.cont1.focus();

return false;//1.返回false。2.这个false将停止代码的运行

}

if(myform.cont2.value==""){

alert("内容2不能为空!!"); myform.cont2.focus(); return false; } if(myform.cont3.value==""){

alert("内容3不能为空!!");

myform.cont3.focus();

return false;

}

}

</script>

<?php

$link = mysqli_connect("localhost","root","")or die( mysql_error());

$db_selected = mysqli_select_db($link,"lx_study");

mysqli_query($link, "set names gb2312");//设置中文字符

$cont1 = isset($_POST['cont1'])?$_POST['cont1']:"";

$cont2 = isset($_POST['cont2'])?$_POST['cont2']:"";//NULL

$cont3 =isset($_POST['cont3'])?$_POST['cont3']:"";

$cont4 = isset($_POST['cont4'])?$_POST['cont4']:"";

$cont5 =isset($_POST['cont5'])?$_POST['cont5']:"";

$submit =isset($_POST['submit'])?$_POST['submit']:"";

if($cont1 != '' && $cont2 != '' && $cont3!='' && $cont4!='' && $cont5!='')

{

if($submit == '新增')//提交进行插入工作

{

$re=mysqli_query ($link,"INSERT INTO biao (Cont1, Cont2, Cont3,Cont4,Cont5)VALUES('$_POST[cont1]','$_POST[cont2]','$_POST[cont3]','$_POST[cont4]','$_POST[cont5]')");

//echo "INSERT INTO biao (Cont1, Cont2, Cont3,Cont4,Cont5) VALUES('$_POST[cont1]','$_POST[cont2]','$_POST[cont3]','$_POST[cont4]','$_POST[cont5]')";

if($re)

{

echo "<script>";

echo "alert('提交成功')";

echo "</script>";

}

else

{

echo "<script>";

echo "alert('提交失败')";

echo "</script>";

}

}

if($submit == '修改')

{

$cont1 = isset($_POST['cont1'])?$_POST['cont1']:"";

$cont2 = isset($_POST['cont2'])?$_POST['cont2']:"";

$cont3 =isset($_POST['cont3'])?$_POST['cont3']:"";

$cont4 = isset($_POST['cont4'])?$_POST['cont4']:"";

$cont5 =isset($_POST['cont5'])?$_POST['cont5']:"";

$submit =isset($_POST['submit'])?$_POST['submit']:"";

$id = isset($_POST['id'])?$_POST['id']:"";

$sql=mysqli_query($link,"UPDATEbiao

id='$id',cont1='$cont1',cont2='$cont2',cont3='$cont3',cont4='$cont4',cont5='$cont5'

id=$id");

//echo "UPDATE biao

id='$id',cont1='$cont1',cont2='$cont2',cont3='$cont3',cont4='$cont4',cont5='$cont5'

id=$id";

if($sql)

{

echo "<script>";

echo "alert('修改成功')";

echo "</script>";

}

else

{

echo "<script>";

echo "alert('修改失败')";

echo "</script>";

}

}

} SET where SET where

$action1 = isset($_GET["action1"])?$_GET['action1']:"";

$id = isset($_GET["id"])?$_GET['id']:""; //修改

$action2 = isset($_GET["action2"])?$_GET['action2']:"";

if($id != '')//这个地方就是说明接受到参数了

{

if($action2=='update_setup1')//修改

{

数据

//exit('修改了');$sql = mysqli_query($link,"select * from biao where id = ".$id);//id对应的数据库 // echo "select * from biao where id = ".$id;

$row=mysqli_fetch_array($sql);//获取结果集

}

}

if($action1=="del")

{

$re=mysqli_query($link,"delete from biao where id='$id.'");

$mark = mysqli_affected_rows($link);

}

?>

<form action="lianxi1.php" method="post" name="myform" enctype="multipart/form-date"> <table width="405" border="1" cellpadding="5" cellspacing="0" >

<tr align="center"><td width="103" height="25" align="right" >

id</td>

<td width="302" height="25"><input type="text" name="id" id="id" size="20" maxlength="100" value="<?=isset($row[0])?$row[0]:""?>" ></td>

</tr>

<tr align="center"><td width="103" height="25" align="right" >

内容1</td>

<td width="302" height="25"><input type="text" name="cont1" id="cont1" size="20" maxlength="100" value="<?=isset($row[1])?$row[1]:""?>" ></td>

</tr>

<tr align="center"><td width="103" height="25" align="right">

内容2</td>

<td width="302" height="25"><input type="text" name="cont2" id="cont2" size="20" maxlength="100" value="<?=isset($row[2])?$row[2]:""?>" ></td>

</tr>

<tr align="center"><td width="103" height="25" align="right">

内容3</td>

<td width="302" height="25"><input type="text" name="cont3" id="cont3" size="20" maxlength="100" value="<?=isset($row[3])?$row[3]:""?>" ></td>

</tr>

<tr align="center"><td width="103" height="25" align="right">

内容4</td>

<td width="302" height="25"><input type="text" name="cont4" id="cont4" size="20" maxlength="100" value="<?=isset($row[4])?$row[4]:""?>" ></td>

</tr>

<tr align="center"><td width="103" height="25" align="right">

内容5</td>

<td width="302" height="25"><input type="text" name="cont5" id="cont5" size="20" maxlength="100" value="<?=isset($row[5])?$row[5]:""?>"></td>

</tr>

<tr align="center" >

<td height="25" colspan="3"><input type="submit" name="submit" id="submit" onClick="return mycheck();" value="新增">

</td>

</tr>

</table>

</form>

<table>

<tr>

<td width="60" height="30" >编号</td>

<td width="60" height="30" >内容1</td>

<td width="60" height="30" >内容2</td>

<td width="60" height="30" >内容3</td>

<td width="60" height="30" >内容4</td>

<td width="60" height="30" >内容5</td>

<td width="60" height="30" >操作1</td>

<td width="60" height="30" >操作2</td>

</tr>

<?php

$re =mysqli_query($link,"select * from biao");//查询数据

//$row=mysqli_fetch_row($sql);

while($row = mysqli_fetch_assoc($re))//循环显示数据

{

// var_dump($row);

?>

<tr>

<td width="60" height="30" ><?php echo $row["id"];?></td>

<td width="60" height="30" ><?php echo $row["cont1"];?></td>

<td width="60" height="30" ><?php echo $row["cont2"];?></td> <td width="60" height="30" ><?php echo $row["cont3"];?></td> <td width="60" height="30" ><?php echo $row["cont4"];?></td> <td width="60" height="30" ><?php echo $row["cont5"];?></td> <td width="60" height="30" ><a href="lianxi1.php?id=<?php

href="lianxi1.php?id=<?php echo echo $row["id"];?>&action1=del">删除</a></td><td width="60" height="30" ><a

$row['id'];?>&action2=update_setup1">修改</a></td>

</tr>

<?php

}

?>

</table>

<script language="javascript">

action = "<?=$action2 ?>";

if(action == 'update_setup1'){

document.getElementById("submit").value="修改";

//$("submit").val("修改");

}

</script>

效果图:


html增删改查代码
由:免费论文网互联网用户整理提供,链接地址:
http://m.csmayi.cn/show/168509.html
转载请保留,谢谢!
相关阅读
最近更新
推荐专题