读书人

如何实现 在表中选中一条记录,然后在点

发布时间: 2012-01-12 22:11:58 作者: rapoo

怎么实现 在表中选中一条记录,然后在点删除按钮 ,就可以把选种的记录删除...
如题...
最好给出代码...


[解决办法]
这个好办,点删除的时候把ID传回后台,在后台实现要删除ID的那一例.
给你个例子.
<%@ page contentType= "text/html;charset=gb2312 "%>
<%@ page import= "java.sql.* "%>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 " />
<title> 数据操作 </title>
<style type= "text/css ">
.activelabel { border-left:2px solid #ffffff;border-top:2px solid #ffffff;border-right:2px solid #888888;border-bottom:2px none #888888;cursor:hand;color:#4a7bfb}
.mainDataCell {word-break:keep-all;border-top:1px none #000000;border-left:1px none #000000;border-bottom:1px solid #000000;border-right:1px solid #000000;}
.dataRowBlur {background-color:#FFFFCC;color:#000000;}
.dataRowFocus {background-color:#08246b;color:#ffffff}
</style>
<script language= "javascript " type= "text/javascript ">
var selectedID=null;
function focusDataRow(id)// 定位数据行
{
if(selectedID!=null&&id!=( "DATA_ "+selectedID))
{
blurDataRow( "DATA_ "+selectedID);
}
if(document.all.item(id).className== "dataRowBlur ")
{
document.all.item(id).className= "dataRowFocus ";
selectedID=String(id).replace(/^DATA_/, " ");
}
else if(document.all.item(id).className== "dataRowFocus ")
{
blurDataRow(id);
}

}
function blurDataRow(id)
{

document.all.item(id).className= "dataRowBlur ";
selectedID=null;
}


function dis1()
{
document.all.item( "tab1 ").style.display= "block ";
document.all.item( "tab2 ").style.display= "none ";
document.all.item( "tab3 ").style.display= "none ";
document.all.item( "t1 ").className= "activelabel ";
document.all.item( "t2 ").className= " ";
document.all.item( "t3 ").className= " ";

}
function dis2()
{
document.all.item( "tab2 ").style.display= "block ";
document.all.item( "tab1 ").style.display= "none ";
document.all.item( "tab3 ").style.display= "none ";
document.all.item( "t2 ").className= "activelabel ";
document.all.item( "t1 ").className= " ";
document.all.item( "t3 ").className= " ";

}
function dis3()
{
document.all.item( "tab3 ").style.display= "block ";
document.all.item( "tab1 ").style.display= "none ";
document.all.item( "tab2 ").style.display= "none ";
document.all.item( "t3 ").className= "activelabel ";
document.all.item( "t1 ").className= " ";
document.all.item( "t2 ").className= " ";

}

function cl()//窗口关闭
{
window.close();
}


function up()//修改选中字段的相应值
{
if (selectedID!=null)
{
var job_desc=document.all.item( "job_desc_ "+selectedID).innerHTML;
var min_lvl=document.all.item( "min_lvl_ "+selectedID).innerHTML;
var max_lvl=document.all.item( "max_lvl_ "+selectedID).innerHTML;
window.showModalDialog( "upd.jsp?job_id= "+selectedID+ "&job_desc= "+job_desc+ "&max_lvl= "+max_lvl+ "&min_lvl= "+min_lvl,window, "dialogWidth:500px;dialogHeight:300px;help=no;status=no; ");


window.location.reload(true);
}
else
{
alert( "请选择一行数据!! ");

}
}
function de() //删除所选一行的数据
{
if(selectedID!=null)
{
if(confirm( "你确定要删除! "))
{
window.showModalDialog( "del.jsp?job_id= "+selectedID,window, "dialogWidth:500px;dialogHeight:300px;help=no;status=no; ");
window.location.reload(true);
}
}
else
{
alert( "请先选择一行! ");
}
}
function ins()//添加一行数据
{
window.showModalDialog( "ins.jsp ",window, "dialogWidth:500px;dialogHeight:300px;help=no;status=no; ");//有模对话框同时向下一级页面传送相应的参数!
window.location.reload(true);
}
</script>
</head>

<body style= "cursor:default; border-style: none " bgcolor= "#CCCCCC " >
<table width= "100% " border= "1 " cellspacing= "0 " cellpadding= "0 " bgcolor= "#CCCCCC " style= "display:block " height=20>
<tr align= "center ">
<td id= "t1 " onClick= "dis1() " class= "activelabel "> 项目一 </td>
<td id= "t2 " onClick= "dis2() " > 项目二 </td>
<td id= "t3 " onClick= "dis3() "> 项目三 </td>
</tr>
</table>
<table width= "100% " border= "1 " cellspacing= "0 " cellpadding= "0 " id= "tab1 " bgcolor= "#CCCCCC " style= "display:block " height=330>
<tr>
<td width= "60 "> job_id </td>
<td width= "260 "> job_desc </td>
<td width= "60 "> min_lvl </td>
<td width= "60 "> max_lvl </td>

</tr>
<%

Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ").newInstance();
String url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mako ";
String user= "sa ";
String password= "8912737 ";

Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql= "select * from jobs ";
ResultSet rs=stmt.executeQuery(sql);

while(rs.next()) {%>
<tr id= "DATA_ <%=rs.getString(1)%> " class= "dataRowBlur " onDblClick= "up() ">
<td width= "60 " onClick= "focusDataRow(this.parentNode.id) " id= "d " class= "mainDataCell "> <%=rs.getString( "job_id ")%> </td>
<td width= "260 " onClick= "focusDataRow(this.parentNode.id) " id= "job_desc_ <%=rs.getString(1)%> " class= "mainDataCell "> <%=rs.getString( "job_desc ")%> </td>
<td width= "60 " onClick= "focusDataRow(this.parentNode.id) " id= "min_lvl_ <%=rs.getString(1)%> " class= "mainDataCell "> <%=rs.getString( "min_lvl ")%> </td>
<td width= "60 " onClick= "focusDataRow(this.parentNode.id) " id= "max_lvl_ <%=rs.getString(1)%> " class= "mainDataCell "> <%=rs.getString( "max_lvl ")%> </td>
</tr>
<%
}
%>
</table>
<table width= "100% " cellpadding= "0 " cellspacing= "0 " bgcolor= "#CCCCCC "id= "tab2 " border= "1 " height=330 style= "border-collapse:collapse;display:none ">


<tr>
<td width= "47% "> 统计口径 </td>

<td width= "42% "> 已选口径 </td>
<td width= "6% "> </td>
</tr>
<tr>


</table>


<td>   </td>
<td rowspan= "5 "> </td>
<td rowspan= "5 "> </td>
</tr>

<tr>
<td>   </td>
</tr>
<tr>
<td> <input name= "right " type= "button " value= ">> " /> </td>
</tr>
<tr>
<td> <input name= "left " type= "button " value= "<< " /> </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>

<table width= "100% " border= "1 " cellspacing= "0 " cellpadding= "0 " id= "tab3 " bgcolor= "#CCCCCC " style= "display:none " height= "330 ">
<tr>
<td width= "41% " align= "right "> 报表名称: </td>
<td colspan= "3 "> </td>
</tr>
<tr>
<td rowspan= "3 ">   </td>
<td width= "5% ">   </td>
<td width= "42% " rowspan= "3 "> </td>
<td width= "12% ">   </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
<div align= "center ">
<input type= "button " value= "删 除 " class= "buttonstyle " onClick= "de() " >   
<input type= "button " value= "修 改 " class= "buttonstyle " onclick= "up() " >   
<input type= "button " value= "添 加 " class= "buttonstyle " onClick= "ins() " >   
<input type= "button " value= "退 出 " class= "buttonstyle " onclick= "cl() " >
</div>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
[解决办法]
楼上的,你可真够有心的,那么多代码谁会看啊?也不知道楼主要的是swing还是jsp?
[解决办法]
我不会jsp swing到是会一点点
我有两种办法。 一是
得到选中的那一条记录的主键列的值
然后调用sql语句 把这个值作为条件 进行删除

再有一个 是将ResultSet 设置为可更新的 然后得到用户选中的那条记录在第几行
然后 调用ResultSet的deleteRow方法。

新手的方法 高手不要见笑

读书人网 >J2SE开发

热点推荐