【发布时间】:2021-02-16 21:25:07
【问题描述】:
我正在尝试在数据库中插入值,以便如果用户选择标记基础然后标记被保存并且如果用户选择等级基础标记字段为空并且应该存储等级我的主键是 gid,它是自动记录和sid 是外键,如果有人可以帮助我,我将无法获取并解决此错误。
private void hide1ActionPerformed(java.awt.event.ActionEvent evt) {
regstration r=new regstration();
Daoregistration dao=new Daoregistration();
if(mark1.getText().length()<0)
{
r.marks=Integer.parseInt(mark1.getText());
}
r.ch=Integer.parseInt(jComboBox2. getSelectedItem().toString());
r.semister=jComboBox1.getSelectedItem().toString();
r.subject1=subj1.getText();
if(jRadioButton2.isSelected()){
r.grade1=(String)jComboBox3.getSelectedItem();
}
int a =dao.gpa(r);
System.out.print(a);
try{
String qry="select * from formula where grades=? or percentage=?";
PreparedStatement pst=dao.con.prepareStatement(qry);
pst.setInt(1, Integer.parseInt(mark1.getText()));
pst.setString(2,(String)jComboBox3.getSelectedItem());
ResultSet rs=pst.executeQuery();
if(rs.next())
{
r.gpoint=rs.getFloat("grade points");
}
}
catch(Exception ex){
}
subj1.setVisible(false);
mark1.setVisible(false);
jComboBox2.setVisible(false);
jComboBox3.setVisible(false);
}
private void hide2ActionPerformed(java.awt.event.ActionEvent evt) {
subj2.setVisible(false);
mark2.setVisible(false);
jComboBox4.setVisible(false);
jComboBox10.setVisible(false);
}
private void jComboBox5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void hide3ActionPerformed(java.awt.event.ActionEvent evt) {
subj3.setVisible(false);
mark3.setVisible(false);
jComboBox5.setVisible(false);
jComboBox11.setVisible(false);
}
private void hide4ActionPerformed(java.awt.event.ActionEvent evt) {
subj4.setVisible(false);
mark4.setVisible(false);
jComboBox6.setVisible(false);
jComboBox12.setVisible(false);
}
而我的 gpa 模块代码是
int gpa(regstration r)
{
int res=0;
try{
connection();
String qry="insert into gpa values(?,?,?,?,?,?,?)";
PreparedStatement pst=con.prepareStatement(qry);
pst.setInt(1, r.sid);
pst.setString(2,r.semister);
pst.setString(3,r.subject1);
pst.setInt(4,r.marks);
pst.setString(5,r.grade1);
pst.setInt(6,r.ch);
pst.setFloat(7,r.gpoint);
res=pst.executeUpdate();
}
catch(Exception e)
{
System.out.println(e.toString());
}
return res;
}
}
【问题讨论】: