我正在使用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);
}
答案 0 :(得分:0)
您应该使用executeUpdate()
对数据库进行更新而不是executeQuery()
方法,而executeUpdate()
不会返回ResultSet
。
使用
pst.executeUpdate();
不
ResultSet executeQuery = pst.executeQuery();//not correct
不知道你为什么评论该行。可能是因为你没有收到任何错误