java.sql.SQLException:[SQLITE_ERROR] SQL错误或缺少数据库(接近“=”:语法错误)

时间:2013-06-06 00:14:50

标签: java sql exception

我使用firefox web开发的东西创建了sqlite数据库,将它保存到我的电脑但是在链接数据库时遇到了一些问题。每当我单击登录按钮进行测试时,我得到“java.sql.SQLException:[SQLITE_ERROR] SQL错误或缺少数据库(接近”=“:语法错误)”。他建议我在数据库类中将“//”更改为“\”并查看它是否有效,但如果有人可以查看代码并帮助我,我会非常感激。

加载程序代码

import java.awt.*;
import javax.swing.*;

import java.awt.event.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Loader extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
JLabel username;
JTextField userField;
JButton register;
JLabel password;
JPasswordField passField;
JButton login;
ImageIcon logo;
JLabel imageLogo;
Connection conn = null;
ResultSet rs = null;
PreparedStatement ps = null;

Loader() {
conn = DBConnect.ConnectDB();
setLayout(null);
username = new JLabel("Username");
username.setBounds(100, 75, 75, 75);
userField = new JTextField("", 10);
userField.setBounds(162, 102, 90, 20);
register = new JButton("Register");
register.setForeground(Color.white);
register.setBackground(Color.black);
register.setBounds(275, 95, 90, 30);
password = new JLabel("Password");
password.setBounds(100, 105, 75, 75);
passField = new JPasswordField();
passField.setBounds(162, 132, 90, 20);
login = new JButton("Login");
login.addActionListener(this);
login.setBounds(275, 125, 90, 30);
login.setForeground(Color.white);
login.setBackground(Color.black);
logo = new ImageIcon(getClass().getResource("nameless.png"));
imageLogo = new JLabel(logo);
imageLogo.setBounds(110, 25, 250, 40);

add(username);
add(userField);
add(register);
add(password);
add(passField);
add(login);
add(imageLogo);
}

@Override
public void actionPerformed(ActionEvent e) {
String sql = "SELECT = FROM Users WHERE Username=? AND Password=?";

try{

ps = conn.prepareStatement(sql);
ps.setString(1, userField.getText());
ps.setString(2, passField.getText());
rs = ps.executeQuery();

if(rs.next()){
JOptionPane.showMessageDialog(null, "Logged In!", "Logged In", JOptionPane.INFORMATION_MESSAGE);
}else{
JOptionPane.showMessageDialog(null, "Invalid Detail.\nPlease Try Agian.", "Error", JOptionPane.ERROR_MESSAGE);
userField.setText("");
passField.setText("");
}

}catch(Exception a){
JOptionPane.showMessageDialog(null, a);
}
finally {
try{
rs.close();
ps.close();
}catch(Exception a) {

}
}
}
}

主要陈述

import javax.swing.JFrame;

public class MainStatement {
public static void main(String Args[]) {
Loader l = new Loader();
l.setVisible(true);
l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l.setResizable(false);
l.setSize(450, 200);
l.setTitle("Nameless™ Database");
}
}

数据库类

import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;

public class DBConnect {

public static Connection ConnectDB(){
try{
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\YellowMilk\\Folders\\Private\\Java\\NamelessDatabase.sqli​te");
return conn;
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}

1 个答案:

答案 0 :(得分:8)

"SELECT = FROM Users WHERE Username=? AND Password=?"

第一个=语法错误。它应该是这样的

"SELECT 1 FROM Users WHERE Username=? AND Password=?"

由于您不关心行中的内容,您只想计算它们的行数,因此每行返回一个虚拟数字1。

如果你想要该行的所有字段,那么你可以这样做:

"SELECT * FROM Users WHERE Username=? AND Password=?"

=是语法错误,您不能在那里=