价值在到达时发生变化。 |可调用语句
我在使用 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
。我做了以下事情:
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:
id_color
value to the create method, still got replaced by a 3如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
存储过程声明有错误:
将其更改为:
希望它能起作用。
There is a mistake in stored procedure declaration:
change it to:
Hopefully it will work.
给定属性定义
shape tinyint(3)
和id_color tinyint(3)
,我很惊讶地看到PROCEDURE putCover()
具有形式参数 <分别为 code>IN 形状 TINYINT 和IN 颜色 TINYINT(3)
。此外,我希望create()
相应地使用setByte()
。Given the atribute definitions
shape tinyint(3)
andid_color tinyint(3)
, I am surprised to seePROCEDURE putCover()
having formal parametersIN shape TINYINT
andIN color TINYINT(3)
, respectively. Moreover, I'd expectcreate()
to usesetByte()
, accordingly.