This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Transfer Learning for Natural Language Processing (MEAP) and have Paul Azunre on-line!
See this thread for details.
Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

I need help with an sql query

 
Ranch Hand
Posts: 170
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, world!

I have encountered a confusing problem with my table design for my media manager. Before insertion into a table called tbl_person I need to perform a procedure where I check if an actor aldready exists or not in the table tbl_castrole and if the actor does not exists in the latter table then I wan't insert the person in the tbl_person table.

The table tbl_person looks so far like this



And the table tbl_castrole looks like this



Before the insertion into the tbl_person table I wan't to do the checking in a method called



How would an sql query look like that checks if a person exists in two tables and has the same id in both of them? I am not a beginner when it comes to sql queries like this but I am very stuck here and are out of ideas. Also if the person exists should I insert this data in the tbl_person table with a unique identifier since there might be two or more persons with the same name in a movie? The latter is a side issue I haven't taken under consideration yet.

I hope I have been clear enough about what my problem is.

Very many wishes to all,
Robert!


 
Robert Ingmarsson
Ranch Hand
Posts: 170
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again!

Just forgot to tell that I am using the embedded apache derby database.

//Robert!
 
Robert Ingmarsson
Ranch Hand
Posts: 170
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, all!

I am just wondering if I am on the right track, I got the code



Just wondering, any further hints or comments are greatly much appreciated here.

Wishes,
Robert!
 
Sheriff
Posts: 7056
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two minor thoughts:

* Do you need the ORDER BY in the SELECT?
* I would put all the close() statements in a finally block so that they get closed whether or not the SQL statement produces an error.  You'll have to surround them with a try/catch block because they may not have been opened.  You can ignore the error.
 
Marshal
Posts: 25452
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or better still, use try-with-resources:



That construction causes the PreparedStatement and the ResultSet to be auto-closed by a secret finally block.

And design-wise, the Connection should normally be closed by the code which created it and not inside a method which received it as a parameter.
 
Knute Snortum
Sheriff
Posts: 7056
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that's better.  Does the ResultSet also get closed in that code?
 
Paul Clapham
Marshal
Posts: 25452
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it does.
 
Robert Ingmarsson
Ranch Hand
Posts: 170
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, again!

Thanks for the replies I have taken the advices and applied them to my code. I'm not quite satisfied becasue this query is really complicated for me at least. First off now there is three tables instead of the earlier two. The first is tbl_person, the second is tbl_crewrole and the third is tbl_castrole. I need a query that does the following: First check if input A is stored in tbl_person and if so, check if the ID for object A is stored in tbl_crewrole or tbl_castrole.

I hope there is someone here that can help me out. The latest thing that I have tried was this sql but it doesn't work



Very many regards and wishes,
Robert!

 
Robert Ingmarsson
Ranch Hand
Posts: 170
2
Netbeans IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just forgot to say that the query is in a method that returns true if the id for object A is found in either tbl_castrole or tbl_crewrole and false if it isn't.



Also I can point out that since this is a very complicated sql-procedure for me I am not sure if I am thinking accordingly to the logic of the method.

//Robert

 
Paul Clapham
Marshal
Posts: 25452
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Robert Ingmarsson wrote:Just forgot to say that the query is in a method that returns true if the id for object A is found in either tbl_castrole or tbl_crewrole and false if it isn't.



But your query pretty clearly returns records when the id is NOT in tbl_castrole AND it's NOT in tbl_crewrole. That's the exact opposite of what you want.

I suppose you could change the code to keep that query and return false if there are records and true otherwise. But that would be confusing to future readers of the code.

Short answer: Simply change the query to reflect what you said it was supposed to do.
    Bookmark Topic Watch Topic
  • New Topic