无法 删除数据库记录 这是为什么啊

caihuoyan 2008-11-20 10:58:25
无法 删除数据库记录 这是为什么啊

connDB.JAVA文件

package com.netbean.web;

import java.sql.*;
import java.sql.DriverManager;


public class connDB{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
public connDB(){
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(java.lang.ClassNotFoundException e){
System.err.println(e.getMessage());
}
}

public ResultSet executeQuery(String sql){
try{
conn=DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=sa");
stmt=conn.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{
conn=DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=sa");
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
result=stmt.executeUpdate(sql);
}catch(SQLException ex){
result=0;
}
return result;
}

public void close(){
try {
if (rs != null) rs.close();
}
catch (Exception e) {
e.printStackTrace(System.err);
}
try {
if (stmt != null) stmt.close();
}
catch (Exception e) {
e.printStackTrace(System.err);
}
try {
if (conn != null) {
conn.close();
}
}
catch (Exception e) {
e.printStackTrace(System.err);
}
}


public String chStr_In(String str){
if(str==null){
str="";
}else{
try{
str=(new String(str.getBytes("iso-8859-1"),"GB2312")).trim();
str=str.replace('\'',(char)1);
}catch(Exception e){
e.printStackTrace(System.err);
}
}
return str;
}

public String chStr_Out(String str){
if(str==null){
str="";
}else{
try{
str=str.replace((char)1,'\'');
}catch(Exception e){
e.printStackTrace(System.err);
}
}
return str;
}
}





用来删除记录的 del。jsp文件


<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.lang.*"%>
<jsp:useBean id="ConnDB" scope="page" class="com.netbean.web.ConnDB"/>
<jsp:useBean id="ChStr" scope="page" class="com.netbean.web.ChStr"/>
<jsp:include page="../huotaiguanli/safe.jsp"/>
<%



String sql="delete from news where Id=3";

int ret=0;
ret=ConnDB.executeUpdate(sql);


if (ret==1){
out.println("<script language='javascript'>alert('信息删除成功!');window.location.href='../huotaiguanli/news.jsp';</script>");
}else{
out.println("<script language='javascript'>alert('信息删除失败!');window.location.href='../huotaiguanli/news.jsp';</script>");

}
%>
...全文
73 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanbin1016 2008-11-21
  • 打赏
  • 举报
回复
首先确定你数据库表里是否真的有ID为3的记录 在输出一下返回值ret看他等于多少 如果大于一代表你数据库里ID为3的记录不只一条 都没问题的话就在捕获下异常 看看删除时报什么错误吧
freedom2001 2008-11-21
  • 打赏
  • 举报
回复
1、去数据库执行delete from news where Id=3看看好用不
2、看看后台有没有报错
zhj92lxs 2008-11-21
  • 打赏
  • 举报
回复
public int executeUpdate(String sql){
int result=0;
try{
conn=DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=sa");
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
result=stmt.executeUpdate(sql);
}catch(SQLException ex){
result=0;
e.printStackTrace();
}
return result;
}

这样看看是不是有异常,另外确定有id为3的记录
zidasine 2008-11-20
  • 打赏
  • 举报
回复
debug下看看executeUpdate方法里是不是发生了SQLException 异常
fx290318458 2008-11-20
  • 打赏
  • 举报
回复
public int executeUpdate(String sql){
int result=0;
try{
conn=DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=sa");
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
result=stmt.executeUpdate(sql);
}catch(SQLException ex){
result=0;
}
return result;
}
老兄你看你的 result声明了应该改为你前面声明的rs吧

81,092

社区成员

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

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