我正在使用 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