我是编程新手,我想对按租金日期分组的数据进行一些过滤。但我得到错误
revenuebylocs bt_filterActionPerformed
SEVERE: null
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Rent_Date BETWEEN '2013-01-01' AND '2014-02-02' GROUP BY Rent_Date, a.Branch_ID' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2618)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1557)
at revenuebylocs.bt_filterActionPerformed(revenuebylocs.java:282)
at revenuebylocs.access$200(revenuebylocs.java:27)
at revenuebylocs$3.actionPerformed(revenuebylocs.java:179)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6267)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
这是我的代码
private void bt_filterActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
String value1= ((JTextField)dt_rangeto.getDateEditor().getUiComponent()).getText();
String value2= ((JTextField)dt_rangefrom.getDateEditor().getUiComponent()).getText();
String sql = " SELECT DISTINCT(Rent_Date) AS DATE, Branch_Name , SUM(Rent_Amount)"+
"FROM t_rent a INNER JOIN t_rent_detail b ON a.Rent_ID = "+
"b.Rent_ID INNER JOIN t_branch c ON a.Branch_ID=c.Branch_ID"+
"WHERE Rent_Date BETWEEN '"+value2+"' AND '"+value1+"' "+
"GROUP BY Rent_Date, a.Branch_ID ";
Pst = koneksi.getKoneksi().prepareStatement(sql);
rs=Pst.executeQuery(sql);
tb_revenue.setModel(DbUtils.resultSetToTableModel(rs));
JOptionPane.showMessageDialog(null, "Filtered");
} catch (SQLException ex) {
Logger.getLogger(staff.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "No Data");
}
}
我做错了什么?如果我手动输入rent_date的值,我可以显示它。我在mysqlog上测试它。 这是我在mysqlog上试过的字符串
SELECT DISTINCT(Rent_Date) AS DATE, Branch_Name , SUM(Rent_Amount)
FROM t_rent a INNER JOIN t_rent_detail b ON a.Rent_ID =
b.Rent_ID INNER JOIN t_branch c ON a.Branch_ID=c.Branch_ID
WHERE Rent_Date BETWEEN '2013-01-01' AND '2014-02-02'
GROUP BY Rent_Date, a.Branch_ID
它表现得很好。它在java中不起作用。我需要添加一些东西吗?
哦还有一个问题。我想像这样展示我的桌子:
Date S Mrental
2014-01-01 10000 10000
2014-05-05 10000 10000
我该怎么做? (S和M是Branch_Name)
答案 0 :(得分:1)
在WHERE
之前(以及FROM
之前)添加空格
" WHERE Rent_Date BETWEEN '"+value2+"' AND '"+value1+"' "