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?
LVL 1
allelopathAsked:
Who is Participating?
 
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
 
CEHJCommented:
Try

String sqlString = "INSERT INTO myTable VALUES ('1010421', null)";
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.