怎么在JSP代码里取值,访问数据库? 可以在页面做‘删除’删除一行<TR>怎么也在后台数据库里删除呢?想把代码写在JSP页面里如(<% %>)怎么做?比如我想删除‘张三’怎么做? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先连接啊 然后再用sql语句:deletefrom table_namewhere conditions在调用executeUpdate(sql)大致就这样me也只是初学jsp 呵呵 楼主的意思是不是这样啊!点击删除就删除成功了!<a href="delete.jsp?aa=<%=rs.getString("id")%>">删除</a>在delete.jsp页面中要获取到你传的参数aa啊!比如:String id=request.getParameter("aa");String sql="delete from zhuce where id='"+id+"'";这样就达到你的目的了! 把这个表格放入到一个<form>标签中,然后删除的超链接加一个当前信息ID的参数,点击的时候,提交到删除的方法,获取相应的ID,删除相应的记录。这就是思路了。 <% /* 当你点删除时候连接到下个页面同时把要删除的id传到下个页面 <th width="10%" valign="middle"><a href="deletesucess.jsp?id=<%=id%>">删除</a></th> </tr>*/ int pid; pid=Integer.parseInt(request.getParameter("id").trim()); System.out.println(pid); //jdbc连接数据库 Connection con =null; Statement str = null; try{ String url="jdbc:odbc:数据源名"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection(url,"",""); String sql = "delete from 表名 where id = '" + pid + "'"; str=con.createStatement(); str.executeUpdate(sql); }catch(ClassNotFoundException e){ e.printStackTrace(); }catch(SQLException e1){ e1.printStackTrace(); }finally{ try{ if(str!=null){ str.close(); str=null; } if(con!=null){ con.close(); con=null; } }catch(Exception e){ e.printStackTrace(); } } %> 方法确实很多,我给你提供一种思路吧,就是在你从数据库中提取这些数据时,也将id用隐藏表单域放在页面中,当你点 删除 链接时,链接到另一个页面,比如doDel.jsp?id=id这样吧id也传到那个页面去,然后到那个页面获取id,并且执行删除即可 我觉得先发送一个请求到action中,再对数据库进行操作 <script language="Javascript"> <!-- function del(id){ if(confirm("你确定要删除吗?")) top.location="delstudent.jsp?id="+id; } </script> id要提前获得"delete from zhuce where id='"+id <% String id = request.getParameter("id"); Connection conn = null; Statement stmt = null; ResultSet rs = null;try{ Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); }catch(ClassNotFoundException ce){ out.println(ce.getMessage()); }try{String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db"; String user="db"; String password="db"; conn= DriverManager.getConnection(url,user,password); stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); int iBack =stmt.executeUpdate("delete from table where id='"+id+"' "); out.println("<SCRIPT language=JavaScript>alert('操作成功);javascript:history.go(-1);</script>");}catch(SQLException e){ System.out.println(e.getMessage());}finally{ stmt.close(); conn.close();}%> 把“删除”设置为一个按钮或超连接,把这一行的唯一性字段传一个过去 用sql语句定位到这一行删除 如:<a href="del.jsp?ID=<%=对应的Id%>">删除</a> 嗯,想写到删除的这页 可以写成 <a href="#?aa= <%=rs.getString("id")%>">删除 </a> 吧? 就是在页面上用脚本把你要删除的<tr>影藏掉,把id记下来,到后台去写循环删除不就OK了嘛 如何生成一个超大的XML文件 tomcat6发布war应用问题!!! 有没有表单自动排版的工具 eclipse+tomcat5.0 开发jsp连接MYSQL的问题,急!搞了2天了! 在eclipse中项目制作中生成的work文件 如何在前台jsp中实现自增号 如何获取window.showModalDialog垂直滚动条滚动高度 helloword.java怎么运行???要不要配置? 为什么从oracle中选出的数据在applet中显示的全是乱码? jsp同时安装两个web服务器? JBoss中 Javascript不能用"<"或">"判断的问题 wamp 的80端口占用了,怎么改成81?
然后再用sql语句:
delete
from table_name
where conditions
在调用executeUpdate(sql)
大致就这样me也只是初学jsp 呵呵
<a href="delete.jsp?aa=<%=rs.getString("id")%>">删除</a>
在delete.jsp页面中要获取到你传的参数aa啊!
比如:
String id=request.getParameter("aa");
String sql="delete from zhuce where id='"+id+"'";
这样就达到你的目的了!
<%
/*
当你点删除时候连接到下个页面同时把要删除的id传到下个页面
<th width="10%" valign="middle"><a href="deletesucess.jsp?id=<%=id%>">删除</a></th>
</tr>
*/
int pid;
pid=Integer.parseInt(request.getParameter("id").trim());
System.out.println(pid);
//jdbc连接数据库
Connection con =null;
Statement str = null;
try{
String url="jdbc:odbc:数据源名";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url,"","");
String sql = "delete from 表名 where id = '" + pid + "'";
str=con.createStatement();
str.executeUpdate(sql);
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e1){
e1.printStackTrace();
}finally{
try{
if(str!=null){
str.close();
str=null;
}
if(con!=null){
con.close();
con=null;
}
}catch(Exception e){
e.printStackTrace();
}
}
%>
<!--
function del(id){
if(confirm("你确定要删除吗?"))
top.location="delstudent.jsp?id="+id;
}
</script>
"delete from zhuce where id='"+id
String id = request.getParameter("id");
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); }
catch(ClassNotFoundException ce){
out.println(ce.getMessage());
}
try{
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db";
String user="db";
String password="db";
conn= DriverManager.getConnection(url,user,password);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
int iBack =stmt.executeUpdate("delete from table where id='"+id+"' "); out.println("<SCRIPT language=JavaScript>alert('操作成功);javascript:history.go(-1);</script>");
}
catch(SQLException e){
System.out.println(e.getMessage());
}
finally{
stmt.close();
conn.close();
}%>
用sql语句定位到这一行删除
<a href="del.jsp?ID=<%=对应的Id%>">删除</a>
嗯,想写到删除的这页 可以写成 <a href="#?aa= <%=rs.getString("id")%>">删除 </a> 吧?