【问题标题】:MySQL exception - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorExceptionMySQL 异常 - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
【发布时间】:2012-07-20 14:30:09
【问题描述】:

我得到错误:

''字段列表'中的未知列'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);}  

    }  

}  

【问题讨论】:

    标签: java mysql exception jdbc


    【解决方案1】:

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

    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";  
    

    【讨论】:

      【解决方案2】:

      您的语法问题在于 nameFROM 之间没有空格

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

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

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

      【讨论】:

      • 谢谢!我不知道这种事情不会由API自己处理。
      • @sweetdreams:怎么可能?你刚刚得到一个 string - 知道当你写“nameFROM”时你的意思是“name FROM”意味着什么?这对 API 的要求有点高……
      【解决方案3】:
      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);}  
      
      }  
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-20
        • 2016-12-30
        • 1970-01-01
        • 2013-11-20
        • 2016-07-07
        • 2015-01-22
        • 2018-04-26
        • 2011-12-15
        相关资源
        最近更新 更多