2014-10-30 48 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