0

I have got adding a member to the database working. I am trying to work out how to update a row in the table using the same system of passing in the values just not adding a new row, just altering the row using the passed in username.

Here is my method for inserting a new member:

 public static void insertMember(String username,String firstName)
{

    try 
     {  
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection(url, username, password);

        PreparedStatement st = connection.prepareStatement("INSERT INTO Members VALUES (?,?)");
        st.setString(1, username);
        st.setString(2, firstName);
        st.executeUpdate();    
     } 

     catch (SQLException e) 
     {
        System.out.println("SQL Exception: "+ e.toString());
     } 

     catch (ClassNotFoundException cE) 
     {
        System.out.println("Class Not Found Exception: "+ cE.toString());
     }


}
1
  • INSERT statement will insert rows in a table, while UPDATE statement will update a row (or a group of rows) in a table. Commented Feb 4, 2014 at 5:20

3 Answers 3

3

You need to use an UPDATE command instead of INSERT command.

Take a look at SQL UPDATE statement.

You will need to provide some means by which you can identify the row which needs to be updated, but this is dependent upon the structure of your table

Sign up to request clarification or add additional context in comments.

Comments

2

I have got adding a member to the database working. I am trying to work out how to update a row in the table using the same system of passing in the values just not adding a new row, just altering the row using the passed in username.

you need Update command

like

UPDATE Members set username='somename',firstname='firstname' where condition

Comments

0

Need to change SQL command instead of INSERT , uses UPDATE. Below code will update username for matching firstname provided in where clause.

          PreparedStatement st = connection.prepareStatement("UPDATE Members set username=? where firstName=?)");
         st.setString(1, username);
         st.setString(2, firstName);
         int updateStatus=st.executeUpdate();  

2 Comments

I guess you should update the first name of the user based on the username, not backwards :).
Wouldn't that require a where statement so you knew which row was to be updated?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.