Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.sqli​te");
return conn;
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
share|improve this question

1 Answer

up vote 2 down vote accepted

"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.

share|improve this answer
thank you so much that was the only problem lol I wish I could upvote your answer but i need 15 rep. time for more coding :) – Mike Nguyen 12 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.