首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将选择语句限制为10天

将选择语句限制为10天
EN

Stack Overflow用户
提问于 2019-11-26 19:09:42
回答 2查看 79关注 0票数 2

我正在尝试实现一个search-by界面,它允许用户搜索仅在过去10天内为数据库添加的内容,然后将结果显示在jtable中。我收到sql语句的语法错误,但我不确定它有什么问题,因为我觉得它是正确的。我知道错误显示了语法错误的地方,但我不确定什么是准确的错误,或者是如何修复它。

这是我的代码:

代码语言:javascript
运行
复制
  package project_files;



import javax.swing.JFrame;
import javax.swing.JToolBar;
import javax.swing.table.DefaultTableModel;

import project_files.registration_test;
import project_files.root_login;
import project_files.gui_interface;
import project_files.video_interface;

import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JSeparator;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.JTable;

public class searchby {

    private JFrame frame;
    private JTable table;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    searchby window = new searchby();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public searchby() {
        initialize();
    }

    public void video_interface() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(200, 200, 523, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lbSearch = new JLabel("Search by");
        lbSearch.setBounds(209, 12, 100, 15);
        frame.getContentPane().add(lbSearch);

        JButton btnSearch = new JButton("Search by");
        btnSearch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                   DefaultTableModel model = new DefaultTableModel(new String[]{"video name", "video description", "video_city", "video_subject", "video_tags"}, 0);
                try {

                    Class.forName("com.mysql.cj.jdbc.Driver");
                    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/userdatabase", "root", "pass1234");
                            Statement stmt= con.createStatement();
                            String sql = "SELECT *, (NOW() - INTERVAL 10 DAY) AS diff" + 
                                    "FROM video " + 
                                    "WHERE date >= (NOW() - INTERVAL 10 DAY))";
                            ResultSet rs=stmt.executeQuery(sql);
                            if(rs.next())
                            {
                                 String d = rs.getString("video_name");
                                 String e = rs.getString("video_description");
                                 String f = rs.getString("video_city");
                                 String g = rs.getString("video_subject");
                                 String h = rs.getString("video_tags");
                                 model.addRow(new Object[]{d, e, f, g, h});
                                    table.setModel(model);
                            {   JOptionPane.showMessageDialog(null, "videos selected Successfully...");
                            }
                            }

                            con.close();
                } catch(Exception e) {System.out.print (e);}






            }});
        btnSearch.setBounds(23, 203, 130, 25);
        frame.getContentPane().add(btnSearch);



        JButton btnNewExit = new JButton("Exit");
        btnNewExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                JFrame frmLogin_sys = new JFrame("Exit");
                if (JOptionPane.showConfirmDialog(frmLogin_sys, "Confirm if you want to exit", "Login System",
                JOptionPane.YES_NO_OPTION)== JOptionPane.YES_NO_OPTION) {
                    System.exit(0);
                }
            }
        });
        btnNewExit.setBounds(373, 203, 114, 25);
        frame.getContentPane().add(btnNewExit);

        JSeparator separator = new JSeparator();
        separator.setBounds(12, 175, 499, 2);
        frame.getContentPane().add(separator);

        JSeparator separator_1 = new JSeparator();
        separator_1.setBounds(12, 36, 499, 2);
        frame.getContentPane().add(separator_1);

        table = new JTable();
        table.setBounds(103, 61, 285, 100);
        frame.getContentPane().add(table);




    }
}

我的错误是:

代码语言:javascript
运行
复制
java.sql.SQLSyntaxErrorException: 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 'videoWHERE date >= (NOW() - INTERVAL 10 DAY)' at line 1
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-11-26 19:11:59

你只是在"AS diff“之后忘记了一个空格:)

票数 4
EN

Stack Overflow用户

发布于 2019-11-26 23:41:59

在这里很明显:'videoWHERE

总是读错误信息的开头,答案通常在那里:

使用

正确的语法以接近:

当你连接时,不要保存在引号前后的空格,使用额外的/双空格没有问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59057871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档