0

So this is just a snippet of my code:

Sql = "update budgetreport" +
                " set sales="+salesText.getText()+
                " where quarter="+ qTracker+
                " set cogs="+cogsText.getText()+
                " where quarter="+ qTracker;
        try {
            myStmt.executeUpdate(Sql);
        } catch (Exception e1) {
            e1.printStackTrace();
        }

My problem is, it says there is an SQL syntax. I ran it with just the first part:

Sql = "update budgetreport" +
                " set sales="+salesText.getText()+
                " where quarter="+ qTracker;
        try {
            myStmt.executeUpdate(Sql);
        } catch (Exception e1) {
            e1.printStackTrace();
        }

And it ran fine. But I do not under stand what to do in order to make my first snippet of code function. Thanks for taking your time to read/answer :)! ......

3
  • 1
    I don't know much about MySql, but it seems as if it should be two different update statements: one for sales and one for cogs. Commented Jul 7, 2015 at 21:54
  • 1
    Also you want to use preparedStatements, as building SQL this way will lead you eventually down a dark path Commented Jul 7, 2015 at 21:56
  • If your question is resolved, please mark one of the answers as accepted. Commented Jul 8, 2015 at 20:57

5 Answers 5

6

Only one SET keyword is allowed per update

String sql = "update budgetreport" +
                " set sales=?, cogs=?" + 
                " where quarter=?";

PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, salesText.getText());
...
preparedStatement.executeUpdate();            
Sign up to request clarification or add additional context in comments.

3 Comments

How does the "PreparedStament" object help? Just curious, thanks!
It protects against SQL Injection attacks as well takes care of quoting of character types
Ok, I didn't know that. I'll be sure to use it.
0

Try this:

Sql = "update budgetreport" +
      " set sales="+salesText.getText()+
      ", cogs="+cogsText.getText()+
      " where quarter="+ qTracker;

Watch out for SQL injection attacks and errors, you shouldn't be concatenating with unchecked textbox contents.

2 Comments

Gee I really feel like an idiot now. All I was missing was a comma!
Anyway, thanks for the response, I really appreciate it :)
0

Use a prepared statement. It's far safer.

String sql = "update budgetreport set sales=? where quarter=?";

PreparedStatement statement = con.prepareStatement(sql);

statement.setString(1, salesText.getText());
statement.setString(2, qTracker.toString());

statement.executeUpdate();

Comments

-1

You should probably start here: http://www.w3schools.com/sql/sql_update.asp since your SQL syntax is incorrect. Try

update budgetreport set sales=?,cogs=? where quarter=?

The above should be a prepared statement. See: http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html

Or, you could ease your life a bit by using some light-weight frameworks like this: http://www.sql2o.org/ which abstracts PreparedStatements and makes your code more readable.

Comments

-1

The syntax for the update query is incorrect so you need to change it>

UPDATE table

SET column1 = expression1,

column2 = expression2,
....

WHERE conditions;

for example

Sql =update budgetreport" + " set sales="+salesText.getText()+ ", cogs="+cogsText.getText()+ " where quarter="+ qTracker;"

Comments

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.