2014-10-30 47 views
0

嗨,我是新的Java Web开发,即时尝试通过数据通过表单传递数据到数据库,使用数据访问对象。使用DAO模式插入数据到hsqldb

继承人插入数据的方法从来就创建至今:

public void addCustomer(String firstname, String lastName, String code) 
{ 
    try 
    { 
     st = getConnection().createStatement(); 
     st.executeUpdate("insert into customer value(id ," + firstname + "," + lastName + "," + code+")"); 

    } catch(Exception e) 
    { 

     throw new RuntimeException(e); 
    } finally { 

     closeResources(); //method which closes connection, resultset and statements 
    } 

} 

现在即时调用Servlet的这个方法看起来像这样:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    response.setContentType("text/html;charset=utf-8"); 
    PrintWriter out = response.getWriter(); 
    out.println(
      "<form action='/Add' method='post'>" 
      +"Eesnimi:<input id='firstNameBox' name='firstName'/> " 
      +"Perekonnanim:<input id='lastNameBox' name='lastName'/>"   
      +"Kood:<input id='codeBox' name='code'/>"  
      +"<input type='submit' value='Lisa'/>" 
      ); 
    String firstname = request.getParameter("firstName"); 
    String lastName = request.getParameter("lastName"); 
    String code = request.getParameter("code"); 
    CustomerDao dao = new CustomerDao(); 
    dao.addCustomer(firstname, lastName, code); 
} 

现在即时得到SQLSyntaxErrorException : user lacks privilege or object not found: NULL Im相当确保案例是在addCustomer方法,但我不能从谷歌找到解决方案,也许这里的任何人都可以帮助我?

回答

1

insert语句的语法似乎是错误的,因为你有上SQLSyntaxErrorException:

st.executeUpdate("insert into customer value(

试试这个:

st.executeUpdate("insert into customer values (

另外请记住,在VALUES子句的顺序应该是相同的顺序你在客户桌上创建的。 Otherwhise,你必须使用完整的INSERT语法:

INSERT INTO table_name (column1,column2,column3,...) 
VALUES (value1,value2,value3,...); 
+0

谢谢,但现在即时获取行和列计数不匹配错误。 Sql语法是否允许将变量添加到语句中? – 2014-10-30 17:21:33

+0

嗯,我在你的SQL语句中看到“id”不是来自你的java代码的值。它是自动增量字段吗?如果是,我认为你可以从价值条款中省略。 – dansouza 2014-10-31 19:41:21