我正在尝试实现一个search-by界面,它允许用户搜索仅在过去10天内为数据库添加的内容,然后将结果显示在jtable中。我收到sql语句的语法错误,但我不确定它有什么问题,因为我觉得它是正确的。我知道错误显示了语法错误的地方,但我不确定什么是准确的错误,或者是如何修复它。
这是我的代码:
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);
}
}我的错误是:
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发布于 2019-11-26 19:11:59
你只是在"AS diff“之后忘记了一个空格:)
发布于 2019-11-26 23:41:59
在这里很明显:'videoWHERE
总是读错误信息的开头,答案通常在那里:
使用
正确的语法以接近:
当你连接时,不要保存在引号前后的空格,使用额外的/双空格没有问题。
https://stackoverflow.com/questions/59057871
复制相似问题