Results 1 to 9 of 9
Thread: problem with java --> sql
- 04-13-2010, 08:10 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 20
- Rep Power
- 0
problem with java --> sql
hey, i'm trying to learn some java and started my own project where i'm trying to filter from dates but i get the following error with this sql statement.
what is wrong here ?
SQL="SELECT * from info where firstdate between'" + Startdate.getText() +"'" + "and'" + Enddate.getText() +"'" + ",count(topic) FROM INFO GROUP BY topic'" ;
Caused by: java.sql.SQLException: near ",": syntax error
- 04-13-2010, 08:18 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 04-13-2010, 08:58 PM #3
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
here's your SQL statement:
SELECT * from info where firstdate between'01/01/01'and'02/02/02',count(topic) FROM INFO GROUP BY topic'
more than one problem with it... First make sure the SQL query works by itself, then put it into Java
- 04-13-2010, 09:09 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 20
- Rep Power
- 0
- 04-13-2010, 09:21 PM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
what I'm saying is this: the problem is not Java but the SQL query.
- the query is not well-formatted (seems to me you need spaces in the '01/01/01'and'02/02/02' part; extra ' symbol at the end)
- when you have re-formatted it, you aren't using the GROUP BY correctly:
SELECT first_name,count(id) FROM people GROUP BY id [that's what you're doing, incorrect]
SELECT first_name,count(id) FROM people GROUP BY first_name [correct]
I'm not sure it's even possible to use SELECT * and GROUP BY in the same query.
- when you've fixed all that, Java part will work
- 04-13-2010, 10:01 PM #6
your sql in the first post was wrong. the syntax is
SELECT
FROM
WHERE
GROUP BY
you can use SELECT * and GROUP BY in the same query, no problem. and the date-format is in most cases YYYY-MM-DD but look at your db manual. which db are you using?
the sql-statement
Java Code:SELECT * from info where firstdate between'01/01/01'and'02/02/02',count(topic) FROM INFO GROUP BY topic'
will never work. you can't use FROM after WHERE in this way. if you want to know how many rows per topic are your table then use
select topic, count(*) from info where firstdate between startdate and enddate group by topic. try it out and check if the count is ok.Last edited by j2me64; 04-13-2010 at 10:14 PM.
- 04-14-2010, 10:05 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Can I also say this ought to be a PreparedStatement, and binding the two dates into it?
- 04-14-2010, 12:57 PM #8
Member
- Join Date
- Dec 2009
- Posts
- 20
- Rep Power
- 0
- 04-14-2010, 01:03 PM #9
Member
- Join Date
- Dec 2009
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
Problem with Java...
By mc6415 in forum New To JavaReplies: 10Last Post: 02-22-2010, 12:24 AM -
java problem
By Mj Shine in forum New To JavaReplies: 5Last Post: 08-15-2009, 05:09 AM -
Java Problem. Need Help!
By bob101 in forum New To JavaReplies: 6Last Post: 03-19-2009, 04:34 AM -
Help, java problem
By Suriman in forum New To JavaReplies: 4Last Post: 03-09-2009, 04:26 PM -
problem with java nio
By andrei stoiculescu in forum NetworkingReplies: 3Last Post: 02-02-2009, 03:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks