1

I get the error:

'Unknown column 'customerno' in 'field list' '.

But, that column exists in my customer table. Then why am I getting this exception ?

Code:

import java.sql.*;  

public class Classy {  

    static String myQuery =   
            "SELECT customerno, name" +  
            "FROM customers;";  

    public static void main(String[]args)  
    {  
        String username = "cowboy";  
        String password = "1234567";  
        try  
        {  
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Business", username, password);  
            Statement stmt = con.createStatement();  
            ResultSet rs = stmt.executeQuery(myQuery);  

            while(rs.next())  
            {  
                System.out.print(rs.getString("customerno"));  
            }

        } catch(SQLException ex){System.out.println(ex);}  

    }  

}  

3 Answers 3

4

Look at what your query really is. This:

static String myQuery =   
        "SELECT customerno, name" +  
        "FROM customers;";  

is equivalent to:

static String myQuery = "SELECT customerno, nameFROM customers;";  

Now can you see what's wrong? I'm surprised it complained about customerno rather than the lack of a FROM part...

Note that I suspect you don't want the ; either. I'd write it all one one line just for readability, when you can, as well as limiting the accessibility and making it final:

private static final String QUERY = "SELECT customerno, name FROM customers";  
Sign up to request clarification or add additional context in comments.

Comments

1

the problem with your syntax is that you have no space between name and FROM

String myQuery =   
        "SELECT customerno, name" +  // problem is here
        "FROM customers;"; 

instead add a space after name

 String myQuery =   
        "SELECT customerno, name " +  // add space here
        "FROM customers"; 

2 Comments

Thanks ! I did not know that this kind of thing would not be handled by the API itself.
@sweetdreams: How could it be? You've just got a string - how is it meant to know that when you wrote "nameFROM" you meant "name FROM"? That's asking a bit much of an API...
0
import java.sql.*;  

public class Classy {  

static String myQuery =   
        "SELECT customerno, name" +  
        "FROM customers;";  

public static void main(String[]args)  
{  
    String username = "cowboy";  
    String password = "1234567";  
    try  
    {  
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Business", username, password);  
        Statement stmt = con.createStatement();  
        ResultSet rs = stmt.executeQuery(myQuery);  

        while(rs.next())  
        {  

//Try and change this with the numeric value that is present in the database, e.g if it's column 2 do something like rs.getString(1);

            System.out.print(rs.getString("customerno"));  
        }  


    }catch(SQLException ex){System.out.println(ex);}  

}  

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.