到底statement和preparedStatement 的 execute executeUpdate executeQuery 用在什么时候 有什么区别?特别是preparedStatement.
我遇到两个问题都是最后preparedStatement.execute(...);有问题。我检查了决定不是sql语句的问题。也不是我没有导入包的问题,是怎么回事啊?

解决方案 »

  1.   

    把代码和执行的sql语句贴出来,
    出错的报错信息也贴出来看看
      

  2.   

    preparedStatement 区别与statement在于它的缓存机制,如果是最后对数据库的操作,数据有问题,但是程序却能正常运行。你就应该检查你的缓存机制是否用的合适。
      

  3.   

    楼主:
    statement是存语句查询
    preparedStatement是参数查询,参数查询在数据库有缓存,可以提高速度.
    他们的区别很大
    请看
    /**
         * @author longjianhui Date:2005-6-8 11:12:32 tags:@param connStr
         *         tags:@param sqlStr tags:@return Return_Type:ResultSet
         *         Description:获取只读数据集
         */
        public static ResultSet getResultSet(Connection connection, String sqlStr)
                throws Exception {
            ResultSet rs = null;
            try {
                if (UF.SysOutPrintFlag())
                    System.out.println("sql:" + sqlStr);
                Statement stmt = connection.createStatement();
                rs = stmt.executeQuery(sqlStr);
                return rs;
            } catch (SQLException ex) {
                if (rs != null) {
                    rs.close();
                    rs = null;
                }
                throw ex;
            }
        }
    2
     /**
         * 
         * @param connection
         * @param sqlStr
         * @param objArr
         * @return
         * @throws SQLException
         *             Description:根据参数语句获取只读数据集
         */
        public static ResultSet getResultSet(Connection connection, String sqlStr,
                Object[] objArr) throws Exception {
            PreparedStatement ps = null;
            try {
                if (UF.SysOutPrintFlag())
                    System.out.println("sql:" + sqlStr);
                ps = connection.prepareStatement(sqlStr);
                for (int i = 1; i <= objArr.length; i++) {
                    Object obj = objArr[i - 1];
                    ps.setObject(i, objArr[i - 1]);
                }
                return ps.executeQuery();
            } catch (SQLException ex) {
                if (ps != null) {
                    ps.cancel();
                    ps.close();
                    ps = null;
                }
                throw ex;
            }
        }
      

  4.   

    preparedStatement con.prepareStatement(sqlStr); 是预置sql语句  里面可以有可变的参数设置如,ps.setString(...);执行如 ps.executeQuery();
    Statement con.createStatement();  用于固定sql语句 执行的时候如stmt.executeQuery(sqlStr)
      

  5.   

    zeq258:
    你就应该检查你的缓存机制是否用的合适。  你能不能说清楚点点 我不懂什么意思啊
      

  6.   

    //添加商品
    public void addProduct(Product product)
    {
    PreparedStatement pstmt = null;
    try
    {
    pstmt = con.prepareStatement("insert into products values(?,?,?,?,?,?)");
    pstmt.setString(1,product.getProductId());
    pstmt.setString(2,product.getCategoryId());
    pstmt.setString(3,product.getName());
    pstmt.setString(4,product.getProducer());
    pstmt.setFloat(5,product.getPrice());
    pstmt.setString(6,product.getDescription());
    }
    catch(Exception ex)
    {
    ex.printStackTrace();
    }
    try
    {
    pstmt.execute();   //错误就在这句话???为什么?
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
      

  7.   

    采用这个ps.executeQuery();
    并且你要检查你说设置的对象
    和数据库的类型是否一直.