MySQL – Java语法错误。为什么这不起作用?

huangapple go评论84阅读模式
英文:

MySQL - Java syntax error. Why this doesnt work?

问题

错误行在“BETWEEN DATE_SUB(now(), interval ” + days + “ day) AND current_date()”附近,但这个语句在MySQL Workbench上可以工作。'
所以,也许错误在于使用between的方式或者日期的处理方式上。
我尝试做的是根据实际日期和之前通过变量days输入的天数来筛选数据,即在“date_sub(now(), interval 这里是天数 days)”中考虑。
是否有正确或更有效的方法?

private void getStatement(int days) {
    try {
        DefaultTableModel model = new DefaultTableModel();
        this.jtblReports.setModel(model);
        Connection con = this.connection.getConexion();
        PreparedStatement ps = null;
        ResultSet rs = null;

        String SELECT = "SELECT client_name, client_surname, client_dni,"
                + " game_name, rent_date, return_date, game_price from "
                + "rent_date BETWEEN DATE_SUB(now(), interval " + days + " day) AND current_date()";

        ps = con.prepareStatement(SELECT);
        rs = ps.executeQuery();

        ResultSetMetaData rsMd = rs.getMetaData();
        int columnsCant = rsMd.getColumnCount();

        model.addColumn("CLIENT NAME");
        model.addColumn("CLIENT SURNAME");
        model.addColumn("CLIENT DNI");
        model.addColumn("GAME");
        model.addColumn("RENT DATE");
        model.addColumn("RETURN DATE");
        model.addColumn("GAME PRICE");

        while (rs.next()) {

            Object[] rows = new Object[columnsCant];

            for (int i = 0; i < columnsCant; i++) {
                rows[i] = rs.getObject(i + 1);
            }
            model.addRow(rows);

        }

        model.fireTableDataChanged();
    } catch (SQLException e) {
        e.printStackTrace();
    }

}
英文:

The error line is highlited near "BETWEEN DATE_SUB(now(), interval " + days + " day) AND current_date()" but this statement work on the mysql workbench. '
So, maybe the mistake is on the way of using between or the date in that way.
The thing im trying to do is filter the data considering my actual date and the days before that i introduce throught the variable days in "date_sub(now(), interval here the days days)" .
Is there a correct or more efficient way?

private void getStatement(int days) {
try {
DefaultTableModel model = new DefaultTableModel();
this.jtblReports.setModel(model);
Connection con = this.connection.getConexion();
PreparedStatement ps = null;
ResultSet rs = null;
String SELECT = &quot;SELECT client_name, client_surname, client_dni,&quot;
+ &quot; game_name, rent_date, return_date, game_price from &quot;
+ &quot;rent_date BETWEEN DATE_SUB(now(), interval &quot; + days + &quot; day) AND current_date()&quot;;
ps = con.prepareStatement(SELECT);
rs = ps.executeQuery();
ResultSetMetaData rsMd = rs.getMetaData();
int columnsCant = rsMd.getColumnCount();
model.addColumn(&quot;CLIENT NAME&quot;);
model.addColumn(&quot;CLIENT SURNAME&quot;);
model.addColumn(&quot;CLIENT DNI&quot;);
model.addColumn(&quot;GAME&quot;);
model.addColumn(&quot;RENT DATE&quot;);
model.addColumn(&quot;RETURN DATE&quot;);
model.addColumn(&quot;GAME PRICE&quot;);
while (rs.next()) {
Object[] rows = new Object[columnsCant];
for (int i = 0; i &lt; columnsCant; i++) {
rows[i] = rs.getObject(i + 1);
}
model.addRow(rows);
}
model.fireTableDataChanged();
} catch (SQLException e) {
e.printStackTrace();
}
}

答案1

得分: 0

您的SQL查询至少缺少表名和WHERE子句的起始部分,应该像这样:

SELECT ... FROM your_table WHERE rent_date BETWEEN ...

英文:

Your SQL query is missing at least the name of the table and the start of the WHERE clause, it should look more like this:

SELECT ... FROM your_table WHERE rent_date BETWEEN ...

huangapple
  • 本文由 发表于 2020年6月29日 10:25:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/62630332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定