gpt4 book ai didi

java - 如何从 Java Netbeans 中的 GUI 更新表?

转载 作者:太空宇宙 更新时间:2023-11-04 13:41:44 27 4
gpt4 key购买 nike

我正在使用 Java Netbeans 设计 GUI,并尝试将 GUI 字段中的值插入到表中。它显示没有错误,但没有插入任何内容。这是我的代码:

Statement stmt = null;
try {

String sql = "Insert into Elective (subject_code,topic,syllabus,credit,Expert_ID,startsession,endsession) values (?,?,?,?,?,?,?)";
pst = conn.prepareStatement(sql);
String subject = Subject_code.getSelectedItem().toString();
pst.setString(1, subject);
pst.setString(2, topic.getText());
pst.setString(3, syllabus.getText());
pst.setString(4, credit.getText());

String Expert = Expert_ID.getSelectedItem().toString();
pst.setString(5, Expert);

int y1 = startsession.getYear();
int y2 = endsession.getYear();
pst.setInt(6, y1);
pst.setInt(7, y2);
//ResultSet executeQuery = pst.executeQuery();
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Elective;");
while (rs.next()) {
String subject_code1 = rs.getString("subject_code");
String topic1 = rs.getString("topic");
String syllabus1 = rs.getString("syllabus");
String credit1 = rs.getString("credit");
String Expert_ID1 = rs.getString("Expert_ID");
int startsession1 = rs.getInt("startsession");
int endsession1 = rs.getInt("endsession");
System.out.println("Subject Code = " + subject_code1);
System.out.println("Topic = " + topic1);
System.out.println("Syllabus=" + syllabus1);
System.out.println("Credit = " + credit1);
System.out.println("Expert_ID = " + Expert_ID1);
System.out.println("Start session = " + startsession1);
System.out.println("End session = " + endsession1);
System.out.println();
}
rs.close();
stmt.close();

} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}

最佳答案

您应该使用 executeUpdate() 来更新数据库,而不是 executeQuery() 方法,并且 executeUpdate() 不会返回 ResultSet

使用

 pst.executeUpdate();

不是

ResultSet executeQuery = pst.executeQuery();//not correct

不知道您为什么评论该行。大概这就是您没有收到任何错误的原因

关于java - 如何从 Java Netbeans 中的 GUI 更新表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31189538/

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