2013-06-06 61 views
3

我做了sqlite數據庫使用Firefox的Web開發的東西,保存到我的電腦,但我遇到了一些問題,而鏈接數據庫。每當我點擊登錄按鈕來測試我得到「java.sql.SQLException:[SQLITE_ERROR] SQL錯誤或缺少數據庫(靠近」=「:語法錯誤)」。他建議我在數據庫類中將「//」更改爲「\」,看看它是否有效,但是如果有人可以查看代碼並幫助我,將不勝感激。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; 
} 
} 
} 

回答

8

代碼

第一個=是語法錯誤。它應該是這樣的

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

既然你不關心什麼是在行,你只是想指望他們中有多少行,所以你每行返回僞號碼1返回。

如果希望所有行的字段,那麼你可以這樣做:

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

=是一個語法錯誤,你不能有=那裏。

+0

非常感謝你,這是唯一的問題大聲笑我希望我可以upvote你的答案,但我需要15代表。時間更多的編碼:) –

相關問題