简体   繁体   中英

SQL Insert Syntax error

I wrote the following code in a Java program.

public int insertRecord()
       {

connection=PersistentieController.getInstance().getConnection();

int resultaat = 0;

String sql ="INSERT INTO TestDb (Nr Dossier, Année) VALUES (?,?)";

          try 
          {
             pstatement = initStatement(connection, sql);
             pstatement.setString(1, "9999");            
             pstatement.setInt(2, 2012);

             resultaat = pstatement.executeUpdate(); 
             pstatement.close();

          } //einde try
          catch ( SQLException sqlException ){
              sqlException.printStackTrace();

          }  

          return resultaat;

       } //einde insert()

I recieve an syntax error. I checked that i don't use reserved words. Any ideas? Thx.

you have escape the columnnames ( with space such as Nr Dossier ) .

backtick if you are using MySQL ,

INSERT INTO TestDb (`Nr Dossier`, `Année`) VALUES (?,?)

brackets if you are using SQLServer and MS Access ,

INSERT INTO TestDb ([Nr Dossier], [Année]) VALUES (?,?)

In this line of code String sql ="INSERT INTO TestDb (Nr Dossier, Année) VALUES (?,?)"; you have mentioned column name Nr Dossier which is invalid column name.

I'd suggest its because your field 'Nr Dossier' has a space in it.

You didnt mention which SQL type you're using, however [] or ` marks can often fix this.

使用单引号内的字符串值:

String sql ="INSERT INTO TestDb ('Nr Dossier', 'Année') VALUES (?,?)";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM