Correct syntax for query filter
Hi guys well what I'm trying to do is produce a query that only produces the results which will match my director input. I have looked for the correct syntax and cannot seem to find it.
Any help would be appreciated
Code of my syntax below:
ResultSet results = myStatement.executeQuery
("SELECT VIDEOID, VIDEONAME, DIRECTOR FROM VIDEOS");
while (results.next()) {
}
TIM_M_91
Junior Poster in Training
83 posts since Feb 2012
Reputation Points: 18
Solved Threads: 0
Skill Endorsements: 0
Check if the connection is created to the database
Connection con;
Statement stmt=con.createStatement();
ResultSet rs=null;
String ss=("SELECT VIDEOID, VIDEONAME, DIRECTOR FROM VIDEOS");
rs=stmt.executeQuery(ss);
while(rs.next())
{
//code goes here
}
That is not what TIM_M_91 has asked.
@TIM_M_91:
It is odd that you haven't found the correct syntax because all you needed is some basic tutorials about sql: http://w3schools.com/sql/default.asp
Any way, you can add this to your query:
SELECT VIDEOID, VIDEONAME, DIRECTOR
FROM VIDEOS
WHERE VIDEOID=[I]something[/I] AND VIDEONAME=[I]somethingelse[/I] ...
Instead of AND you can use OR
The columns that you use as filters don't need to be at the select. This query is valid for example:
SELECT VIDEONAME
FROM VIDEOS
WHERE VIDEOID=[I]something[/I]
If you want to put it in java, using the Statement interface (as in your code) you can try this:
String videoId = "some_value";
String ss="SELECT VIDEOID, VIDEONAME, DIRECTOR FROM VIDEOS";
ss += " WHERE VIDEOID='"+videoId+"'";
System.out.println("Executing query:"+ss);
rs=stmt.executeQuery(ss);
What's out for the single quotes ' . The value of the ss is the query executed so print it and see what you have.
javaAddict
Nearly a Senior Poster
3,338 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 450
Skill Endorsements: 7