Avatar of annie613
annie613

asked on 

trouble with SQL statement in code

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()
Java

Avatar of undefined
Last Comment
Mick Barry
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
can you print the stack trace? please
Ghost
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>ResultSet rs = stmt.executeQuery("SELECT English from translator WHERE Spanish="+spanWrd);        

should be

ResultSet rs = stmt.executeQuery("SELECT English from translator WHERE Spanish='"+spanWrd + "'");        
Avatar of 91mustang
91mustang

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
Avatar of 91mustang
91mustang

oops mine is still wrong,  Objects and CEHJ's are correct....
Avatar of Tommy Braas
Tommy Braas
Flag of Australia image

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();
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of annie613
annie613

ASKER

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!
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo