Results 1 to 8 of 8
Thread: jdbc foreign keys problem
- 03-15-2011, 01:21 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
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?
- 03-15-2011, 07:50 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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 08:30 AM.
- 03-15-2011, 08:25 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
And that's an unusual constructor it has to be said.
- 03-15-2011, 10:06 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
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
- 03-15-2011, 10:09 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
But that's my point.
Why have SQL executing in a constructor?
It's really (really) not the place for it.
- 03-15-2011, 10:22 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Java Code:INSERT INTO C2J (idJava,idComponent) (SELECT a.idJava, b.idComponent FROM JavaFiles a, Components b)
- 03-15-2011, 10:57 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 18
- Rep Power
- 0
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
- 03-15-2011, 11:07 AM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
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
-
JPA Primary Key from two foreign key
By peterbo in forum New To JavaReplies: 4Last Post: 10-23-2010, 03:34 PM -
foreign keys in jtable gui
By blackpearlmoni in forum New To JavaReplies: 0Last Post: 10-31-2009, 04:52 PM -
innodb and foreign keys problem
By Pierced1 in forum JDBCReplies: 0Last Post: 06-29-2009, 02:59 AM -
How do i display foreign languages in the IDE console panel ?
By BobZ_Annapolis in forum EclipseReplies: 1Last Post: 02-20-2009, 04:16 PM -
System.getProperty with foreign language
By barakz in forum Advanced JavaReplies: 2Last Post: 11-25-2008, 04:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks