我有一个MySQL报价表,列名为quote_date
,其中存储了报价发布的日期。我想在我的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);
}
没有填充组合框的原因是在第一行:
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");
,组合框就会填充年份