0

I'm creating a program where I should save data on database. I have two frames but only using one table in one database and on the first frame I already saved data on row 1-6. Now on the second frame I want to add data on row 7-12 only. How can I do this? Or should I make another table instead?

I'm newbie to Java, so help would be great.

Here's my code for the save:

    JOptionPane.showMessageDialog(this, "Save this input?", "Confirm", JOptionPane.QUESTION_MESSAGE);

    strCountry = txtCountry.getText();
    strMembers = txtareaMembers.getText();
    strSong = txtSong.getText();
    strAlbum = txtAlbum.getText();
    Concert = (String) cmbConcerts.getSelectedItem();
    Biblio = (String) cmbBiblio.getSelectedItem();

    btnSubFirst.setEnabled(false);
    btnSubPrev.setEnabled(false);
    btnSubNext.setEnabled(true);
    btnSubLast.setEnabled(true);
    btnSubEdit.setEnabled(false);
    btnSubSave.setEnabled(false);
    btnSubCancel.setEnabled(true);
    txtCountry.setEditable(false);
    txtSong.setEditable(false);
    txtAlbum.setEditable(false);

    SaveData();
public void SaveData() {
    try {
        String strQuery = "INSERT INTO tblband(bandname,label,genre,year,member,album,country,members,song,recent,concert,biblio)" + "VALUES" + "(?,?,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement st = null;
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/banddb", "root", "");
        st = con.prepareStatement(strQuery);
        st.setString(1, strBand);
        st.setString(2, strRecord);
        st.setString(3, Genre);
        st.setString(4, Year);
        st.setString(5, Member);
        st.setString(6, Album);
        st.setString(7, strCountry);
        st.setString(8, strSong);
        st.setString(9, strAlbum);
        st.setString(10, Concert);
        st.setString(11, Biblio);
        st.setString(12, strMembers);
        st.executeUpdate();
        con.close();
        JOptionPane.showMessageDialog(null, "Data is successfully inserted into database.");
    } catch(Exception e) {
        JOptionPane.showMessageDialog(null, "Error in submitting data." + e);
    }
}
Unihedron
  • 10,902
  • 13
  • 62
  • 72
  • your **INSERT INTO** statement, only include the columns you want to insert into (7-12 headings) not (1-12). You might also need to add in the row you want to insert them into (instead of creating a new row)- so include a **where** clause – jbutler483 Sep 05 '14 at 12:54
  • Should I change the INSERT INTO a SELECT * FROM? or I can just retain it? – Enna Horan Sep 05 '14 at 13:33
  • INSERT should be ok, but an UPDATE query may also be what you're looking for: (See my answer below) – jbutler483 Sep 05 '14 at 13:34
  • Bottom of this page: http://www.w3schools.com/sql/sql_insert.asp – jbutler483 Sep 05 '14 at 13:35
  • oh by the way, in this code I already just typed the columns where I'm gonna insert data, but what parameter should I use? 1-6 or 7-12? – Enna Horan Sep 05 '14 at 13:57
  • include whichever column names you want to update (in a particular order), then in your values bit add the values (in order) as well – jbutler483 Sep 05 '14 at 13:59

1 Answers1

1

You could use an update query:

The UPDATE statement is used to update existing records in a table.

UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

A working example of this can be found: here.

Notice the WHERE clause in the SQL UPDATE statement!

The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

jbutler483
  • 24,074
  • 9
  • 92
  • 145