1 Replies - 4938 Views - Last Post: 11 June 2011 - 02:39 PM Rate Topic: -----

#1 seyidesh   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 117
  • Joined: 24-November 10

Saving into Database Table that has foreign key colume

Posted 11 June 2011 - 02:32 PM

Hi guys, please i need your help, I have this assignment to do but i have issues with my codes. The following are my issues

1. I am not sure about the correct codes to use in saving (Int value) items from jcomboBox to my database Table,

2. I am also not sure if am to include the Foreign Column when writing my SQL statement to insert value into the Database table that has Foreign Key column

 try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://127.0.01:3306/CMS", "root", "2bforever");
            quary = "INSERT INTO anniversary VALUES (?,?,?,?,?)";
            ps = con.prepareStatement(quary);
            
            ps.setString(1, jTextField1.getText());
            ps.setString(2, jTextField2.getText());
            ps.setString(3, (String) jComboBox1.getSelectedItem());
            ps.setString(4, (String) jComboBox2.getSelectedItem());
             ps.setString(5, jTextField3.getText());
             int  result =ps.executeUpdate();
             
             con.close();
             jLabel6.setText("Record successfully Saved.");
            
        } catch (Exception se) {System.out.println("Error"+se);
           jLabel6.setText("An Error Occur, Record Can not save");
        }


Note: my database has seven (7) column but here in my code, I only used five (5) column, i did not use those column for Foreign Key.

How can i go about this please?

My run-time Error is
Errorjava.sql.SQLException: Column count doesn't match value count at row 1


Is This A Good Question/Topic? 0
  • +

Replies To: Saving into Database Table that has foreign key colume

#2 nick2price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Saving into Database Table that has foreign key colume

Posted 11 June 2011 - 02:39 PM

If your database table has 7 columns, you have to pass it 7 pieces of information. So include your foreign key columns, even if you pass it an empty value (Although it should still have a value otherwise there is no point in it).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1