Solved

trouble with SQL statement in code

Posted on 2004-04-01
9
199 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 104
hibernate insert example 13 28
servlet web applications   metadata-complete="true" or false 3 36
mysql jsp example issue 32 32
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now