0

Why do I always get the error Student_Id do not have a default value?
Is there something some with my code?
Cant I do 2 PreparedStement and execute it together?

private void cmdaddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cmdaddMouseClicked
        // TODO add your handling code here:
        //String sql = "INSERT INTO student (Student_ID, Last_Name, First_Name, Middle_Initial, Address, Course, Year, Contact_No, Equipment_Name, Quantity, Date_Borrowed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        String sql = "INSERT INTO student (Student_ID, Last_Name, First_Name, Middle_Initial, Address, Course, Year, Contact_No, Date_Borrowed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
        String sql2 = "INSERT INTO equipment (Equipment_Name, Quantity) VALUES (?, ?)";
        try{
            pst = conn.prepareStatement(sql);
            
            pst.setString(1, txtid.getText());
            pst.setString(2, txtlast.getText());
            pst.setString(3, txtfirst.getText());
            pst.setString(4, txtinitial.getText());
            pst.setString(5, txtaddress.getText());
            pst.setObject(6, itemcourse.getSelectedItem());
            pst.setObject(7, itemyear.getSelectedItem());
            pst.setString(8, txtcontact.getText());
            pst.setObject(9, jDateChooser1.getDate());
            
            pst.execute();
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
            e.printStackTrace();
        }
        
        try{
            pst2 = conn.prepareStatement(sql2);
            pst2.setObject(1, comboequip.getSelectedItem());
            pst2.setObject(2, itemquantity.getSelectedItem());
            
            pst2.execute();
           

        }
        
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
            e.printStackTrace();
        }
    }
3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by javalover
0

Student_ID is not a string, could you change line 9 to:

pst.setInt(1, txtid.getInt());

Yes, you can usually execute to different prepared statements.

Hope that helps :)

0

Hi frnd!! check what type of field you created in your database for stu_id.. May be auto number or what, if so it wont work else change its type to Number and do as before statement..That sounds good.. Normally we will be keeping that as auto number only then only it allocates incremental numbers for each registration.. And surely you can use two prepared statements..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.