• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 357
  • Last Modified:

MySQLSyntaxException with INSERT statement

This gives a MySQLSyntaxErrorException: You have an error in your SQL syntax message:;

String sqlString = "INSERT INTO myTable VALUES ('1010421', 'null')";
int val = statement.executeUpdate(sqlString);

What is wrong?
0
allelopath
Asked:
allelopath
  • 4
  • 4
  • 3
  • +2
4 Solutions
 
CEHJCommented:
Try

String sqlString = "INSERT INTO myTable VALUES ('1010421', null)";
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
there are several things:
* you use executeUpdate for an INSERT ???  shouldn't that be executeInsert ?
* you write 'null',  shouldn't that be NULL (without quotes) ?
* int val = statement  ...  do you expect a return value?
* INSERT INTO myTable  should be followed by the column names :  INSERT INTO myTable ( col1, col2 )   and then the values clause.
 
0
 
allelopathAuthor Commented:
I think null instead of 'null' is part of the answer, but not all of it.

I don't see a method called executeInsert()
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
CEHJCommented:
Maybe a numeric column? Try

String sqlString = "INSERT INTO myTable VALUES (1010421, null)";
0
 
CEHJCommented:
That will only work for a table of two columns. Please post the output of

desc myTable;

from the mysql command line
0
 
malfunction84Commented:
executeUpdate() is for any sort of query that doesn't return results, which includes INSERT, UPDATE, DELETE and even things like ALTER TABLE.  executeQuery() returns a ResultSet and is used for SELECT statements.
0
 
objectsCommented:
did utry angelIII's suggestion?

 INSERT INTO myTable ( col1, col2 ) values ('1010421', null)

(replacing col1 and col2 with your column names

and also try it thru mysql directly
 
0
 
allelopathAuthor Commented:
desc myTable;
says that the table doesn't exist.

In the terminal, I do a "use mydb"
but I have no equivalent in the java code (and so, perhaps the table is not being created
in the database I think it is)
Do i need a "use db" statement in the Java code?
0
 
objectsCommented:
problems with your comnnection string.
you need to specify the correct database in your comnnection string

0
 
objectsCommented:
or u could try:

 INSERT INTO dbname.myTable ( col1, col2 ) values ('1010421', null)

but i'd recomend a different connection string to deal with different databases.
0
 
allelopathAuthor Commented:
I have Connection connection = DriverManager.getConnection(url, user, password);
where url is:
    private String url = "jdbc:mysql://localhost:3306/mysql";
What are the parts of this String, ie what is the database?
0
 
objectsCommented:
mysql (the 2nd mention)
0
 
CEHJCommented:
:-)

It's not necessary to specify the column names unless you're using fewer values than you have columns
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now