Hello, I have a simple handler on the button, depending on what number of the month we enter into the textfield, it considers the amount of money for a given month. Here is my code:

public void actionPerformed(ActionEvent e) { try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cursova", "root", "root"); String sql = "SELECT YEAR(endDate) as SalesYear , " + "MONTH(endDate) as SalesMonth, " + "SUM(price) AS TotalSales " + "FROM arend where month(endDate) =? " + "GROUP BY YEAR(endDate), MONTH(endDate) " + "ORDER BY YEAR(endDate), MONTH(endDate)"; PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, textFieldTest.getText()); ResultSet rs = pst.executeQuery(); if (rs.next()) { String year = rs.getString("SalesYear"); labelMonth.setText(year); String month = rs.getString("SalesMonth"); lblYear.setText(month); String result = rs.getString("TotalSales"); resultLabel.setText(result); } } catch (SQLException ex) { ex.printStackTrace(); } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } 

But the error knocks out:

column 'endDate' not found

Closed due to the fact that off-topic participants Mike , Alexei Shimansky , Pavel Mayorov , VenZell , aleksandr barakin 9 Jun '16 at 14:18 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Mike, Alexey Shimansky, Pavel Mayorov, VenZell, aleksandr barakin
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • You do not have a column with the name endDate in the table in the database - Alexey Shimansky
  • @ Alexey Shimansky is, here are images.vfl.ru/ii/1465333410/bc222af6/12949442.png - M-Misha-M
  • Perhaps you did not update the database after adding this column - YuriySPb
  • @ M-Misha-M in the SELECT query in the enumeration there is no mention of the endData column and price as well - Alexey Shimansky
  • @ Alexey Shimansky, I'm sorry, I'm confused a bit, what do I need to do? I removed as the same error. Ie I can not write month (endDate) ??? I just need to extract a month - M-Misha-M

2 answers 2

You have in the result set three columns SalesYear , SalesMonth and TotalSales , the names of which you assign using the keyword AS . However, you retrieve endDate and price , so a message is returned to you about the impossibility of finding a column with this name. Even if these columns exist in the source table, they do not fall into the result set.

  • I removed as the same error ((I can't write Month (endDate)? - M-Misha-M
  • @ M-Misha-M, better not remove the AS construct, it allows you to set convenient names. It is better to add the missing endDate column to the SELECT clause, and to rs.getString ("price"), use rs.getString ("TotalSales") - cheops
  • did not understand a bit, see I changed, say, String year = rs.getString ("SalesYear"); labelMonth.setText (year); but now it says that this does not exist either, you can pliz update your answer - M-Misha-M
  • the error remains the same column 'SaleYear' not found - M-Misha-M
  • In the string you gave, String year = rs.getString ("SalesYear"); At the end of SalesYear there is a space - is it a typo or is it the same in the code? If it is present in the code, it should be removed. - cheops

Update:

The problem is fixed by such a request. id_arend is the key field here.

 String sql = "SELECT id_arend, YEAR(endDate) as SalesYear, " + "MONTH(endDate) as SalesMonth, " + "SUM(price) as TotalSales " + "FROM arend where month(endDate) =? " + "GROUP BY YEAR(endDate), MONTH(endDate) " + "ORDER BY YEAR(endDate), MONTH(endDate)"; 

Thanks to all

  • id_arend здесь ключевое поле. - but? what? I did not understand O_o - Alexey Shimansky
  • @ Alexey Shimansky is the primary key, autoincrement, I included it in the request and everything is fine. I did not understand why the request did not work without select id_arend - M-Misha-M