jjzjj

java - 无法添加或更新子行 : a foreign key constraint fails

coder 2023-10-06 原文

有人知道我的代码有什么问题吗?我正在使用一个简单的 INSERT 语句(而不是常规的 PreparedStatement,因为我正在尝试利用 mySQL 的 AES_ENCRYPT/DECRYPT 函数。我不知道如何将它们与 PreparedStatements 的常规设置一起使用。所有时髦的问号。

我不断得到经典:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (video_game_db.login, CONSTRAINT login_ibfk_1 FOREIGN KEY (cust_ID) REFERENCES customer (cust_ID))

当然,这让我很抓狂。

我整晚都在努力使密码“东西”的常规 JDBC 实现正常工作。最后只能加密字符串,不能解密。我认为它必须用 mySQL 而不是 JDBC 做一些事情,因为在我将它们输入数据库之前它们解密得很好,但之后我可以让它工作。


请看下面我的JDBC表DDL和DML语句:

DDL

Statement s = conn.createStatement();
s.executeUpdate("CREATE TABLE customer("+"cust_ID CHAR(10) NOT NULL,"+"PRIMARY KEY(cust_ID),"+"first_Name CHAR(30)NOT NULL,mI CHAR(2),last_Name CHAR(50)NOT NULL,street_Name CHAR(50),city CHAR(30) NOT NULL,state CHAR(50) NOT NULL,"
            +"zip_Code CHAR(5) NOT NULL, home_Phone CHAR(12) UNIQUE, referrer CHAR(30), quantity INTEGER NOT NULL, item_No CHAR(10))"); 
s.executeUpdate("CREATE TABLE login ("+"user_Name CHAR(50) NOT NULL,"+"PRIMARY KEY(user_Name),"+"pass_Word CHAR(50)NOT NULL, cust_ID CHAR(10))");  

DML

public static void setCustTable(String cust_ID, String lName, String fName, String mI, String street_Name, String city, String state,
  String zip_Code, String home_Phone, String referrer, int quantity, String itemNo)throws IOException, SQLException 
{

// connect to database
try
{    

    PreparedStatement stat = conn.prepareStatement("INSERT INTO customer (cust_ID, first_Name, mI, last_Name, street_Name, city, state, zip_Code, home_Phone, referrer, quantity, item_No)"
                + " VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");

    stat.setString(1, cust_ID);
    stat.setString(2, fName);
    stat.setString(3, mI);
    stat.setString(4, lName);
    stat.setString(5, street_Name);
    stat.setString(6, city);
    stat.setString(7, state);
    stat.setString(8, zip_Code);
    stat.setString(9, home_Phone);
    stat.setString(10, referrer);
    stat.setInt(11, quantity);
    stat.setString(12, itemNo);

    stat.executeUpdate();
}  

 catch (SQLException e) 
{
    e.printStackTrace();
    throw e;
}

**********************************************************************************



public static void insertLoginData(String username5, String password5, String custID5)throws IOException, SQLException, NoSuchAlgorithmException, InvalidKeyException

{
    Statement s = conn.createStatement();
    String insert="INSERT INTO login VALUES('username5', AES_ENCRYPT('text','password5'),'custID5')";
    s.executeUpdate(insert);

最佳答案

就其值(value)而言,我没有看到任何插入 customer 的内容。报错提示从登录到有外键约束 顾客。因此客户条目必须在登录插入之前存在。


是否有意引用“username5”?我认为它是方法的参数,但插入使用文字。

关于java - 无法添加或更新子行 : a foreign key constraint fails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6126514/

有关java - 无法添加或更新子行 : a foreign key constraint fails的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  4. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  5. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  6. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  7. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  8. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  9. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  10. ruby-on-rails - 使用 rails 4 设计而不更新用户 - 2

    我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它​​不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数

随机推荐