数据库有个表Config:这个表第一个字段为自动增加式...现在客户那我也不知道已经有多少条数据了...下面这段语句该如何写才能真确插入,插入后第一个字段根据序列自动填充(表已经设置该字段为自动填充)!INSERT INTO `Config` VALUES (, '3', '','0');我用?表示 出错!

解决方案 »

  1.   

    比如:
    数据表已经有5条数据 或者 6条数据  现在运行这段SQL后 的第一个字段为 6 或 7  可以自动填充!现在问题是这段SQL第一个字段值该如何表示?
      

  2.   

    INSERT INTO <表名> [(<字段名1> [,<字段名2>, ...])] VALUES (<常量1> [,<常量2>, ...])
      

  3.   

    在insert 中写出明确的字段名
      

  4.   

    INSERT INTO <表名> [(<字段名1> [,<字段名2>, ...])] VALUES (<常量1> [,<常量2>, ...])如果字段是自动增长型你就可以把它在<表名>后省略就可以了
      

  5.   

    try {
          // Load the database driver
          Class.forName("org.gjt.mm.mysql.Driver");
          // Get a Connection to the database
          connection = DriverManager.getConnection(connectionURL, "root", "admin"); 
          //Add the data into the database
          String sql = "insert into emp_details values (?,?,?,?,?,?,?,?)";
          PreparedStatement pst = connection.prepareStatement(sql);
          pst.setString(1, uId);
          pst.setString(2, fname);
          pst.setString(3, sname);
          pst.setString(4, address1);
          pst.setString(5, address2);
          pst.setString(6, town);
          pst.setString(7, county);
          pst.setString(8, zipcode);
          int numRowsChanged = pst.executeUpdate();
          // show that the new account has been created
          out.println(" Hello : ");
          out.println(" '"+fname+"'");
          pst.close();
        }
        catch(ClassNotFoundException e){
          out.println("Couldn't load database driver: " + e.getMessage());
        }
        catch(SQLException e){
          out.println("SQLException caught: " + e.getMessage());
        }
        catch (Exception e){
          out.println(e);
        }
        finally {
          // Always close the database connection.
          try {
            if (connection != null) connection.close();
          }
          catch (SQLException ignored){
            out.println(ignored);
          }
        }
      }
      

  6.   

    这个问题很简单,有两种方法,第一个就是你问号那个位置设为0,在mysql中,如果你的字段已经设置为自增长,那么添0,它就会设为该字段最大值+1,第二种方法好像你已经知道了
      

  7.   

    设为自增了,就可以了,写个null,都没问题,它应该会自己判断的