我的数据库上有一个布尔列,它告诉我用户是否是ADMIN。因此,在删除这些布尔TRUE用户之一之前,我想检查数据库中是否还有一个,如果只有一个,我希望系统不允许删除它。请检查我的代码并提供一些想法,这是到目前为止的代码:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
boolean aux = false;
int sum = 0;
String sqlQuery = "select sum (Admin) from UserInfo";
try {
pst = conn.prepareStatement(sqlQuery);
pst.setInt(1, sum);
pst.execute();
if (sum >1){
int q = JOptionPane.showConfirmDialog(null, "Do you really want to delete?", "Confirmation", JOptionPane.YES_NO_OPTION);
if (q==0) {
String sql="delete from UserInfo where Name = ?";
try {
pst = conn.prepareStatement(sql);
pst.setString(1, text_name.getText());
pst.execute();
//JOptionPane.showMessageDialog(null, "Deleted");
}catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
rs.close();
pst.close();
} catch(Exception e) {
}
}
Update_Table();
FieldsCleaner();
}
} else {
JOptionPane.showMessageDialog(null, "Single ADMIN, can't be deleted");
}
}catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
rs.close();
pst.close();
} catch(Exception e) {
}
}
}
答案 0 :(得分:0)
你们给的答案,使我更加努力了。猜猜我已经为此创建了一个非常动态的代码。您可以根据需要使用它。 在这里:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
boolean aux = false;
int sum = 0;
try {
int row = Table_UserInfor.getSelectedRow();
String Table_click=(Table_UserInfor.getModel().getValueAt(row, 1).toString());
String sqlQuery ="select * from UserInfo where Name = '"+Table_click+"' ";
pst =conn.prepareStatement(sqlQuery);
rs =pst.executeQuery();
aux=rs.getBoolean("Admin");
if (rs.next() && aux == true){
try {
String sqlCount = "select count(*) cnt from UserInfo where Admin = 1";
pst =conn.prepareStatement(sqlCount);
rs =pst.executeQuery();
sum = rs.getInt("cnt");
if (rs.next() && sum > 1) {
int q = JOptionPane.showConfirmDialog(null, "Do you really want to delete?", "Confirmation", JOptionPane.YES_NO_OPTION);
if (q==0) {
String sql="delete from UserInfo where Name = ?";
try {
pst = conn.prepareStatement(sql);
pst.setString(1, text_name.getText());
pst.execute();
}catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
rs.close();
pst.close();
} catch(Exception e) {
}
}
Update_Table();
FieldsCleaner();
}
}else {
JOptionPane.showMessageDialog(null, "Single ADMIN, can't be deleted");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
rs.close();
pst.close();
} catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
}
else if (aux == false){
int q = JOptionPane.showConfirmDialog(null, "Do you really want to delete?", "Confirmation", JOptionPane.YES_NO_OPTION);
if (q==0) {
String sql="delete from UserInfo where Name = ?";
try {
pst = conn.prepareStatement(sql);
pst.setString(1, text_name.getText());
pst.execute();
}catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
rs.close();
pst.close();
} catch(Exception e) {
}
}
Update_Table();
FieldsCleaner();
}
}
}catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
} finally {
try {
rs.close();
pst.close();
} catch(Exception e) {
}
}
}