我试图从html表单获取输入并使用JDBC,AngularJS将其保存到mySql

时间:2015-10-30 18:56:40

标签: java mysql angularjs

这里我从html表单获取输入并使用JDBC连接器将其保存到mySql。另外我不确定,我将如何保存日期

  

错误1:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:   您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近VALUES(' a',' a@b.com',' abcd',' 2015-10-30')&#39 ;在第1行

     

错误2:restaurant.my.exceptions.AppException:添加时出错   客户到数据库

这是我的DAO课程:

public Customer addPerson(Customer customer) throws AppException {


    //jdbc resource
    Connection con = DBUtil.connectToDB();
    PreparedStatement ps = null;
    ResultSet rs=null;


    try {
            //getting info from front end and setting it into database
            ps =con.prepareStatement("INSERT INTO customer(First_Last_Name, EMAIL, PASSWORD, DATE_RESERVATION), VALUES(?,?,?,?)", PreparedStatement.RETURN_GENERATED_KEYS);
            ps.setString(1,customer.getFull_Name());
            ps.setString(2,customer.getEmail());
            ps.setString(3,customer.getPassword());
            //ps.setString(4,customer.getFull_Name());

            java.util.Date utilDate=customer.getDate_Reservation();
            //@SuppressWarnings("deprecation")
            java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
            ps.setDate(4,sqlDate);

//              
//          double uDate=new java.util.Date();
//          uDate=customer.getDate_Reservation();
//          java.sql.Date sDate = convertUtilToSql(uDate);
//          DateFormat df = new SimpleDateFormat("dd/MM/YYYY - hh:mm:ss");
//          ps.setDate(4,df.format(uDate));
            ps.executeUpdate();
            //geting generated key
            rs= ps.getGeneratedKeys();

            if(rs.next()){
                customer.setId(rs.getInt(1));

            }

            return customer;    


    } catch (SQLException e) {

        e.printStackTrace();
        throw new AppException("Error in adding customer to the database", e.getCause());
    }finally{
        DBUtil.closeResources(ps, rs, con);
    }



}

这是我的控制器:

@POST
@Path("/add")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public AppResponse addCustomer(Customer customer){


    AppResponse resp = new AppResponse();
    // generating the common msg for successful/ unsuccessful connection
    try {
        //if successful show list
        CustomerDAO customerDAO=new CustomerDAO();
        customer=customerDAO.addPerson(customer);

        resp.setMessage("Customer has been added to the system");
        resp.setPayload(customer);

    } catch (AppException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        resp.setStatus(AppResponse.ERROR);
        resp.setMessage(e.getMessage());
    }

    //customerDAO.getAll();
    return resp;


}

这是我的AngularJS代码:

mctrl.addCustomer= function(){                          
        $http({                 
                method: 'POST',
                url: 'api/customer/add',
                data: mctrl.newCustomer
            }).success(function(data){
                console.log(data);
                mctrl.newCustomer=null;
            }).error(function(error){           
                console.log(error);                         
            });
        };

0 个答案:

没有答案