日期:2014-05-18  浏览次数:20858 次

一个关于在servlet中向数据库插入新数据的问题
今天我在往一个数据库里面插入数据的时候用了以下语句,我想问下这个句子有错误么。
INSERT   INTO   products(SN,name,company,image,intro,price,type)VALUES( "sn3 ", "samsang ", "china ", "/images/some.jpg ", "as   good   as ", "12 ", "mobile ")


当我运行的时候,eclipse报错java.sql.SQLException:   [Microsoft][SQLServer   2000   Driver   for   JDBC][SQLServer]在此上下文中不允许使用   'sn3 '。此处只允许使用常量、表达式或变量。不允许使用列名。SN列中也无sn3数据。。。


原始代码如下
import   java.io.IOException;
import   java.io.PrintWriter;
import   java.sql.*;
import   javax.servlet.ServletException;
import   javax.servlet.http.HttpServlet;
import   javax.servlet.http.HttpServletRequest;
import   javax.servlet.http.HttpServletResponse;
import   com.pk.jsp.beans.admin.*;


public   class   pdtDB   extends   HttpServlet   {

  public   pdtDB()   {
    super();
  }

  public   void   doPost(HttpServletRequest   request,   HttpServletResponse   response)
      throws   ServletException,   IOException   {
   
   
    pdtValidate.setName(request.getParameter( "pdtName "));
    pdtValidate.setCompany(request.getParameter( "pdtCompany "));
    pdtValidate.setImage(request.getParameter( "pdtImage "));
    pdtValidate.setIntro(request.getParameter( "pdtIntro "));
    pdtValidate.setPrice(request.getParameter( "pdtPrice "));
    pdtValidate.setType(request.getParameter( "pdtType "));
   
    try{
     
      ResultSet   rs   =   statm.executeQuery( "SELECT   *   FROM   products ");
     
      while(rs.next()){
        rows++;
      }
      pdtSN   =   "sn "+(rows+1);
     
      if(pdtValid(pdtValidate)){
        statm.executeUpdate( "INSERT   INTO   products(SN,name "   +
            ",company,image,intro,price,type) "   +
            "VALUES(\ " "+pdtSN+ "\ ",\ " "+pdtValidate.getName()+ "\ ",\ " "+pdtValidate.getCompany()+
            "\ ",\ " "+pdtValidate.getImage()+ "\ ",\ " "+pdtValidate.getIntro()+
            "\ ",\ " "+pdtValidate.getPrice()+ "\ ",\ " "+pdtValidate.getType()+ "\ ") ");
      }
     
    }catch(SQLException   sqle){