I am getting the following exceptions I tried placing quotes and commas between the values still I am not able to resolve

问题内容:

You have an error in your SQL syntax; check the manual that corresponds
to your MariaDB server version for the right syntax to use near ‘1,2,3,4)
values (‘2017-01-11′,’P’,’P’,’P’,’A’)’ at line 1

The error is possibly in the insertColumn and insertValue variable. I am not able to get the syntax right. Possibly I may be missing some quotes in the variables, but I don’t know where. I referred to different sources online but the attempts failed. columnNames is a list that has all the columns.

 String insertColumns = ""; 
 String insertValues = "";

 if(columnNames != null && columnNames.size() > 0){
    insertColumns +=  columnNames.get(0);
    insertValues += "?";
 }


 for(int i = 1; i < columnNames.size();i++){
  insertColumns += "," + columnNames.get(i) ;
  insertValues += ","+"?";
 }
 System.out.println(insertColumns);
 String insertSql; 
 //Here is where the problem lies
 insertSql = "INSERT INTO os ("+ insertColumns+") values (" + insertValues + 
 ")";
 System.out.println(insertSql);        
 try{
 PreparedStatement ps = conn.prepareStatement(insertSql);
 ps.setDate(1, sqlStartDate);
 for(int i=1;i<=no_of_stud;i++){
    int j=i+1;
    int k=i-1;
    ps.setString(j, present[k]);
 }

问题评论:

1  
What’s the output from System.out.println(insertSql); ?
    
You can eliminate a lot of ambiguity by using String insertColumns = String.join(",", columnNames); and String insertValues = String.join(",", Collections.nCopies(columnNames.size(), "?"));.
    
Try to edit : insertColumns += columnNames.get(0); To insertColumns += "'"+ columnNames.get(0) +"'"; and insertColumns += "," + columnNames.get(i) ; to `insertColumns += “,” + “‘”+ columnNames.get(i) +”‘”;
    
Please edit your question to post a title that accurately represents/summarizes your question.
    
The column names are just “1” up to “4”? That are no column names; you could use stud1stud4. And the start date column is missing.

原文地址:

https://stackoverflow.com/questions/47755419/i-am-getting-the-following-exceptions-i-tried-placing-quotes-and-commas-between

Tags:, ,

添加评论