您好我是Java Web开发的新手,我正在尝试使用数据访问对象通过表单将数据传递到数据库。
这是我目前创建的数据插入方法:
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
我很确定案例是addCustomer
方法,但我无法从谷歌找到解决方案,也许这里有人可以帮助我吗?
答案 0 :(得分:1)
insert语句似乎是错误的,因为你得到了SQLSyntaxErrorException:
st.executeUpdate("insert into customer value(
试试这个:
st.executeUpdate("insert into customer values (
另外,请记住VALUES子句上的顺序应与您在customer表上创建的顺序相同。另外,您必须使用完整的INSERT语法:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);