使用java组合框只想从MS Access中删除选定的项目记录不是整个记录
我正在使用java applet GUI应用程序,并希望这样做,当我按下删除按钮时,只有在组合框中选择的项目被删除从MS Access,但我下面的代码删除,全程记录从MS Access ..使用java组合框只想从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...");
}
}
没有错误,但只在我所选字段删除从我的数据库全程记录insted的的...
产量:
apple
banana
grapes
null
in 2nd
1
ResultSet is closed
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
为null。
即UPDATE trans_dest SET route2= NULL where route2='"+r2+"'"
见这个例子也许这将是有益的。
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();
}
}
不工作....它删除整个record..i想要删除该项目从特定的细胞.... – 2013-03-05 07:52:21
我知道你的意思 你可能会用更新查询 看我编辑我的答案 – Azad 2013-03-05 08:37:21
谢谢你soooooo太多了...它的工作原理...我试图解决它从最后一天...终于做到了...谢谢你uuu – 2013-03-05 08:59:47
我想从其中包含ComboBox项不整排的数据库只删除单元格... – 2013-03-05 07:54:58
看到anwser的编辑,这将有助于你 – NPKR 2013-03-05 08:00:50