NetBeans IDE中的Combobox不会填充MySQL提取的年份

时间:2015-10-08 16:57:58

标签: java mysql swing netbeans

我有一个名为quote_date的引号的MySQL表,它存储了发布报价的日期。我想在那个日期的那一年填充我的Netbeans应用程序中的组合框。我过去获得年份的SQL语法是:

SELECT EXTRACT(YEAR FROM quote_date) AS quoteYear from quotesdb 

此语法适用于MySQL,但在Netbeans上,组合框保持为空。没有发生错误。可能有什么不对?

以下是我在Netbeans中实现语法的方法:

try
 {
     String sql = "SELECT EXTRACT(YEAR FROM quote_date) AS quoteYear from quotesdb";
     Class.forName("com.mysql.jdbc.Driver");
    //connection for database
    Connection conn = (Connection)
            //root and username and password for access to the database
    DriverManager.getConnection("jdbc:mysql://localhost:3306/salventri","root","password");
    //create the statement that will be used
    Statement stmt=conn.createStatement();
    ResultSet rs = stmt.executeQuery(sql); 

    while(rs.next())
    {
        String year = rs.getString("quote_date");
        //adding the quote year to the combobox
        cboQIYear.addItem(year);
    }

1 个答案:

答案 0 :(得分:1)

它没有填充组合框的原因是在初始行中:

String sql = "SELECT EXTRACT(YEAR FROM quote_date) AS quoteYear from quotesdb";

我想从quoteYear获得年份,但我没有更改该行中的列名:

String year = rs.getString("quote_date");

rs.getString("quote_date");更改为rs.getString("quoteYear");后,组合框中填入了年份