If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > JAVA > Help with search query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-10-06, 05:24
uraknai uraknai is offline
Registered User
 
Join Date: Mar 2006
Posts: 41
Help with search query

Hi,

I'm having trouble writing the java code for the following mySql query:

SELECT student_id, first_name, last_name
FROM student
WHERE student_id = given student_id;

I can do it for a set id but need it to work when the student_id is given as a method parameter.

So far, I have:

Code:
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT student_id, first_name, last_name " + "FROM student " + "WHERE student_id = " + "329012");

but need something like:

Code:
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT student_id, first_name, last_name " + "FROM student " + "WHERE student_id = " + '"student_id"');
I'm having some problem with the quotation marks. I need to student_id to be input as a parameter but the whole query to be read as a String. I'm currently getting

Unclosed character literal and Unclosed String literal error messages.

Could someone please help.
Reply With Quote
  #2 (permalink)  
Old 07-10-06, 06:23
dimis2500 dimis2500 is offline
Registered User
 
Join Date: Jan 2005
Posts: 362
PreparedStatement


Last edited by dimis2500; 07-10-06 at 06:30.
Reply With Quote
  #3 (permalink)  
Old 07-11-06, 18:07
HackmanC HackmanC is offline
Registered User
 
Join Date: Jul 2006
Posts: 8
Like dimis2500 said you should use Prepared Statements or better Callable Statements for Stored Procedures.

But... some times is easy to write a query like yours for testing ...
or when a query can be polymorphic, if I am allow to say so.

Almost every dbms can use the ' like a ", you can use the syntax:
... + "WHERE student_id = '" + student_id + "'"
but only when there is no other option

Last edited by HackmanC; 07-11-06 at 18:15.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On