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();
        }
    }

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 :)

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..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.