价值在到达时发生变化。 |可调用语句

发布于 2024-10-09 16:09:21 字数 5157 浏览 4 评论 0原文

我在使用 DAO 类和 StoredProcedure 时遇到了一个奇怪的问题,发生的情况是我使用了一个 CallableStatement 对象,该对象需要 15 个 IN 参数,即字段 的值>id_color 可以从 HTML 表单中正确检索,甚至可以在 CallableStatement setter 方法中进行设置,但是当它发送到数据库时,id_color code> 被值 3 覆盖,这是“上下文”: 我有以下类 DAO.CoverDAO ,它处理该表的 CRUD 操作


CREATE TABLE `cover_details` (
  `refno` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `shape` tinyint(3) unsigned NOT NULL ,
  `id_color` tinyint(3) unsigned NOT NULL ',
  `reversefold` bit(1) NOT NULL DEFAULT b'0' ,
  `x` decimal(6,3) unsigned NOT NULL ,
  `y` decimal(6,3) unsigned NOT NULL DEFAULT '0.000',
  `typecut` varchar(10) NOT NULL,
  `cornershape` varchar(20) NOT NULL,
  `z` decimal(6,3) unsigned DEFAULT '0.000' ,
  `othercornerradius` decimal(6,3) unsigned DEFAULT '0.000'',
  `skirt` decimal(5,3) unsigned NOT NULL DEFAULT '7.000',
  `foamTaper` varchar(3) NOT NULL,
  `foamDensity` decimal(2,1) unsigned NOT NULL ,
  `straplocation` char(1) NOT NULL ',
  `straplength` decimal(6,3) unsigned NOT NULL,
  `strapinset` decimal(6,3) unsigned NOT NULL,
  `spayear` varchar(20) DEFAULT 'Not Specified',
  `spamake` varchar(20) DEFAULT 'Not Specified',
  `spabrand` varchar(20) DEFAULT 'Not Specified',
  PRIMARY KEY (`refno`)
) ENGINE=MyISAM AUTO_INCREMENT=143 DEFAULT CHARSET=latin1 $$

插入封面的方式是通过存储过程,如下所示


CREATE DEFINER=`root`@`%` PROCEDURE `putCover`(
                                    IN shape TINYINT,
                                    IN color TINYINT,
                                    IN reverse_fold BIT,
                                    IN x DECIMAL(6,3), 
                                    IN y DECIMAL(6,3),
                                    IN type_cut VARCHAR(10),
                                    IN corner_shape VARCHAR(10),
                                    IN cutsize DECIMAL(6,3),
                                    IN corner_radius DECIMAL(6,3),
                                    IN skirt DECIMAL(5,3),
                                    IN foam_taper VARCHAR(7),
                                    IN foam_density DECIMAL(2,1),
                                    IN strap_location CHAR(1),
                                    IN strap_length DECIMAL(6,3),
                                    IN strap_inset DECIMAL(6,3)
                                    )
BEGIN
    INSERT INTO `dbre`.`cover_details` 
    (`dbre`.`cover_details`.`shape`,
     `dbre`.`cover_details`.`id_color`,
     `dbre`.`cover_details`.`reversefold`,
     `dbre`.`cover_details`.`x`,
     `dbre`.`cover_details`.`y`,
     `dbre`.`cover_details`.`typecut`,
     `dbre`.`cover_details`.`cornershape`,
     `dbre`.`cover_details`.`z`,
     `dbre`.`cover_details`.`othercornerradius`,
     `dbre`.`cover_details`.`skirt`,
     `dbre`.`cover_details`.`foamTaper`,
     `dbre`.`cover_details`.`foamDensity`,
     `dbre`.`cover_details`.`strapLocation`,
     `dbre`.`cover_details`.`strapInset`,
     `dbre`.`cover_details`.`strapLength`
     )
    VALUES
    (shape,color,reverse_fold,
     x,y,type_cut,corner_shape,
     cutsize,corner_radius,skirt,foam_taper,foam_density,
     strap_location,strap_inset,strap_length);
END

:可以看到基本上它只是填充每个字段,现在,创建封面的 CoverDAO.create(CoverDTO cover) 方法如下所示:


public void create(CoverDTO cover) throws DAOException {
        Connection link = null;
        CallableStatement query = null;
        try {
            link = MySQL.getConnection();
            link.setAutoCommit(false);
            query = link.prepareCall(
                "{CALL putCover(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}"
                                    );
            query.setByte(1,cover.getShape().byteValue());
            query.setByte(2,cover.getColor().byteValue());
            query.setBoolean(3, cover.getReverseFold());
            query.setBigDecimal(4,cover.getX());
            query.setBigDecimal(5,cover.getY());
            query.setString(6,cover.getTypeCut());
            query.setString(7,cover.getCornerShape());
            query.setBigDecimal(8, cover.getZ());
            query.setBigDecimal(9, cover.getCornerRadius());
            query.setBigDecimal(10, cover.getSkirt());
            query.setString(11, cover.getFoamTaper());
            query.setBigDecimal(12, cover.getFoamDensity());
            query.setString(13, cover.getStrapLocation());
            query.setBigDecimal(14, cover.getStrapLength());
            query.setBigDecimal(15, cover.getStrapInset());
            query.executeUpdate();
            link.commit();
        } catch (SQLException e) {
            throw new DAOException(e);
        } finally {
            close(link, query);
        }
    }

CoverDTO 由访问器方法组成,MySQL 对象基本上返回连接从水池中。

这是带有虚拟但适当数据的 pset 查询: putCover(1,10,0,80.0,80.0,'F','Cut',0.0,0,15.0,'4x2',1.5,'A',10.0,5.0)(删除了一些尾随零) 正如您所看到的,当我写入数据库而不是第二个参数中的 10 时,一切都很好,写入了 3 。我做了以下事情:

  • 跟踪创建方法的 id_color 值,仍然被 3 替换
  • 在 DAO 创建方法中硬编码该值,仍然被 3 替换 从
  • MySQL Workbench 调用该过程,它工作得很好,所以我假设创建方法中发生了一些事情,我们非常感谢任何帮助。
  • I having a weird problem with a DAO class and a StoredProcedure, what is happening is that I use a CallableStatement object which takes 15 IN parameters, the value of the field id_color is retrieved correctly from the HTML forms it even is set up how it should in the CallableStatement setter methods, but the moment it is sent to the database the id_color is overwriten by the value 3 here's the "context":
    I have the following class DAO.CoverDAO which handles the CRUD operations of this table

    
    CREATE TABLE `cover_details` (
      `refno` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `shape` tinyint(3) unsigned NOT NULL ,
      `id_color` tinyint(3) unsigned NOT NULL ',
      `reversefold` bit(1) NOT NULL DEFAULT b'0' ,
      `x` decimal(6,3) unsigned NOT NULL ,
      `y` decimal(6,3) unsigned NOT NULL DEFAULT '0.000',
      `typecut` varchar(10) NOT NULL,
      `cornershape` varchar(20) NOT NULL,
      `z` decimal(6,3) unsigned DEFAULT '0.000' ,
      `othercornerradius` decimal(6,3) unsigned DEFAULT '0.000'',
      `skirt` decimal(5,3) unsigned NOT NULL DEFAULT '7.000',
      `foamTaper` varchar(3) NOT NULL,
      `foamDensity` decimal(2,1) unsigned NOT NULL ,
      `straplocation` char(1) NOT NULL ',
      `straplength` decimal(6,3) unsigned NOT NULL,
      `strapinset` decimal(6,3) unsigned NOT NULL,
      `spayear` varchar(20) DEFAULT 'Not Specified',
      `spamake` varchar(20) DEFAULT 'Not Specified',
      `spabrand` varchar(20) DEFAULT 'Not Specified',
      PRIMARY KEY (`refno`)
    ) ENGINE=MyISAM AUTO_INCREMENT=143 DEFAULT CHARSET=latin1 $
    
    

    The the way covers are being inserted is by a stored procedure, which is the following:

    
    CREATE DEFINER=`root`@`%` PROCEDURE `putCover`(
                                        IN shape TINYINT,
                                        IN color TINYINT,
                                        IN reverse_fold BIT,
                                        IN x DECIMAL(6,3), 
                                        IN y DECIMAL(6,3),
                                        IN type_cut VARCHAR(10),
                                        IN corner_shape VARCHAR(10),
                                        IN cutsize DECIMAL(6,3),
                                        IN corner_radius DECIMAL(6,3),
                                        IN skirt DECIMAL(5,3),
                                        IN foam_taper VARCHAR(7),
                                        IN foam_density DECIMAL(2,1),
                                        IN strap_location CHAR(1),
                                        IN strap_length DECIMAL(6,3),
                                        IN strap_inset DECIMAL(6,3)
                                        )
    BEGIN
        INSERT INTO `dbre`.`cover_details` 
        (`dbre`.`cover_details`.`shape`,
         `dbre`.`cover_details`.`id_color`,
         `dbre`.`cover_details`.`reversefold`,
         `dbre`.`cover_details`.`x`,
         `dbre`.`cover_details`.`y`,
         `dbre`.`cover_details`.`typecut`,
         `dbre`.`cover_details`.`cornershape`,
         `dbre`.`cover_details`.`z`,
         `dbre`.`cover_details`.`othercornerradius`,
         `dbre`.`cover_details`.`skirt`,
         `dbre`.`cover_details`.`foamTaper`,
         `dbre`.`cover_details`.`foamDensity`,
         `dbre`.`cover_details`.`strapLocation`,
         `dbre`.`cover_details`.`strapInset`,
         `dbre`.`cover_details`.`strapLength`
         )
        VALUES
        (shape,color,reverse_fold,
         x,y,type_cut,corner_shape,
         cutsize,corner_radius,skirt,foam_taper,foam_density,
         strap_location,strap_inset,strap_length);
    END
    

    As you can see basically it just fills each field, now, the CoverDAO.create(CoverDTO cover) method which creates the cover is like so:

    
    public void create(CoverDTO cover) throws DAOException {
            Connection link = null;
            CallableStatement query = null;
            try {
                link = MySQL.getConnection();
                link.setAutoCommit(false);
                query = link.prepareCall(
                    "{CALL putCover(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}"
                                        );
                query.setByte(1,cover.getShape().byteValue());
                query.setByte(2,cover.getColor().byteValue());
                query.setBoolean(3, cover.getReverseFold());
                query.setBigDecimal(4,cover.getX());
                query.setBigDecimal(5,cover.getY());
                query.setString(6,cover.getTypeCut());
                query.setString(7,cover.getCornerShape());
                query.setBigDecimal(8, cover.getZ());
                query.setBigDecimal(9, cover.getCornerRadius());
                query.setBigDecimal(10, cover.getSkirt());
                query.setString(11, cover.getFoamTaper());
                query.setBigDecimal(12, cover.getFoamDensity());
                query.setString(13, cover.getStrapLocation());
                query.setBigDecimal(14, cover.getStrapLength());
                query.setBigDecimal(15, cover.getStrapInset());
                query.executeUpdate();
                link.commit();
            } catch (SQLException e) {
                throw new DAOException(e);
            } finally {
                close(link, query);
            }
        }

    The CoverDTO is made of accessesor methods, the MySQL object basically returns the connection from a pool.

    Here is the pset Query with dummy but appropriate data:
    putCover(1,10,0,80.0,80.0,'F','Cut',0.0,0,15.0,'4x2',1.5,'A',10.0,5.0) (removed some trailing zeros)
    As you can see everything is fine just when I write to the DB instead of 10 in the second parameter a 3 is written. I have done the following:

  • Traced the id_color value to the create method, still got replaced by a 3
  • Hardcoded the value in the DAO create method, still got replaced by a 3
  • Called the procedure from the MySQL Workbench, it worked fined so I assume something is happening in the create method, any help is really appreciated.
  • 如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(2

    嗳卜坏 2024-10-16 16:09:21

    存储过程声明有错误:

    IN color TINYINT(3)
    

    将其更改为:

    IN color TINYINT
    

    希望它能起作用。

    There is a mistake in stored procedure declaration:

    IN color TINYINT(3)
    

    change it to:

    IN color TINYINT
    

    Hopefully it will work.

    紫南 2024-10-16 16:09:21

    给定属性定义 shape tinyint(3)id_color tinyint(3),我很惊讶地看到 PROCEDURE putCover() 具有形式参数 <分别为 code>IN 形状 TINYINT 和 IN 颜色 TINYINT(3)。此外,我希望 create() 相应地使用 setByte()

    Given the atribute definitions shape tinyint(3) and id_color tinyint(3), I am surprised to see PROCEDURE putCover() having formal parameters IN shape TINYINT and IN color TINYINT(3), respectively. Moreover, I'd expect create() to use setByte(), accordingly.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文