gpt4 book ai didi

java - 删除数据库中的行

转载 作者:搜寻专家 更新时间:2023-10-30 20:16:12 25 4
gpt4 key购买 nike

我想删除从JTable中选择的行从数据库中,我检查了我的 java 程序是否已连接到数据库,但我很困惑如何从 ms access 中删除整行数据库不影响或触及任何列只是想删除 jtable 中的选定行

我的数据库由 8 列组成,全名、父亲姓名、父亲 cnic、出生日期、类(class)、地址、城市和省份

int x = MyTable.getSelectedRow();
String b = String.valueOf(MyTable.getValueAt(x, 1));

try {
Connection con;
Statement stmt;


Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
con =DriverManager.getConnection("jdbc:ucanaccess://C://Users//abdul//Desktop/StudentDatabase.accdb");


stmt = con.createStatement();

stmt.executeUpdate("delete from StudentDatabase where row = 'x'" );


stmt.close();
con.close();
}
catch(Exception e) {
e.printStackTrace();
}

最佳答案

stmt.executeUpdate("delete from StudentDatabase where row = 'x'" );

这个 SQL 查询是错误的。例如,如果您在数据库中有下表,客户 ID 客户名称联系人姓名
1 迈克 +248439533
2 鲍勃 +345353535

如果你想删除“Mike”这一行,那么你应该将查询写成

从 StudentDatabase 中删除 CustomerName = 'Mike'

如果要删除CustomerID为“2”的行,那么查询应该写成

从 StudentDatabase 中删除 CustomerID = 2

对于您的情况,我希望这将是您的答案。

public class Test {
public static void main(String[] args) {
int rowIndex = MyTable.getSelectedRow();
String name = String.valueOf(MyTable.getValueAt(rowIndex, 1));

try {
Connection con;
Statement stmt;


Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
con = DriverManager.getConnection("jdbc:ucanaccess://C://Users//abdul//Desktop/StudentDatabase.accdb");


stmt = con.createStatement();

int result = stmt.executeUpdate("delete from StudentDatabase where CustomerName = '"+name+"'" );

if(result!=1){
JOptionPane.showMessageDialog(null,"No record exists related to "+name);
}



stmt.close();
con.close();
}
catch(Exception e) {
e.printStackTrace();
}
}

关于java - 删除数据库中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39061724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com