java.sql。SQLException:[SQLITE_ERROR] SQL错误或缺失数据库(接近" = ":语法错误)

时间:2022-02-16 01:37:41

I made the sqlite database using the firefox web development thing, saved it to my pc but I ran into some problem while linking the database. Whenever I click the login button to test I get "java.sql.SQLException:[SQLITE_ERROR] SQL error or missing database (near "=":syntax error)". He advise me to change the "//" to "\" within the database class and see if it worked but it didn't so if someone could look at the code and help me out it would greatly appreciated.

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

code for loader

代码加载程序

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) {

}
}
}
}

main statement

主要的声明

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");
}
}

database class

数据库类

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 个解决方案

#1


8  

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

"SELECT = FROM Users WHERE Username=? "和密码= ?”

The first = is a syntax error. It should be like this

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

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

从用户名=?和密码= ?”

Since you don't care about what's in the rows, you just want to count how many rows they are, so you return a dummy number 1 per row returned.

因为你不关心行中有什么,你只需要计算它们有多少行,所以你返回一个每一行1的虚拟数字。

If you want all of the fields of the row then you'd do this:

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

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

“从用户名=?”和密码= ?”

But = is a syntax error, you can't have = there.

但是=是一个语法错误,不能有=。

#1


8  

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

"SELECT = FROM Users WHERE Username=? "和密码= ?”

The first = is a syntax error. It should be like this

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

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

从用户名=?和密码= ?”

Since you don't care about what's in the rows, you just want to count how many rows they are, so you return a dummy number 1 per row returned.

因为你不关心行中有什么,你只需要计算它们有多少行,所以你返回一个每一行1的虚拟数字。

If you want all of the fields of the row then you'd do this:

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

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

“从用户名=?”和密码= ?”

But = is a syntax error, you can't have = there.

但是=是一个语法错误,不能有=。