?
Solved

trouble with SQL statement in code

Posted on 2004-04-01
9
Medium Priority
?
205 Views
Last Modified: 2010-07-27
here is my code
 public void transSpanish()
  {
    if (comboSpanish.getSelectedItem().equals(spanWrd))
    System.out.println("hi im: " +spanWrd);//test for now
    {
        jTextArea1.setText(""+ spanWrd +" translates to the english ");//just to see in text area for now
        try
        {
            //connection to DB
            Connection con = DriverManager.getConnection(url, user, password);
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT English from translator WHERE Spanish="+spanWrd);        
            while (rs.next())
            {
                int col = 0;
                String English = rs.getString("English");
               //pulls from DB and translates word from spanish to english
                jTextArea1.setText("" + English);
            }//end while

            stmt.close();
            con.close();
        }//end try
        catch (Exception e)
        {
            System.out.println(e);
        }//end catch
       
    }//end if spanWrd    

my table is called translator
my Primarykey is wordID
the other fields are (wordID, Category, English, Spanish, Categoría)

this function works....fine...all i want to do in the first function is take the spanish word from the choice box
and compare it to the table then in the textArea show the english word.... ???? any comment :)
im getting sometype of error...missing parameter...


//this function works
public void viewWords()
      {
          //Set string array to the column headers
          String columnheaders[] = {"Category", "English", "Spanish", "Categoría"};
          
          int row = 2; //start at row two on table
          
        tblWords.setFont(new Font("Dialog", 0, 10));
        tblWords.setEnabled(false);
               
        // show column headers        
        for (int i=0; i<columnheaders.length; i++)
        tblWords.setValueAt(columnheaders[i], 0, i);
          
          try
        {
            Connection con = DriverManager.getConnection(url, user, password);
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT Category, English, Spanish, Categoría  FROM translator");
           
            while (rs.next())
            {
                //Get information from database to variables
                int col = 0;
                String Category = rs.getString("Category");
                String English = rs.getString("English");
                String Spanish = rs.getString("Spanish");
                String Categoría = rs.getString("Categoría");
               
               
                tblWords.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
               
                //Print information out in a row
                tblWords.setValueAt(Category, row, col);
                col++;
                tblWords.setValueAt(English, row, col);
                col++;                
                tblWords.setValueAt(Spanish, row, col);
                col++;
                tblWords.setValueAt(Categoría, row, col);          
                row++;
            }//while

            stmt.close();
            con.close();
        }//try
        catch (Exception e)
        {
            System.out.println(e);
        }//catch
      }//viewWords()
0
Comment
Question by:annie613
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 10736943
try:

ResultSet rs = stmt.executeQuery("SELECT English from translator WHERE Spanish='"+spanWrd+"'");        
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10736951
can you print the stack trace? please
Ghost
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10736955
>>ResultSet rs = stmt.executeQuery("SELECT English from translator WHERE Spanish="+spanWrd);        

should be

ResultSet rs = stmt.executeQuery("SELECT English from translator WHERE Spanish='"+spanWrd + "'");        
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Expert Comment

by:91mustang
ID: 10736974
you need to suround the java variables with ' and '
ResultSet rs = stmt.executeQuery("SELECT English from translator WHERE Spanish="'+spanWrd+'");    

as you working with a varchar field
0
 
LVL 4

Expert Comment

by:91mustang
ID: 10736985
oops mine is still wrong,  Objects and CEHJ's are correct....
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10737030
Or better yet, use a PreparedStatement and not worry about quoting!

            PreparedStatement stmt = con.prepareStatement("SELECT English from translator WHERE Spanish=?");
            stmt .setString(1, spanWrd);
            ResultSet rs = stmt.executeQuery();
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10737054
Yes, better to use a PreparedStatement and also a good idea to manipulate the table through its model, not through the gui as appears to be happening at the moment
0
 

Author Comment

by:annie613
ID: 10737060
ok the first thing i tried worked
objects suggested
ResultSet rs = stmt.executeQuery("SELECT English from translator WHERE Spanish='"+spanWrd+"'");

and that was my problem
single quotes :)
thanks again
cheers!
0
 
LVL 92

Expert Comment

by:objects
ID: 10737070
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question