Results 1 to 8 of 8
  1. #1
    axenos is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default jdbc foreign keys problem

    Hello,

    this is my code for an array in a database with 4 other arrays. C2J includes 3 fields: its id (primary key), idJava, idComponent...idJava and idComponent is from another 2 arrays from the DB.

    public C2J(Statement statement) throws SQLException{

    int up = statement.executeUpdate("INSERT INTO C2J (idJava,idComponent)"
    + "VALUES((SELECT idJava FROM JavaFiles),(SELECT idComponent FROM Components))");
    }

    I get an error that says:
    Subquery returns more than 1 row

    I tried using LIMIT 1 next to the ids (btw idJava and idComponent are foreign keys) but it doesn't really work right.

    Any help?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    What exactly are you trying to do? Are you trying to make a 1 to 1 connection between the two tables (one idJava to exactly one idComponent) or are you trying to create a 1 to many connection (ie one row for every idComponent per idJava or vce-versa)?
    Last edited by masijade; 03-15-2011 at 09:30 AM.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,428
    Rep Power
    21

    Default

    And that's an unusual constructor it has to be said.

  4. #4
    axenos is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    I know it's a strange constructor, but C2J has no other functionality...

    my connection is one to many: many JavaFiles in one Constructor

    I think I'll just have to add "Select idComponent FROM Components WHERE componentName=........"

    ps. thanks for your relpy

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,428
    Rep Power
    21

    Default

    But that's my point.
    Why have SQL executing in a constructor?
    It's really (really) not the place for it.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    Java Code:
    INSERT INTO C2J (idJava,idComponent) (SELECT a.idJava, b.idComponent FROM JavaFiles a, Components b)

  7. #7
    axenos is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    ok...i made a function for this and the constructor calls that function.

    masijade i tried my way and your way and the result is the same. I keep getting this error:
    Subquery returns more than 1 row

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    Don't use the values keyword (if that's what you are doing) and you may need to actually remove the parens around the select subquery. You want an insert-select not an insert-values.

    That you are doing this stuff in a constructor (whather as a method called from a constructor, or directly in the constructor) definately more than hints at a design problem, though.

Similar Threads

  1. JPA Primary Key from two foreign key
    By peterbo in forum New To Java
    Replies: 6
    Last Post: 06-29-2014, 10:30 AM
  2. foreign keys in jtable gui
    By blackpearlmoni in forum New To Java
    Replies: 0
    Last Post: 10-31-2009, 05:52 PM
  3. innodb and foreign keys problem
    By Pierced1 in forum JDBC
    Replies: 0
    Last Post: 06-29-2009, 02:59 AM
  4. Replies: 1
    Last Post: 02-20-2009, 05:16 PM
  5. System.getProperty with foreign language
    By barakz in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 05:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •