0

我正在使用 java applet GUI 应用程序,并且想要这样做,当我按下删除按钮时,只有在组合框中选择的项目会从 MS Access 中删除,但我下面的代码从 MS Access 中删除了整个记录。

code i tried is:

 if(e.getSource()==btn_del)
    {
       try
                {


                    Connection con;

                    DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());

                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

                    con = DriverManager.getConnection("jdbc:odbc:dsnproj","","");

                    Statement s=con.createStatement();

                    String q="select * from trans_dest
                                 where                                    
                                 dest='"+cmb_dest.getSelectedItem()+"'";

                    ResultSet r=s.executeQuery(q);

                    while (r.next()) {


                        r1=r.getString(2);

                        System.out.println(r1);

                        r2=r.getString(3);

                        System.out.println(r2);

                        r3=r.getString(4);
                        System.out.println(r3);

                        r4=r.getString(5);
                        System.out.println(r4);

                        String qryd1,qryd2,qryd3,qryd4;

                        qryd1="DELETE route1 from trans_dest where route1='"+r1+"'";
                        qryd2="DELETE route2 from trans_dest where route2='"+r2+"'";
                        qryd3="DELETE route3 from trans_dest where route3='"+r3+"'";
                        qryd4="DELETE route4 from trans_dest where route4='"+r4+"'";

                       if(r1.equals(cmb_route.getSelectedItem()))

                        {

                            System.out.println("in 1st");
                           int executeUpdate= s.executeUpdate(qryd1);
                           System.out.println(executeUpdate);

                        }

                        else if(r2.equals(cmb_route.getSelectedItem()))

                        {

                            System.out.println("in 2nd");

                            int executeUpdate1 = s.executeUpdate(qryd2);
                            System.out.println(executeUpdate1);
                        }

                        else if(r3.equals(cmb_route.getSelectedItem()))

                        {

                            System.out.println("in 3rd");

                            int executeUpdate2 = s.executeUpdate(qryd3);
                            System.out.println(executeUpdate2);
                        }

                        else if(r4.equals(cmb_route.getSelectedItem()))

                        {

                            System.out.println("in 4th");

                            int executeUpdate3 = s.executeUpdate(qryd4);
                            System.out.println(executeUpdate3);
                        }


                 }




              Frame f=new Frame();

               JOptionPane.showMessageDialog(f, "Deleted product Successfully... ");

              System.out.println("deleted...");
       }

}

没有错误,但从我的数据库中删除了整个记录,仅在我选择的字段中插入...

输出是:

apple
banana
grapes
null
in 2nd
1
ResultSet is closed
4

2 回答 2

0

看看这个例子也许会很有用。

import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;

public class ComboboxTest  extends JFrame implements ActionListener{
    JComboBox  combo ;
    JButton    del;
    public ComboboxTest(){
        combo = new JComboBox(new String[]{"Apple","Orange","Banana"});
        del = new JButton("DELETE");
        del.addActionListener(this);
        this.add(combo, BorderLayout.NORTH);
        this.add(del,BorderLayout.SOUTH);
        this.setSize(300,200);
        this.setVisible(true);
        this.setDefaultCloseOperation(3);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton b = (JButton)e.getSource();
        if(b == del) {
        String query = "update trans_dest set CellName='' where dest = "
            + "'"+combo.getSelectedItem().toString()+"'";
        try{

              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
              Connection c = DriverManager.getConnection("jdbc:odbc:dsnproj","","");
              Statement s = c.createStatement();
              s.executeUpdate(query);
              System.out.println("Selected Item Removed .");
              s.close();

        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
        }//end if

    }


    public static void main(String...args){
        new ComboboxTest();
    }
}
于 2013-03-05T07:25:14.547 回答
0
from my database insted of only on field which i selected..

这意味着您希望只删除选定的行。

根据 DELETE 查询查询,这是不可能的。DELETE 查询将查询满足 where 条件的表中的整个记录​​。

qryd2="DELETE route2 from trans_dest where route2='"+r2+"'";

这将从数据库中删除满足条件的整行。

编辑:

如果要删除列的值,可以使用 将列值设置为空update query

IEUPDATE trans_dest SET route2= NULL where route2='"+r2+"'"

于 2013-03-05T07:05:29.243 回答