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

Join 150,201 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,009 people online right now. Registration is fast and FREE... Join Now!




SQL query with JDBC

2 Pages V  1 2 >  
Closed TopicStart new topic

SQL query with JDBC

nick2price
22 Sep, 2008 - 01:40 PM
Post #1

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
I am trying to come up with the correct sql for my database query but cannot seem to get it right *INEXPERIENCED* I'm not sure if this should be posted here, as its being done through java and PreparedStatements, or in an sql forum. I shall try here for now if you fine people dont mind.
I have 4 tables all linked appropiately.
CODE
Meet(meetID, meetName)
Event(eventID, meetID, eventName)
Round(roundID, eventID, roundNumber)
Competitor(CompetitorID, roundID, name, timeSet)


You can see in my Competitor table i have a field Time_Set. I need to return the 3 fastest times, in round 1, for the event 100M Run. My whole database is saved under the name Register, although i dont know if its need for this. I have attempted somthing along the lines of:
CODE
        PreparedStatement ps = null;
        ResultSet rs = null;
        
    try {
            con = DatabaseUtils.connect(DRIVER, URL);  
            ps = con.prepareStatement("SELECT Time_Set FROM Competitor WHERE Round.Round_Number = VALUES (?) AND Event.Event_Name = VALUES (?) ORDER BY Time_Set ASC");
            ps.setString(1, "Round_1");
            ps.setString(2, "100M Run");
            rs = ps.executeQuery();


             while (rs.next()) {
            String s = rs.getString("Time_Set");
            System.out.println(s);
        }

        }


I am being returned some lovely undefined function errors as my sql is problably embarresingly pathetic. Any help would be great.
cheers
User is offlineProfile CardPM
+Quote Post

Gloin
RE: SQL Query With JDBC
22 Sep, 2008 - 01:59 PM
Post #2

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 935



Thanked: 54 times
My Contributions
I haven't practiced SQL in a while now but as far as I remember, you have to match

WHERE Round.Round_Number = <--

with values from the Competitor table.
User is online!Profile CardPM
+Quote Post

g00se
RE: SQL Query With JDBC
22 Sep, 2008 - 02:08 PM
Post #3

D.I.C Addict
Group Icon

Joined: 19 Sep, 2008
Posts: 521



Thanked: 44 times
My Contributions
The sort of thing you need (although you might need to return more columns) is:

CODE

SELECT TOP 3
timeSet
FROM
Competitor C
INNER JOIN
Round R
ON C.roundID = R.roundID
INNER JOIN
Event E
ON R.eventID = E.eventID
ORDER BY 1 DESC

User is offlineProfile CardPM
+Quote Post

nick2price
RE: SQL Query With JDBC
22 Sep, 2008 - 02:23 PM
Post #4

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
Ahhh, that Inner Join stuff has kinda jogged a memory cell in my brain. Now the problem is getting that to be a useable preparedStatement.
User is offlineProfile CardPM
+Quote Post

nick2price
RE: SQL Query With JDBC
22 Sep, 2008 - 04:24 PM
Post #5

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
At the moment, i have it like this
CODE
ps = con.prepareStatement("SELECT Time_Set FROM Competitor C INNER JOIN Round R ON C.roundID = R.roundID INNER JOIN Event E ON R.eventID = E.eventID ORDER BY 1 DESC ");

But my syntax according to my error is totally wrong. And with the above, how is it getting the round number and event name?
User is offlineProfile CardPM
+Quote Post

g00se
RE: SQL Query With JDBC
22 Sep, 2008 - 11:27 PM
Post #6

D.I.C Addict
Group Icon

Joined: 19 Sep, 2008
Posts: 521



Thanked: 44 times
My Contributions
I would suggest you get the query working in your db command line client before you use it in Java

QUOTE
And with the above, how is it getting the round number and event name?


It isn't - you're only selecting competitor. As i mentioned above, you need to select the columns you want to be returned
User is offlineProfile CardPM
+Quote Post

g00se
RE: SQL Query With JDBC
23 Sep, 2008 - 12:57 AM
Post #7

D.I.C Addict
Group Icon

Joined: 19 Sep, 2008
Posts: 521



Thanked: 44 times
My Contributions
I've just executed the following successfully in MySQL:

CODE
SELECT timeSet FROM Competitor C INNER JOIN CRound R ON C.roundID = R.roundID INNER JOIN Event E ON R.eventID = E.eventID ORDER BY 1 DESC LIMIT 3



User is offlineProfile CardPM
+Quote Post

nick2price
RE: SQL Query With JDBC
23 Sep, 2008 - 02:33 AM
Post #8

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
Right, i am pretty sure i have the sql right, but i am also sure that i have not placed it in my java code correctly. Any help in sorting this out please would be soooo appreciated.
CODE
try {
            con = DatabaseUtils.connect(DRIVER, URL);

            rs = st.executeQuery("SELECT TOP 3 c.Time_Set FROM Competitor c") +
                ("INNER JOIN Round r") +
                ("ON r.Competitor_ID=c.Competitor_ID") +
                ("INNER JOIN Event e") +
                ("ON e.Event_ID=r.Event_ID") +
                ("WHERE r.Round_Number = 'Round 1' AND e.Event_Name = '100M Run'") +
                ("ORDER BY c.Time_Set ASC ");

                rs = st.executeQuery();


             while (rs.next()) {
            String s = rs.getString("Time_Set");
            System.out.println(s);
        }

User is offlineProfile CardPM
+Quote Post

Gloin
RE: SQL Query With JDBC
23 Sep, 2008 - 03:42 AM
Post #9

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 935



Thanked: 54 times
My Contributions
The problem is possibly that when you concatenate the strings using the + operator

("INNER JOIN Round r") +
("ON r.Competitor_ID=c.Competitor_ID") +

you forget to insert blanks in the end of the lines.
So the end result looks like this

..."INNER JOIN Round rON r.Competitor_ID=c.Competitor_ID"...

On the good side, it's very easy to change.
User is online!Profile CardPM
+Quote Post

nick2price
RE: SQL Query With JDBC
23 Sep, 2008 - 04:03 AM
Post #10

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
On the good side, that was easy to fix. On the bad side, i now get a null pointer exception, even when i have changed my code to be somthing simple like
CODE
    try {
            con = DatabaseUtils.connect(DRIVER, URL);

            String str=("SELECT * FROM Competitor");// +
                  rs=st.executeQuery(str);


             while (rs.next()) {
            String s = rs.getString("Time_Set");
            System.out.println(s);
        }


What could be the causes of this?
User is offlineProfile CardPM
+Quote Post

nick2price
RE: SQL Query With JDBC
23 Sep, 2008 - 04:09 AM
Post #11

D.I.C Regular
***

Joined: 23 Nov, 2007
Posts: 338



Thanked: 12 times
My Contributions
Passed the null pointer exception, but now i am apparantly missing an operator in the line
CODE
                ("INNER JOIN Round r ON r.Competitor_ID = c.Competitor_ID ") +

User is offlineProfile CardPM
+Quote Post

Gloin
RE: SQL Query With JDBC
23 Sep, 2008 - 04:10 AM
Post #12

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 935



Thanked: 54 times
My Contributions
What causes the null pointer exception. Is there anything to be selected in the DB?
What is st?
According to your first posts, Time_Set is not in Competitor, however timeSet is, did you make any changes in the DB?

String s = rs.getString("Time_Set");
User is online!Profile CardPM
+Quote Post

2 Pages V  1 2 >
Closed TopicStart new topic
Time is now: 1/9/09 04:59AM

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