我在数据库中建立了一个User表,想向其中添加数据,所有代码如下:
try{
            String sql="INSERT INTO User VALUES(?,?)";
            PreparedStatement ps=con.prepareStatement(sql);
            ps.setString(1,userName);
            ps.setString(2,passWard);
           
            ps.executeUpdate();
            con.close();
            JOptionPane.showMessageDialog(this,"添加成功!");
            //clearForm();
}
catch(SQLException e){
System.out.println("E Code"+e.getErrorCode());
System.out.println("E M="+e.getMessage());
}
但出现了以下错误,请指教。
E Code156
E M=[Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]在关键字 'User' 附近有语法错误。

解决方案 »

  1.   

    你还是把Exception按层打印出来吧,那样看的清楚些,还有你表的结构是什么样的。
      

  2.   

    只有两个字段吗?如果不只两个字段,就要显式的说明要插入的只有这两个字段。
     String sql="INSERT INTO User(username,password) VALUES(?,?)";
      

  3.   

    sql有问题?
    表名是否正确?user还是User?
    表中字段有多少? INSERT INTO User(name,pass) VALUES(?,?)打印异常,获取更多信息
    catch(SQLException e){
                    System.out.println("E Code"+e.getErrorCode());
                    System.out.println("E M="+e.getMessage());
    e.printStackTrace();
                }
      

  4.   

    你的user表中只有两个字段么?
    要不是的话,应该这么写:
    String sql="INSERT INTO User(field1,field2) VALUES(?,?)";
      

  5.   

    这个表是只有两个字段的,换成这种形式还是不行,老是出现这样的错误,我换了用户名还是一样。
    java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]对象名 'Users' 无效。
    at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
    at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
    at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
    at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
    at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
    at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
    at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
    at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
    at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
    at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
    at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
    at com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown Source)
    at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)
    at UserAdd.addFI(UserAdd.java:83)
    at UserAdd.actionPerformed(UserAdd.java:60)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6216)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
    at java.awt.Component.processEvent(Component.java:5981)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4583)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4413)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2475)
    at java.awt.Component.dispatchEvent(Component.java:4413)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
      

  6.   

    谁叫你多打个s的??对象名 'Users' 无效
      

  7.   

    数据库重新建立一个用户share ,建立过程如下:在storeManager数据库中选重用户 ---〉新建用户 -- 〉名称选择(这一步中有两个关键点 1、身份验证选SQL身份验证,默认数据库选StoreManager)-〉建立新教色share ,此时更改程序,将用户登陆名和密码修改一下。
      

  8.   

    有人说在数据库中最好不用User,所以我改用Users,在数据库中我也改了表名的……
      

  9.   

    PreparedStatement ps=con.prepareStatement(sql);
                    ps.setString(1,userName);
                    ps.setString(2,passWard);set是重0开始把