Solved

trouble with SQL statement in code

Posted on 2004-04-01
9
200 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
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 92

Accepted Solution

by:
objects earned 100 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

840 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