2016-10-29 38 views
0

這一直讓我發瘋,我相信這很簡單。當我嘗試從出現的表格輸入保留時,我收到了一個'值必須包含至少一個元素'的錯誤。這一切都運行良好。無論是在VALUES部分中使用引號,還是在分隔逗號中加上(+)符號或引號,都會得到不同的錯誤消息。當我把報價放在table_num上時,我得到了錯誤,並告訴我你不能將CHAR插入到INTEGER中。當我刪除引號時,我得到的錯誤告訴我 - 嚴重:java.sql.SQLSyntaxErrorException:列'TABLE_NUM'或者不在FROM列表中的任何表中,或者出現在聯接規範中等等。誰能告訴我發生了什麼?這是jsp代碼。提前致謝。在數據庫中插入預約

 <%  
      int tableNum = 0; 
      String firstName = null; 
      String lastName = null; 
      String Address = null; 
      int Phone = 0; 
      java.sql.Date date = null; 
      int People = 0; 

      if (request.getParameter("table_num")!=null){ 
       tableNum = Integer.parseInt(request.getParameter("table_num")); 
      } 
      if (request.getParameter("first")!=null){ 
       firstName = request.getParameter("first"); 
      } 
      if (request.getParameter("last")!=null){ 
       lastName = request.getParameter("last"); 
      } 
      if (request.getParameter("address")!=null){ 
       Address = request.getParameter("address"); 
      } 
      if (request.getParameter("phone")!=null){ 
       Phone = Integer.parseInt(request.getParameter("phone")); 
      } 
      if (request.getParameter("date")!=null){ 
       java.util.Date utilDate = new java.util.Date(request.getParameter("date")); 
       date = new java.sql.Date(utilDate.getTime()); 
      } 
      if (request.getParameter("people")!=null){ 
       People = Integer.parseInt(request.getParameter("people")); 
      } 
      if(tableNum != 0 && firstName != null && lastName != null && Address != null && Phone != 0 && date != null && People != 0){ 
       String URL = "jdbc:derby://localhost:1527/Reservations"; 
       String USERNAME= "johnpaul"; 
       String PASSWORD= "purlease"; 
       Connection myCon = null; 
       Statement ste = null; 
       PreparedStatement preparedStmt = null; 

       try{ 
        Class.forName("org.apache.derby.jdbc.ClientDriver"); 
        System.out.println("Connecting to DB..."); 
        Connection con=DriverManager.getConnection("jdbc:derby://localhost:1527/Reservations","johnpaul", "purlease"); 
        System.out.println("Connected successfuly"); 

        System.out.println("Inserting records into table");  
        Statement st = con.createStatement(); 
        String query = "INSERT INTO JOHNPAUL.CUSTOMER_RESERVATIONS(TABLE_NUM,FIRST_NAME,LAST_NAME,ADDRESS,TELEPHONE,DATE,NUMBER_IN_PARTY)VALUES(table_num,first,last,address,phone,date,people)"; 


        st.executeUpdate (query); 
        System.out.println("Records inserted"); 


        }catch(SQLException se){ 
        se.printStackTrace(); 
       }catch(ClassNotFoundException se){ 
      //Handle errors for JDBC 
        se.printStackTrace(); 
      }catch(Exception e){ 
       //Handle errors for Class.forName 
        e.printStackTrace(); 

      } 
      }    
     %> 

回答

0

你的問題似乎是在這裏:

String query = "INSERT INTO JOHNPAUL.CUSTOMER_RESERVATIONS 
    (TABLE_NUM, FIRST_NAME,LAST_NAME,ADDRESS,TELEPHONE, DATE, NUMBER_IN_PARTY) 
    VALUES (table_num, first,last,address,phone,date,people)"; 

這裏包括兩個: 1.您遠離串;和 2.將變量中的值連接到字符串。

String query = "INSERT INTO JOHNPAUL.CUSTOMER_RESERVATIONS 
     (TABLE_NUM, FIRST_NAME,LAST_NAME,ADDRESS,TELEPHONE, DATE, NUMBER_IN_PARTY) 
     VALUES (" + table_num + ", '" + first + "', '" + last + "', '" + address + "', " + phone + " , '" + date + "', " + people + ");"; 

您可能需要驗證數據庫引擎期望的日期字段的格式。

+0

謝謝李。我把它整理好了。現在只是有日期問題。當我刪除jsp代碼中的所有文件時,所有工作都很好,並且記錄被插入到數據庫中。 Bur獲取錯誤信息告訴我 - ''DATE'類型的列在離開所有日期編碼時不能保存'INTEGER'類型的值。 –