在调试时如何打印出一个完整的SQL语句?

akangcm 2002-12-10 12:14:57
1.问题1
我在写sql语句时,用?代替一系列变量,然后用PreparedStatement 中的方法
分别给?对应的变量赋值,我想知道在System.out.println(strSql)时
如何打印出来一个给?赋值后的完整的SQL语句?
2.问题2
用PreparedStatement 和用Statement 有何区别?

实例如下:
PreparedStatement ps=null;
String strSql="";

strSql="insert into jia_purch(hp_bianh,hp_mingc,hp_guig,hp_gouhjg,hp_gouhsl) "
+" values(?,?,?,?,?)";
ps=con.prepareStatement(strSql);

ps.setString(1,purchDO.getHPbianh()) ; //货品编号
ps.setString(2,purchDO.getHPmingc()) ; //货品名称
ps.setString(3,purchDO.getHPguig()) ; //货品规格
ps.setDouble(8,purchDO.getHPgouhjg()); //购货价格
ps.setInt(9,purchDO.getHPgouhsl()); //购货数量

ps.executeUpdate();
在执行上面的语句时,出错了,我想将给变量?赋值后的SQL语句打印出来,
应该如何处理呀?
...全文
725 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Andrawu 2002-12-16
  • 打赏
  • 举报
回复
strSql="insert into jia_purch(hp_bianh,hp_mingc,hp_guig,hp_gouhjg,hp_gouhsl) "
+" values(?,?,?,?,?)";

是打印不出?所代表的值的。
liucm 2002-12-16
  • 打赏
  • 举报
回复
PreparedStatemen是Statement的一个子类,Statement不需要传参数,一般的情况下都用此方法。PreparedStatemen必须传sql参数,具有ps.setString(),ps.setInt()等方法,如果同一个sql语句被多次执行时,此方法比较好,但此方法有一个弱点就是你所说的打印不出传过参数后的一个带参数植的sql语句
凋零的老树 2002-12-12
  • 打赏
  • 举报
回复
把变量直接写在SQL语句里有什么不好的?我估计大部分人跟我一样写在SQL语句里,所以回答你的少,建议你也改
akangcm 2002-12-12
  • 打赏
  • 举报
回复
后2个参数不应该是8和9,应该是4和5。但是我还是打印不出来赋值后的sql语句来。
akangcm 2002-12-11
  • 打赏
  • 举报
回复
为何如此少的人回复呀!
akangcm 2002-12-10
  • 打赏
  • 举报
回复
在调试时如何打印出一个完整的SQL语句?

1.问题1
我在写sql语句时,用?代替一系列变量,然后用PreparedStatement 中的方法分别给?对应的变量赋值,
我想知道在System.out.println(strSql)时,如何打印出来一个给?赋值后的完整的SQL语句?
2.问题2
用PreparedStatement 和用Statement 有何区别?
实例如下:
PreparedStatement ps=null;
String strSql="";

strSql="insert into jia_purch(hp_bianh,hp_mingc,hp_guig,hp_gouhjg,hp_gouhsl) "
+" values(?,?,?,?,?)";
ps=con.prepareStatement(strSql);

ps.setString(1,purchDO.getHPbianh()) ; //货品编号
ps.setString(2,purchDO.getHPmingc()) ; //货品名称
ps.setString(3,purchDO.getHPguig()) ; //货品规格
ps.setDouble(8,purchDO.getHPgouhjg()); //购货价格
ps.setInt(9,purchDO.getHPgouhsl()); //购货数量

ps.executeUpdate();
在执行上面的语句时,出错了,我想将给变量?赋值后的SQL语句打印出来,
应该如何处理呀?
zxhong 2002-12-10
  • 打赏
  • 举报
回复
以上的代码是正确的。
出错的原因可能在get()方法上以及字段类型上
zeeler 2002-12-10
  • 打赏
  • 举报
回复
在ps.executeUpdate();前面打印strSql,把ps.executeUpdate()注释掉或在打印后结束进程
jmars 2002-12-10
  • 打赏
  • 举报
回复
除非不得已,一般不要用prepareStatement,好多书上有教.
利而不害 2002-12-10
  • 打赏
  • 举报
回复
ps.setString(1,purchDO.getHPbianh()) ; //货品编号
ps.setString(2,purchDO.getHPmingc()) ; //货品名称
ps.setString(3,purchDO.getHPguig()) ; //货品规格
ps.setDouble(8,purchDO.getHPgouhjg()); //购货价格
ps.setInt(9,purchDO.getHPgouhsl()); //购货数量
为什么后两个的参数是8和9,你有这么多?号吗?
akangcm 2002-12-10
  • 打赏
  • 举报
回复
为什么如此少的人回应呀!

81,116

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧