我有一个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");
时,组合框中填充了年份