将预留插入数据库

时间:2016-10-29 17:23:33

标签: mysql database

这让我发疯,我确信这很简单。我得到的值必须包含至少一个元素'当我尝试从出现的表中输入预订时,服务器出错。一切正常。无论我在VALUES部分使用引号还是在分隔逗号上加上(+)符号或引号,我都会收到不同的错误消息。当我在table_num上添加引号时,我得到并且错误告诉我您无法将CHAR插入INTEGER。当我删除引号时,我得到错误告诉我 - 严重:java.sql.SQLSyntaxErrorException:Column' 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(); 

            }
            }            
         %>

1 个答案:

答案 0 :(得分: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)";

这里有两件事:  逃避你的弦乐;和  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 + ");";

您可能必须验证数据库引擎期望日期字段的格式。