Solved

trouble with SQL statement in code

Posted on 2004-04-01
9
197 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:

757 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

22 Experts available now in Live!

Get 1:1 Help Now