2017-04-09 124 views
0

我在嘗試添加引號時收到錯誤。我試圖從字段中獲取記錄,然後使用Query插入到德比數據庫中。 我得到一個錯誤,說:java.sql.SQLSyntaxErrorException:語法錯誤:遇到 「\ '\ '\',\ '\',\'」 在1號線,列33java.sql.SQLSyntaxErrorException:語法錯誤:在第1行第33列遇到「','','','」

String Query = "select * from QUOTATION order by DATE DESC"; 
        try { 
         Class.forName("org.apache.derby.jdbc.ClientDriver"); 
         Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/MavenUp","adminmu","admin"); 
          Statement st= conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
       ResultSet rs=st.executeQuery(Query); 

          int id =0; 
       rs.beforeFirst(); 
       if(rs.next()) 
          { 

       id = rs.getInt(1); 

          id++; 
          } 
          else 
          { 
           id=1; 
          } 
          String idd = Integer.toString(id); 
          String Query1; 
       Query1 = "Insert into QUOTATION " + "values ("+idd+"','"+ClientName.getText()+"','"+QuotationNo.getText()+"','"+Date.getDate()+"'}"; 
          if(st.executeUpdate(Query1)== 1) 
       { 
        JOptionPane.showMessageDialog(this,"Your Quote info has been Inserted"); 
       } 
       else 
       { 
        JOptionPane.showMessageDialog(this, "Something went wrong Try again"); 
       } 
        }catch (ClassNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       }catch (SQLException e){ 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
          } 
+0

因爲你缺少單引號 –

+0

1.調試工具_方式_在這些類型的問題的更有用比我們什麼您正在執行的查詢是錯誤的。 2.請不要使用字符串連接來構建查詢 - JDBC具有綁定參數的概念,這對於查詢非常有用。 –

+0

然後我應該怎樣,否則字符串連接 – Sparta

回答

1

使用此

Query1 = "Insert into QUOTATION " + "values ('"+idd+"','"+ClientName.getText()+"','"+QuotationNo.getText()+"','"+Date.getDate()+"')"; 
+0

現在它給出這個錯誤 java.sql.SQLSyntaxErrorException:語法錯誤:在第1行第78列遇到「}」 – Sparta

+0

我已經更新了我的答案。 ü只需要刪除'}' –

+0

啊,是工作,但現在它給這個 java.sql.SQLSyntaxErrorException:第1行,列77 – Sparta

相關問題