【问题标题】:jdbc mysql executeUpdate not workingjdbc mysql执行更新不起作用
【发布时间】:2018-06-29 10:01:18
【问题描述】:
PreparedStatement  stmt1 = con.prepareCall("update khol SET emailcnf =  'ya' where khol.id = 
     (SELECT guiid.id FROM guiid WHERE guiid.gchbi = 'htg1' and guiid.typ = 'reg')");

System.out.println("rows COUNT "+stmt1.executeUpdate());

stmt1.close();

输出是:

行数 0

executeUpdate 返回 0,当我直接在 Mysql 中运行相同的语句时,它工作正常。

【问题讨论】:

  • 使用con.prepareStatement 而不是con.prepareCall
  • 它不起作用,我已经尝试过使用 con.prepareStatement
  • 你确定你连接到同一个数据库吗?
  • 是和其他单语句,如插入、删除、更新工作正常,但是当我尝试在 jdbc 中使用 select 语句运行更新但它不起作用
  • Aborted connection 6 to db: 'atg' user: 'root' host: 'localhost' (读取通讯包时出错) Aborted connection 7 to db: 'atg' user: 'root' host :'localhost'(读取通信数据包时出错)中止连接 8 到 db:'atg'用户:'root'主机:'localhost'(读取通信数据包时出错)中止连接 10 到 db:'atg'用户: “root”主机:“localhost”(读取通信数据包时出错)这是我的 MYSQL 日志中的最后一行

标签: java mysql jdbc


【解决方案1】:

您应该使用con.prepareStatement() 而不是con.prepareCall()

查看以下链接,帮助您了解createStatement() prepareSatement()prepareCall() 之间的区别

http://javaconceptoftheday.com/statement-vs-preparedstatement-vs-callablestatement-in-java/

以下应该有效:

PreparedStatement  stmt1 = con.prepareStatement("update khol SET emailcnf =  'ya' where khol.id = 
            (SELECT guiid.id FROM guiid WHERE guiid.gchbi = 'htg1' and guiid.typ = 'reg')");

System.out.println("rows COUNT "+stmt1.executeUpdate());

stmt1.close();

【讨论】:

  • 它不起作用,我已经尝试过使用 con.prepareStatement
  • 在您的具体示例中,您实际上可以使用createStatement(),但一旦您开始添加动态参数,您就需要确保使用准备好的语句以避免引入 SQL 注入漏洞。
  • 显示您的con 作业。
  • 我正在使用存储过程,但在这里我刚刚粘贴了副本语句,我尽可能多地尝试在 jdbc 中使用 select 语句运行更新,但它不起作用
  • if (con == null) { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/atg", "root", "root"); }
猜你喜欢
  • 2017-12-28
  • 1970-01-01
  • 2014-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多