gpt4 book ai didi

java - 无法使用 netbeans 添加或更新父行

转载 作者:行者123 更新时间:2023-12-02 11:08:22 26 4
gpt4 key购买 nike

当我创建seance时,我的代码遇到了问题,其中另一个表teacher有外键,出现此错误:

a foreign key constraint fails (classroommanag.seance, CONSTRAINT seance_ibfk_4 FOREIGN KEY (id_teacher) REFERENCES teacher (id))

我尝试了很多解决方案,但没有成功。这是我的代码:

String url="jdbc:mysql://localhost:3306/classroommanag";
String login ="root";
String psswd ="";
String group= jComboBox2.getSelectedItem().toString();
int teacher=0;

try {
Timestamp currentdate = new Timestamp((new java.util.Date().getTime()));
jComboBox1.removeAllItems();
Class.forName("com.mysql.jdbc.Driver");
Connection cn = DriverManager.getConnection(url, login, psswd);
Statement st=cn.createStatement();
Statement stat=cn.createStatement();
String req = " SELECT id FROM goupe WHERE groupname = '"+group+"';";
ResultSet com = st.executeQuery(req);

while(com.next()) {
idgroup = com.getInt("id");}
String re = " SELECT id FROM teacher WHERE name = '"+AuthentServer.getname()+"' and password='"+AuthentServer.getpass()+"';";
ResultSet con= stat.executeQuery(re);
id = con.getInt("id");
String ti = " INSERT INTO seance (`temp`,`id_group`,`id_module`,`id_teacher`)VALUES('"+currentdate+"','"+idgroup+"','"+idgroup+"','"+id+"')";
st.execute(ti);
String sql = " SELECT name,etat FROM student WHERE id_goupe='"+idgroup+"';";
ResultSet rs = stat.executeQuery(sql);

while (rs.next()) {
String nom=rs.getString("name");
int etat=rs.getInt("etat");

if(etat==1) {
jComboBox1.addItem(nom+" connected");
currentdate=new Timestamp(new java.util.Date().getTime());
String date=currentdate.toString().substring(0,10);
String nomDossier="C:/wamp/www/cmsEnseignant/Travaux/"+group+"/"+nom+"/"+date;
File dir = new File (nomDossier);
if(!dir.isDirectory()) {
dir.mkdirs();
}
}
else {
jComboBox1.addItem(nom+" not connected");
currentdate=new Timestamp(new java.util.Date().getTime());
String date=currentdate.toString().substring(0,10);
String nomDossier="C:/wamp/www/cmsEnseignant/Travaux/"+group+"/"+nom+"/"+date;
File dir = new File (nomDossier);
if(!dir.isDirectory()) {
dir.mkdirs();
}
}
}
}
}
catch (ClassNotFoundException ex) {
Logger.getLogger(WorkSpace.class.getName()).log(Level.SEVERE, null, ex);
}
catch (SQLException ex) {
Logger.getLogger(WorkSpace.class.getName()).log(Level.SEVERE, null, ex);
}

最佳答案

外键约束失败通常在您尝试向子表插入父表中不存在的数据时发生。正如在 seance 表中一样,id_teacher 是引用 teacher 表中的 id 的外键,然后您作为 id_teacher 插入到 seance 表的 id 必须作为主键 id 存在于 teacher 表中。

更新:

在您的代码中,使用 ResultSet con= stat.executeQuery(re); 执行查询后,您可以使用 id = con.getInt("id") 直接获取值;

但是如果你看到 Resultset 的官方文档,上面写着:

A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.

所以,这一行应该可以解决您的问题。由于您尚未使用任何 next() 方法,因此光标位于第一行之前,该行对于您的外键来说是无效数据。这就是您收到错误的原因。

要解决此问题,如果您的查询应返回多行,则必须使用 while 循环和 con.next() 来迭代所有行,或者您可以使用con.next() 一次,如果它只返回一行数据。

关于java - 无法使用 netbeans 添加或更新父行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50760081/

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