Welcome to Dream.In.Code
Become a Java Expert!

Join 149,950 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,333 people online right now. Registration is fast and FREE... Join Now!




Another JDBC Statement question

 
Reply to this topicStart new topic

Another JDBC Statement question

nick2price
25 Sep, 2008 - 02:36 PM
Post #1

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 332



Thanked: 11 times
My Contributions
How would i write an INSERT INTO statement in java where one of the values is gotten from another table. This is what i have attempted
CODE
    try
        {
            con = DatabaseUtils.connect(DRIVER, URL);  
            String sql = "INSERT INTO tblEvent (Event_Name, Meet_ID) VALUES (?) " +
            "SELECT eveType2, " +
            "ID FROM tblMeet " +
            "WHERE Meet_Name='Wembley' ";
            
            ps = con.prepareStatement(sql);
            ps.setString(1, eveType2);
            
            ps.executeUpdate();  

        }


Where ID is a primary key in tblMeet but i need to INSERT it as a foriegn key in this table.
User is offlineProfile CardPM
+Quote Post

johnmalloy
RE: Another JDBC Statement Question
25 Sep, 2008 - 04:47 PM
Post #2

New D.I.C Head
*

Joined: 24 Sep, 2008
Posts: 8



Thanked: 2 times
My Contributions
Not sure of the question but do you mean:

SQL

Insert into <TABLE3>
SELECT <COLUMN>
FROM <TABLE1>
LEFT JOIN <TABLE2> ON <TABLE1>.<INDEXED COLUMN> = <TABLE2>.<MATCHING INDEX COLUMN>
WHERE ...

User is offlineProfile CardPM
+Quote Post

pbl
RE: Another JDBC Statement Question
25 Sep, 2008 - 04:53 PM
Post #3

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,574



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(nick2price @ 25 Sep, 2008 - 03:36 PM) *

How would i write an INSERT INTO statement in java where one of the values is gotten from another table. This is what i have attempted
CODE
    try
        {
            con = DatabaseUtils.connect(DRIVER, URL);  
            String sql = "INSERT INTO tblEvent (Event_Name, Meet_ID) VALUES (?) " +
            "SELECT eveType2, " +
            "ID FROM tblMeet " +
            "WHERE Meet_Name='Wembley' ";
            
            ps = con.prepareStatement(sql);
            ps.setString(1, eveType2);
            
            ps.executeUpdate();  

        }


Where ID is a primary key in tblMeet but i need to INSERT it as a foriegn key in this table.


Not and SQL expert but you have a join missing.
Always manage to do my transactions without join.... Java does it for me..
no performance impact if the the tables have less than I'll say 1000 rows

User is online!Profile CardPM
+Quote Post

nick2price
RE: Another JDBC Statement Question
26 Sep, 2008 - 05:10 AM
Post #4

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 332



Thanked: 11 times
My Contributions
Will, i still be able to get the value if all the tables are updated at the same time?
Let me try explaining it better. I have a table called tblMeet. It has a primary key called ID which is an autoNumber and a meetName field set to 'Wembley'. Then i have another table called tblEvent. It also has an pimary key ID field set as an autoNumber, it has a eventName field, as it has a field called Meet_ID which is the foriegn key to the ID field in my tblMeet. Now in my java program, i want to try and set this foriegn key. I have got a working query in my database, like so
CODE
INSERT INTO tblEvent ( Event_Name, Meet_ID )
SELECT '100M Run', [ID]
FROM tblMeet
WHERE Meet_Name='Wembley';


But now i need to do this in my java program.

This post has been edited by nick2price: 26 Sep, 2008 - 05:25 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 05:32PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month