Exception in thread “main” java.sql.SQLException: near “s”: syntax error

前端 未结 2 1656
逝去的感伤
逝去的感伤 2021-01-23 06:45

I wrote a program that populates a database with a list of words. Problem is, it throws up the \"Exception in thread \"main\" java.sql.SQLException: near \"s\": syntax error\" e

相关标签:
2条回答
  • 2021-01-23 07:17

    Use PreparedStatement to avoid problems with erroneous input:

    PreparedStatement p = c.prepare("INSERT INTO WORDS (ID,CAT,WORD) VALUES(?, ?, ?)");
    p.setInt(1, i);
    p.setString(2, wordcat);
    p.setString(3, word);
    p.execute();
    //commit results if using InnoDB, etc
    
    0 讨论(0)
  • 2021-01-23 07:31

    Thanks Rogue for a good code. I had an error in SQLite during INSERT operation, SQL string contained "'" inside a text. So, an escaping in this way helped to me. I've changed a code a bit.

    PreparedStatement prst = conn.prepareStatement(
        "INSERT INTO test(id, name, type) "
        + "VALUES(?, ?, ?)");
    prst.setLong(1, id);
    prst.setString(2, title);
    prst.setObject(3, type);
    prst.execute();
    

    So when I tried to insert a null value in INTEGER column, I used a method setObject().

    0 讨论(0)
提交回复
热议问题