Why would JDBC-MySQL throws error in your SQL syntax though my generated query works fine? [duplicate]











up vote
0
down vote

favorite













This question already has an answer here:




  • MySQLSyntaxErrorException near “?” when trying to execute PreparedStatement

    1 answer




There are plenty of similar code blocks in the project I've been working on.



    DBConnector db = new DBConnector();
Connection conn = db.connect();
String insertQuery = "INSERT INTO track_item_result (tirTrackItemId, tirNodeId, tirDetailId, tirResultString, tirResultValue, tirStatus) " + "VALUES (?,?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(insertQuery, Statement.RETURN_GENERATED_KEYS);
pstmt.setInt(1, this.trackItem.getId());
pstmt.setInt(2, this.node.getId());
pstmt.setInt(3, this.tirDetailId);
pstmt.setString(4, this.tirResultString);
pstmt.setString(5, this.tirResultValue);
pstmt.setString(6, this.tirStatus);
System.out.println(pstmt.toString());
pstmt.executeUpdate(insertQuery);

ResultSet generatedKeys = pstmt.getGeneratedKeys();


I printed the query in 3rd-last line in the above code which is a well formed SQL statement.



com.mysql.cj.jdbc.ClientPreparedStatement: INSERT INTO track_item_result (tirTrackItemId, tirNodeId, tirDetailId, tirResultString, tirResultValue, tirStatus) VALUES (3,15,0,'<EOL><EOL><EOL><EOL>','-1','F')


And if I copy-paste this generated query directly on MySQL client, it seems to work fine. But in the project it throws the dreaded error in SQL syntax



java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?,?,?)' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:118)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1393)
at com.mysql.cj.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2353)
at com.mysql.cj.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1303)
at com.example.myproject.models.TrackItemResult.insertInDb(TrackItemResult.java:98)
at com.example.myproject.healthcheck.TrackItemRunnable.run(TrackItemRunnable.java:86)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)


Here line TrackItemResult.insertInDb(TrackItemResult.java:98) is the pstmt.executeUpdate() line.



I'm unable to understand the behavior. The query looks pretty sane and actually works. The values are correctly formatted according to the schema. What else might cause this?










share|improve this question













marked as duplicate by Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 10:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 4




    try pstmt.executeUpdate(); instead of pstmt.executeUpdate(insertQuery);
    – Abhinav Singh
    Nov 12 at 9:13










  • Oops this silly mistake had blocked me for almost an hour! Thanks a ton.
    – Aditya
    Nov 12 at 9:24















up vote
0
down vote

favorite













This question already has an answer here:




  • MySQLSyntaxErrorException near “?” when trying to execute PreparedStatement

    1 answer




There are plenty of similar code blocks in the project I've been working on.



    DBConnector db = new DBConnector();
Connection conn = db.connect();
String insertQuery = "INSERT INTO track_item_result (tirTrackItemId, tirNodeId, tirDetailId, tirResultString, tirResultValue, tirStatus) " + "VALUES (?,?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(insertQuery, Statement.RETURN_GENERATED_KEYS);
pstmt.setInt(1, this.trackItem.getId());
pstmt.setInt(2, this.node.getId());
pstmt.setInt(3, this.tirDetailId);
pstmt.setString(4, this.tirResultString);
pstmt.setString(5, this.tirResultValue);
pstmt.setString(6, this.tirStatus);
System.out.println(pstmt.toString());
pstmt.executeUpdate(insertQuery);

ResultSet generatedKeys = pstmt.getGeneratedKeys();


I printed the query in 3rd-last line in the above code which is a well formed SQL statement.



com.mysql.cj.jdbc.ClientPreparedStatement: INSERT INTO track_item_result (tirTrackItemId, tirNodeId, tirDetailId, tirResultString, tirResultValue, tirStatus) VALUES (3,15,0,'<EOL><EOL><EOL><EOL>','-1','F')


And if I copy-paste this generated query directly on MySQL client, it seems to work fine. But in the project it throws the dreaded error in SQL syntax



java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?,?,?)' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:118)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1393)
at com.mysql.cj.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2353)
at com.mysql.cj.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1303)
at com.example.myproject.models.TrackItemResult.insertInDb(TrackItemResult.java:98)
at com.example.myproject.healthcheck.TrackItemRunnable.run(TrackItemRunnable.java:86)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)


Here line TrackItemResult.insertInDb(TrackItemResult.java:98) is the pstmt.executeUpdate() line.



I'm unable to understand the behavior. The query looks pretty sane and actually works. The values are correctly formatted according to the schema. What else might cause this?










share|improve this question













marked as duplicate by Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 10:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 4




    try pstmt.executeUpdate(); instead of pstmt.executeUpdate(insertQuery);
    – Abhinav Singh
    Nov 12 at 9:13










  • Oops this silly mistake had blocked me for almost an hour! Thanks a ton.
    – Aditya
    Nov 12 at 9:24













up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:




  • MySQLSyntaxErrorException near “?” when trying to execute PreparedStatement

    1 answer




There are plenty of similar code blocks in the project I've been working on.



    DBConnector db = new DBConnector();
Connection conn = db.connect();
String insertQuery = "INSERT INTO track_item_result (tirTrackItemId, tirNodeId, tirDetailId, tirResultString, tirResultValue, tirStatus) " + "VALUES (?,?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(insertQuery, Statement.RETURN_GENERATED_KEYS);
pstmt.setInt(1, this.trackItem.getId());
pstmt.setInt(2, this.node.getId());
pstmt.setInt(3, this.tirDetailId);
pstmt.setString(4, this.tirResultString);
pstmt.setString(5, this.tirResultValue);
pstmt.setString(6, this.tirStatus);
System.out.println(pstmt.toString());
pstmt.executeUpdate(insertQuery);

ResultSet generatedKeys = pstmt.getGeneratedKeys();


I printed the query in 3rd-last line in the above code which is a well formed SQL statement.



com.mysql.cj.jdbc.ClientPreparedStatement: INSERT INTO track_item_result (tirTrackItemId, tirNodeId, tirDetailId, tirResultString, tirResultValue, tirStatus) VALUES (3,15,0,'<EOL><EOL><EOL><EOL>','-1','F')


And if I copy-paste this generated query directly on MySQL client, it seems to work fine. But in the project it throws the dreaded error in SQL syntax



java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?,?,?)' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:118)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1393)
at com.mysql.cj.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2353)
at com.mysql.cj.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1303)
at com.example.myproject.models.TrackItemResult.insertInDb(TrackItemResult.java:98)
at com.example.myproject.healthcheck.TrackItemRunnable.run(TrackItemRunnable.java:86)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)


Here line TrackItemResult.insertInDb(TrackItemResult.java:98) is the pstmt.executeUpdate() line.



I'm unable to understand the behavior. The query looks pretty sane and actually works. The values are correctly formatted according to the schema. What else might cause this?










share|improve this question














This question already has an answer here:




  • MySQLSyntaxErrorException near “?” when trying to execute PreparedStatement

    1 answer




There are plenty of similar code blocks in the project I've been working on.



    DBConnector db = new DBConnector();
Connection conn = db.connect();
String insertQuery = "INSERT INTO track_item_result (tirTrackItemId, tirNodeId, tirDetailId, tirResultString, tirResultValue, tirStatus) " + "VALUES (?,?,?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(insertQuery, Statement.RETURN_GENERATED_KEYS);
pstmt.setInt(1, this.trackItem.getId());
pstmt.setInt(2, this.node.getId());
pstmt.setInt(3, this.tirDetailId);
pstmt.setString(4, this.tirResultString);
pstmt.setString(5, this.tirResultValue);
pstmt.setString(6, this.tirStatus);
System.out.println(pstmt.toString());
pstmt.executeUpdate(insertQuery);

ResultSet generatedKeys = pstmt.getGeneratedKeys();


I printed the query in 3rd-last line in the above code which is a well formed SQL statement.



com.mysql.cj.jdbc.ClientPreparedStatement: INSERT INTO track_item_result (tirTrackItemId, tirNodeId, tirDetailId, tirResultString, tirResultValue, tirStatus) VALUES (3,15,0,'<EOL><EOL><EOL><EOL>','-1','F')


And if I copy-paste this generated query directly on MySQL client, it seems to work fine. But in the project it throws the dreaded error in SQL syntax



java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?,?,?)' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:118)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1393)
at com.mysql.cj.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2353)
at com.mysql.cj.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1303)
at com.example.myproject.models.TrackItemResult.insertInDb(TrackItemResult.java:98)
at com.example.myproject.healthcheck.TrackItemRunnable.run(TrackItemRunnable.java:86)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)


Here line TrackItemResult.insertInDb(TrackItemResult.java:98) is the pstmt.executeUpdate() line.



I'm unable to understand the behavior. The query looks pretty sane and actually works. The values are correctly formatted according to the schema. What else might cause this?





This question already has an answer here:




  • MySQLSyntaxErrorException near “?” when trying to execute PreparedStatement

    1 answer








java mysql sql database jdbc






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 8:59









Aditya

2,0011334




2,0011334




marked as duplicate by Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 10:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 12 at 10:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 4




    try pstmt.executeUpdate(); instead of pstmt.executeUpdate(insertQuery);
    – Abhinav Singh
    Nov 12 at 9:13










  • Oops this silly mistake had blocked me for almost an hour! Thanks a ton.
    – Aditya
    Nov 12 at 9:24














  • 4




    try pstmt.executeUpdate(); instead of pstmt.executeUpdate(insertQuery);
    – Abhinav Singh
    Nov 12 at 9:13










  • Oops this silly mistake had blocked me for almost an hour! Thanks a ton.
    – Aditya
    Nov 12 at 9:24








4




4




try pstmt.executeUpdate(); instead of pstmt.executeUpdate(insertQuery);
– Abhinav Singh
Nov 12 at 9:13




try pstmt.executeUpdate(); instead of pstmt.executeUpdate(insertQuery);
– Abhinav Singh
Nov 12 at 9:13












Oops this silly mistake had blocked me for almost an hour! Thanks a ton.
– Aditya
Nov 12 at 9:24




Oops this silly mistake had blocked me for almost an hour! Thanks a ton.
– Aditya
Nov 12 at 9:24












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










try pstmt.executeUpdate(); instead of pstmt.executeUpdate(insertQuery);






share|improve this answer





















  • Done and accepted. Thanks.
    – Aditya
    Nov 12 at 9:25


















up vote
1
down vote













pstmt.executeUpdate(); No need for parameters



or you should Check parameter types for you sql






share|improve this answer




























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    try pstmt.executeUpdate(); instead of pstmt.executeUpdate(insertQuery);






    share|improve this answer





















    • Done and accepted. Thanks.
      – Aditya
      Nov 12 at 9:25















    up vote
    1
    down vote



    accepted










    try pstmt.executeUpdate(); instead of pstmt.executeUpdate(insertQuery);






    share|improve this answer





















    • Done and accepted. Thanks.
      – Aditya
      Nov 12 at 9:25













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    try pstmt.executeUpdate(); instead of pstmt.executeUpdate(insertQuery);






    share|improve this answer












    try pstmt.executeUpdate(); instead of pstmt.executeUpdate(insertQuery);







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 12 at 9:25









    Abhinav Singh

    764




    764












    • Done and accepted. Thanks.
      – Aditya
      Nov 12 at 9:25


















    • Done and accepted. Thanks.
      – Aditya
      Nov 12 at 9:25
















    Done and accepted. Thanks.
    – Aditya
    Nov 12 at 9:25




    Done and accepted. Thanks.
    – Aditya
    Nov 12 at 9:25












    up vote
    1
    down vote













    pstmt.executeUpdate(); No need for parameters



    or you should Check parameter types for you sql






    share|improve this answer

























      up vote
      1
      down vote













      pstmt.executeUpdate(); No need for parameters



      or you should Check parameter types for you sql






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        pstmt.executeUpdate(); No need for parameters



        or you should Check parameter types for you sql






        share|improve this answer












        pstmt.executeUpdate(); No need for parameters



        or you should Check parameter types for you sql







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 9:25









        yi mu

        213




        213















            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python