insert into user values(?,?)
它说user表可能不止两个字段,如果你想忽略一些字段的插入
可以这样,其实这样的写法是更好的,否则以后一旦增加字段了,你的代码就必须重写
insert into user(ID,Name) values(?,?)

解决方案 »

  1.   

    哦!谢谢!^-^
    我数据库里是有3个字段,还有一个自动编号的ID
    当我插入数据库的时候,又出这种错误!javax.servlet.ServletException: Syntax error or access violation: You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'user(UserID,UserName) values('1','2')' at line 1
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
    org.apache.jsp.jsp.adduser_005fdo_jsp._jspService(adduser_005fdo_jsp.java:82)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    root cause java.sql.SQLException: Syntax error or access violation: You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'user(UserID,UserName) values('1','2')' at line 1
    org.gjt.mm.mysql.MysqlIO.sendCommand(MysqlIO.java:497)
    org.gjt.mm.mysql.MysqlIO.sqlQueryDirect(MysqlIO.java:550)
    org.gjt.mm.mysql.Connection.execSQL(Connection.java:885)
    org.gjt.mm.mysql.PreparedStatement.execute(PreparedStatement.java:1093)
    database.UserBean.addUser(UserBean.java:44)
    org.apache.jsp.jsp.adduser_005fdo_jsp._jspService(adduser_005fdo_jsp.java:75)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    我怀疑还是这里的问题,请达人指点!
    public void addUser(User user)throws Exception
    {

    PreparedStatement pstmt=con.prepareStatement("insert into user(UserID,UserName) values(?,?)");
    pstmt.setString(1,user.getUserID());
    pstmt.setString(2,user.getUserName());
    pstmt.execute();

    }
      

  2.   

    楼上说的对阿,从你的bean看你的表中还有一个自动编号字段,改改插入的sql应该就可以了。
      

  3.   

    我回帖的时候还没有看到你的回复。
    insert into user (id,userid,userName)  values(,'1','hello');
    到sql命令中执行下
      

  4.   

    To: umljsp(夜未央天未白) 
    这样不行!在msyql下出错Database changed
    mysql> insert int uer (id,userid,username) values(,'1','hello');
    ERROR 1064: You have an error in your SQL syntax.  Check the manual that corresp
    onds to your MySQL server version for the right syntax to use near 'int uer (id,
    userid,username) values(,'1','hello')' at line 1
    mysql> e
      

  5.   

    哈哈,不好意思,一时疏忽阿
    insert into user (id,userid,userName)  values('','1','hello');
    就可以了
      

  6.   

    可是我要写到Bean里啊,要用这个写的!这里的sql语句应该怎么写呢?是不是这里的错误?
    public void addUser(User user)throws Exception
    {

    PreparedStatement pstmt=con.prepareStatement("insert into user(UserID,UserName) values(?,?)");
    pstmt.setString(1,user.getUserID());
    pstmt.setString(2,user.getUserName());
    pstmt.execute();

    }
      

  7.   

    首先在mysql中确定你的插入sql能不能执行的通,如果不通报什么错误,你的sql本身应该没有错误
    注意你的userid的数据类型。如果是Int就不要用setString
      

  8.   

    我的表的结构是这样的
    ID int(20) 自动编号
    UserID varchar(24)
    UserName varchar(24)我在sql下插入是没有问题的,我现在怀疑两个问题1、我的字段里有一个自动编号,这个是不用插入的,也就说我在插入数据的时候不用考虑这个字段!但这个我在Bean里应该怎么写
    2、在插入数据的时候,先接收的数据放到User的Bean里,如:User.setUserID(request.getParameter("userid"));然后我在添加数据的时候这样操作,UserAdmin.addUser(User);使用addUser方法.
    小弟第一次封装这些操作,不知道我的想法具体行不行!