JAVA从JTable中直接删除数据库中数据出错!

来源:百度知道 编辑:UC知道 时间:2024/05/29 09:19:13
还望有高手解决下!
public static class DeleteAction implements ActionListener{
public void actionPerformed(ActionEvent e){
int i=jTable.getSelectedRow();
int ID=Integer.parseInt((String)jTable.getValueAt(i, 0));
JOptionPane.showMessageDialog(null, ID);
if(i==-1) JOptionPane.showMessageDialog(null,"请选择要删除的行!");
else{
String sql="delete from tb_reader where ReaderID="+ID;
int j=Dao.executeUpdate(sql);
if(j==1) JOptionPane.showMessageDialog(null, "删除成功!");
else JOptionPane.showMessageDialog(null, "删除失败!");
}
}
}

过程中显示的ID值也正确,可是都是删除失败!Dao.executeUpdate()执行insert也成功了,但在这部分过不了!
public static int executeUpdate(String sql){
try {
if(conn==null){
new Dao();
return conn.createStatement().executeUpdate(sql);
}
else return -1;
} catch (SQLException e) {
System.out.println(e.getMes

看看你的表是否与其它表的联系,每条记录是否与另外一个表中的记录有联系,这样的话如查你的设置不对是删除失败的,但插入就可以。检查一下你的代码,还有就是你要把错误信息都贴出来,大家才能帮你

晕,你这是数组越界了, 你是不是用vector 或者list 或者数组之类的东西,然后遍历或者引用其中一个元素,超出来vector的尺寸,你仔细检查下代码
ArrayIndexOutOfBoundsException: 2 >= 2
数组只有两个元素,0和1
你引用 vector.element(2) 就是引用第三个元素,会越界

过不了的部分 报异常了没有 把异常贴出来
就是这句
System.out.println(e.getMessage()); 把这句打印出来的内容贴出来

主外键关联……
你要删除的表与别的表有关联,所以删不掉

有关java的问题,这里都有很详细的讲解,你可以参考一下,方法有很多
参考资料:http://www.baidu.com/s?wd=%B1%B1%B7%E7%CD%F8&rsv_bp=0&rsv_spt=3&inputT=422

你敢把错误堆栈信息贴出来么?