Question

Hi I am a beginner in java db netbeans.,

I have two tables namely transaction and checks tables in transaction table I have columns named [transID], [PayToOrder], [BankCode], [Checknumber]. in checks table I have columns named [checknumber], [dateissued], [amount], [transID]. I am using a form as an entry.

here is the block of code that I used to insert data in the database.

private void btnAddRecordActionPerformed(java.awt.event.ActionEvent evt) {                                             
    // TODO add your handling code here:
    String trID = txtTransID.getText();
    int ID = Integer.parseInt(trID);
    String pto = txtPtO.getText();
    String bc = txtBankCode.getText();
    String cn = txtCheckNum.getText();
    int chNum = Integer.parseInt(cn);
    String amount = txtAmount.getText();
    int amnt = Integer.parseInt(amount);
    String dates = (String) txtDate.getValue();

    try{
        stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        stmt2 = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        String sql1 = "Select * From transactions";
        String sql2 = "Select * From checks";
        rs1 = stmt1.executeQuery(sql1);
        rs2 = stmt2.executeQuery(sql2);

        rs1.moveToInsertRow();
        rs2.moveToInsertRow();

        rs1.updateInt("transID", ID);
        rs1.updateString("PAYTOORDER", pto);
        rs1.updateString("BANKCODE", bc);
        rs1.updateInt("checknumber", chNum);
        rs2.updateInt("checknumber", chNum);
        rs2.updateInt("AMOUNT", amnt);
        rs2.updateString("DATEISSUED", dates);
        rs2.updateInt("transID", ID);

        rs1.insertRow();
        rs2.insertRow();
        stmt1.close();
        stmt2.close();
        rs1.close();
        rs2.close();

        JOptionPane.showMessageDialog(this, "Successfully Recorded!");
       }

    catch(SQLException err){
        JOptionPane.showMessageDialog(this, err.getMessage());
    }
}

I keep on getting a error:

"Insert on table 'TRANSACTIONS' caused a violation of Foreign Key constraint 'CHECKNUMBER' for key (inputted data).

please if anyone can enlighten me on this part

thanks

Rommel Ando

Was it helpful?

Solution

You are trying to insert data into the table TRANSACTIONS with a CHECKNUMBER that does not exist in the table CHECKS.

So if you have the relation you have above, you will not be able to insert this data in this fashion as the transID and Checknumber are the same for both.

You can either remove one of the foreign keys or you could set the checknumber to NULL for the insert of the transaction, insert the checks, then update the transaction check number to what is should be

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top