Reputation: 73
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.
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.sqlite");
return conn;
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
Upvotes: 3
Views: 14261
Reputation: 21773
"SELECT = FROM Users WHERE Username=? AND Password=?"
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.
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.
Upvotes: 9