我有一个名为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);
}
答案 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");
后,组合框中填入了年份