为什么删除语句不起作用?

yinliangzhi 2004-09-15 09:14:35
为什么
String sql="DELETE FROM qx where qx_yonghuming='"+d_first+'"';
String sql="DELETE FROM qx where qx_yonghuming='aa'";
都不起作用?


文件1 shanchu.jsp


<%@ page contentType="text/html;charset=GBK" %>
<%@ page language="java" import="java.sql.*" %>

<jsp:useBean id="connDb" scope="page" class="quanxian_db.ConnOracle"/>

<%

String d_first=request.getParameter("text_shanchuyonghu").trim();
String d_second=request.getParameter("text_reshanchuyonghu").trim();
out.println(d_first);
out.println(d_second);
if (d_first.trim() == d_second.trim()) {
String sql="DELETE FROM qx where qx_yonghuming='"+d_first+'"';
String sql="DELETE FROM qx where qx_yonghuming='aa'";
connDb.executeUpdate(sql);
}
else{
out.println("两次输入用户名不一致!");
}
%>



用于连接数据库的javabean:

package quanxian_db;
import java.sql.*;

public class ConnOracle {
String sDBDiver="sun.jdbc.odbc.JdbcOdbcDriver";
Connection connect=null;
//Statement stmt;
ResultSet rs=null;

public ConnOracle(){
try{
Class.forName(sDBDiver);
}
catch(java.lang.ClassNotFoundException e){
System.err.println(e.getMessage());
}
}

public ResultSet executeQuery(String sql){
try{
connect=DriverManager.getConnection("jdbc:odbc:quanxian","system","pass");
Statement stmt=connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}

catch(SQLException ex){
System.err.println(ex.getMessage());
}
return rs;
}

public int executeUpdate(String sql){
int result=0;
try{
connect=DriverManager.getConnection("jdbc:odbc:quanxian","system","pass");
Statement stmt=connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
result=stmt.executeUpdate(sql);
}

catch(SQLException ex){
System.err.println(ex.getMessage());
}
return result;
}


}


用于调用shanchu文件的页面的一部分:

<form action="shanchu.jsp" method="post" >
<table width="65%" border="0" align="center" bgcolor="#99CCFF">
<tr>
<td width="14%" align="left"><strong>删除用户</strong></td>
<td width="30%" align="left"><font color="#000000"><b>输入用户名</b></font><font><b>
<input name="text_shanchuyonghu" type="text" size=15>
</b></font></td>
<td width="31%" align="left"><font color="#ff0033"><b><font color="#000000">确认输入</font>
<input name="text_reshanchuyonghu" type="text" size=15>
</b></font></td>
<td width="11%" align="center">
<input type="submit" name="Submit" value=" 删 除 " >
</td>
</tr>

</table>
</form>

...全文
174 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
yinliangzhi 2004-09-16
  • 打赏
  • 举报
回复
connect.BeginTrans();加在哪儿?
wandou999 2004-09-15
  • 打赏
  • 举报
回复
String sql="DELETE FROM qx where qx_yonghuming='"+d_first+'"';
改成:
String sql="DELETE FROM qx where qx_yonghuming='"+d_first+"'";
xitianjile 2004-09-15
  • 打赏
  • 举报
回复
connect.BeginTrans();
有这个东西吗?
忘记了。。
Tasia 2004-09-15
  • 打赏
  • 举报
回复
connect.rollback();会抛出异常,也要try..catch
yinliangzhi 2004-09-15
  • 打赏
  • 举报
回复
问题解决. llama198011(绝对小盈盈) 的connect.commit();//新增加的语句起作用;但
为什么connect.rollback();//新增加的语句报错?
pifulu 2004-09-15
  • 打赏
  • 举报
回复
1.字符窜的判断问题,你可以尝试在条件判断中打印一些语句,来察看程序是否在预计的条件 中。
2.用execute(sql);主要是把sql打印出来,看看拼接是否正确,然后复制在数据库中是否能运行
llama198011 2004-09-15
  • 打赏
  • 举报
回复
你应该再写一个方法去提交Delete语句,在class ConnOracle类中修改方法public int executeUpdate(String sql);改为:
public int executeUpdate(String sql){
int result=0;
try{
connect=DriverManager.getConnection("jdbc:odbc:quanxian","system","pass");
Statement stmt=connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
result=stmt.executeUpdate(sql);
connect.commit();//新增加的语句
}
catch(SQLException ex){
connect.rollback();//新增加的语句
System.err.println(ex.getMessage());
}
return result;
}

return result;
}
yinliangzhi 2004-09-15
  • 打赏
  • 举报
回复
直接试应该没问题,但我就是想试一下我用的javabean有无问题?
Tasia 2004-09-15
  • 打赏
  • 举报
回复
String sql="DELETE FROM qx where qx_yonghuming='"+d_first+'"';
引号错了。改成:
String sql="DELETE FROM qx where qx_yonghuming='"+d_first+"'";

提问题要把出错的提示列出来,这样别人帮你找错误才会快,才会准确。
wangwei8117 2004-09-15
  • 打赏
  • 举报
回复
你不要使用executeUpdate(sql)函数,直接使用execute(sql)函数试一下!~
yinliangzhi 2004-09-15
  • 打赏
  • 举报
回复
没有任何提示,程序正常执行,就是没有删除掉记录!
linxianlie 2004-09-15
  • 打赏
  • 举报
回复
有没有出现什么提示?
yinliangzhi 2004-09-15
  • 打赏
  • 举报
回复
问题是没有判断,直接用也不行呀!
String sql="DELETE FROM qx where qx_yonghuming='"+d_first+'"';
是不是javabean有问题?
alaal 2004-09-15
  • 打赏
  • 举报
回复
去看看这个,里面有详细解答

http://community.csdn.net/Expert/topic/3364/3364544.xml?temp=.729336
Tasia 2004-09-15
  • 打赏
  • 举报
回复
if (d_first.trim() == d_second.trim()) 这个判断的结果总是false。
因为d_first和d_second是两个不同的String,它们用==比较总是false。所以你的delete语句总是没有执行。
你可以用eqauls来判断:
if(d_first.trim().equals(d_second.trim())){
}
AgathaZ 2004-09-15
  • 打赏
  • 举报
回复
if (d_first.trim() == d_second.trim())

既然两个都是字符型的,就不能用==比较,而是用equals
if (d_first.trim().equals(d_second.trim()))

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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