1

我得到错误:

'字段列表'中的未知列'customerno''。

但是,该列存在于我的客户表中。那为什么我会得到这个异常?

代码:

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);}  

    }  

}  
4

3 回答 3

4

看看你的查询到底是什么。这个:

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

相当于:

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

现在你能看出什么问题了吗?我很惊讶它抱怨customerno而不是缺少FROM一部分......

请注意,我怀疑您也不想要;。我会把它写成一行,只是为了可读性,如果可以的话,以及限制可访问性并使其成为最终版本:

private static final String QUERY = "SELECT customerno, name FROM customers";  
于 2012-07-21T17:12:17.210 回答
1

你的语法的问题是你在之间没有空格nameFROM

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

而是在之后添加一个空格name

 String myQuery =   
        "SELECT customerno, name " +  // add space here
        "FROM customers"; 
于 2012-07-21T17:21:10.913 回答
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())  
        {  

//尝试使用数据库中存在的数值来更改它,例如,如果它是第 2 列,则执行类似 rs.getString(1) 的操作;

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


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

}  

}
于 2012-07-21T17:10:09.337 回答