请教JDBC delete 语句的问题

zcxie2008 2009-05-12 04:59:59

DELETE from roleNperm WHERE roleID=104 AND permCode="2";

其中roleID为Integer类型,permCode为varchar(4)类型
以上语句在MySQL中可以执行,并可以删除对应的行,但在JDBC中,我使用以下语句:


public void delRolePerm(String rolesID, String permCode) {
try {
db.connect();
String sql1 = "delete from rolenperm where roleID="+rolesID + "and permCode=\""+permCode+"\"";
db.stat = db.conn.createStatement();
db.stat.executeUpdate(sql1);

} catch(Exception e) {
e.printStackTrace();
} finally{
db.close();
}
}


出现的错误是:
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'permCode="2"' at line 1

请教为什么错,该如何处理?
想通过两个参数来删除表中的一行,该如何写sql那个String? 谢谢!
...全文
669 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
wefrogll 2009-05-12
  • 打赏
  • 举报
回复
楼上说得对.完全可以打执行前打印一下语句 就知道最后执行的是怎样的SQL了.
zcxie2008 2009-05-12
  • 打赏
  • 举报
回复
谢谢各位的回答
qiheia 2009-05-12
  • 打赏
  • 举报
回复
结贴给分啊
zcxie2008 2009-05-12
  • 打赏
  • 举报
回复

Bingo!

你真细心,我真粗心,哈哈,谢谢你了!
qiheia 2009-05-12
  • 打赏
  • 举报
回复
你是不是roleID="+roleID + "与and之间少了个空格啊??
zcxie2008 2009-05-12
  • 打赏
  • 举报
回复
放到MySQL里可以执行,但在类里就执行不了,看第一帖那里
说我SQL syntax错误,但不清楚错误在哪里,哪位帮帮忙啊
qiheia 2009-05-12
  • 打赏
  • 举报
回复

如果你还有错误的话,把你的sql放到mysql中去运行下,就知道你的sql是不是有错误了。。。
zcxie2008 2009-05-12
  • 打赏
  • 举报
回复
还是不行,使用 String sql1 = "delete from rolenperm where roleID="+roleID + "and permCode='"+permCode+"'";
错误提示:

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'permCode='2'' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1027)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1837)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1961)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2537)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1564)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1485)
at com.struts.model.PermBean.delRolePerm(PermBean.java:116)
at com.struts.model.PermBean.main(PermBean.java:125)
valen_jia 2009-05-12
  • 打赏
  • 举报
回复

String sql1 = "delete from rolenperm where roleID="+rolesID + "and permCode='"+permCode+"'";
valen_jia 2009-05-12
  • 打赏
  • 举报
回复

String sql1 = "delete from rolenperm where roleID="+rolesID + "and permCode='"+permCode+"'";

wangwenhui-com-cn 2009-05-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qiheia 的回复:]
String sql1 = "delete from rolenperm where roleID="+rolesID + "and permCode='"+permCode+"';
这样改,看有效果么
[/Quote]

支持
qiheia 2009-05-12
  • 打赏
  • 举报
回复
String sql1 = "delete from rolenperm where roleID="+rolesID + "and permCode='"+permCode+"'";
zhangpeixv 2009-05-12
  • 打赏
  • 举报
回复
permCode=\""+permCode+"\"";

这里改成permCode='"+permCode+"'"呢
cjdxhc 2009-05-12
  • 打赏
  • 举报
回复
字符串要加''间隔开!
dsamsofter 2009-05-12
  • 打赏
  • 举报
回复
这样写试试
String sql1 = "delete from rolenperm where roleID="+rolesID + "and permCode="'+permCode+'"";
chenchuanfeng001 2009-05-12
  • 打赏
  • 举报
回复
String sql1 = "delete from rolenperm where roleID= '"+rolesID + "' and permCode= '"+permCode+"'";
qiheia 2009-05-12
  • 打赏
  • 举报
回复
String sql1 = "delete from rolenperm where roleID="+rolesID + "and permCode='"+permCode+"';
这样改,看有效果么

81,090

社区成员

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

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