2016-07-25 34 views
1

我正在開發一個具有3個JFrame的基本程序。成功登錄後將會打開登錄,註冊和儀表板。但是,輸入用戶名和密碼並單擊登錄按鈕後,我收到錯誤消息。Java和MySql中的SQL語法錯誤SELECT查詢

這裏的錯誤:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' password='1234'' at line 1

這裏是我的代碼:

import java.awt.BorderLayout; 
import java.awt.EventQueue; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 

import com.mysql.jdbc.Statement; 

import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.ImageIcon; 
import java.awt.Font; 
import javax.swing.JTextField; 
import javax.swing.JButton; 
import java.awt.event.ActionListener; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.awt.event.ActionEvent; 

public class Login extends JFrame { 

private JPanel contentPane; 
private JTextField txtUsrName; 
private JTextField txtPAss; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       Login frame = new Login(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public Login() { 
    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 
    setBounds(100, 100, 450, 348); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    contentPane.setLayout(null); 

    JLabel lblLogin = new JLabel("Welcome To TechApp"); 
    lblLogin.setFont(new Font("Tekton Pro", Font.PLAIN, 18)); 
    lblLogin.setBounds(135, 19, 163, 28); 
    contentPane.add(lblLogin); 

    JLabel lblUsername = new JLabel("UserName:"); 
    lblUsername.setFont(new Font("Alaska", Font.PLAIN, 15)); 
    lblUsername.setBounds(174, 58, 88, 28); 
    contentPane.add(lblUsername); 

    txtUsrName = new JTextField(); 
    txtUsrName.setBounds(145, 90, 132, 20); 
    contentPane.add(txtUsrName); 
    txtUsrName.setColumns(10); 

    JLabel lblPassword = new JLabel("Password:"); 
    lblPassword.setFont(new Font("Alaska", Font.PLAIN, 15)); 
    lblPassword.setBounds(182, 118, 95, 46); 
    contentPane.add(lblPassword); 

    txtPAss = new JTextField(); 
    txtPAss.setColumns(10); 
    txtPAss.setBounds(145, 156, 132, 20); 
    contentPane.add(txtPAss); 

    JButton btnNewButton = new JButton("login"); 
    btnNewButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 


      String _username = txtUsrName.getText(); 
      String _password = txtPAss.getText(); 
      String url = "jdbc:mysql://127.0.0.1:3306/javabase"; 
      String user = "java"; 
      String passw = "password"; 

      try{ 
       // 1.Get a connection To Database 
       Connection myConn = DriverManager.getConnection(url, user, passw); 

       // 2.Create a statement 
       Statement myStmt = (Statement) myConn.createStatement(); 

       // 3.Execute SQL Query 
       String sql = "SELECT userame, password FROM registration WHERE userame='"+_username+"', password='"+_password+"' "; 
       ResultSet result = myStmt.executeQuery(sql); 
       //myStmt.executeUpdate(sql); 

       int count = 0; 
       while(result.next()){ 
        count = count + 1; 
       } 
       if(count == 1){ 
        Dashboard frame = new Dashboard(); 
        frame.setVisible(true); 
       } 
       else if(count > 1){ 
        JOptionPane.showMessageDialog(null, "Duplicate User! Access Denied!"); 
       } 
       else{ 
        JOptionPane.showMessageDialog(null, "User Not Found!"); 
       } 


      } 
      catch(Exception ex) 
      { 
       ex.printStackTrace(); 
      } 





     } 
    }); 
    btnNewButton.setBounds(169, 202, 89, 49); 
    contentPane.add(btnNewButton); 

    JButton btnRegister = new JButton("Register"); 
    btnRegister.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      Main frame = new Main(); 
      frame.setVisible(true); 
     } 
    }); 
    btnRegister.setBounds(168, 264, 89, 23); 
    contentPane.add(btnRegister); 

    JLabel lblNewLabel = new JLabel(""); 
    lblNewLabel.setFont(new Font("Alaska", Font.PLAIN, 16)); 
    lblNewLabel.setIcon(new ImageIcon("D:\\ExploitGate\\MAS-9831-Offwhite2.jpg")); 
    lblNewLabel.setBounds(0, 0, 434, 310); 
    contentPane.add(lblNewLabel); 
} 
} 

我搜索了計算器論壇,並進行給出可能的解決方案here 任何人都可以請指導我如何處理這個錯誤? 在此先感謝:)

+0

我認爲你需要使用'密碼= ' 「+ _ +密碼」' ' 「';而不是'密碼='」 + _password +「'」;' - 注意「密碼」值末尾缺少關閉的「」字符。 – Castaglia

回答

1

您在WHERE子句之間使用逗號,而不是AND

String sql =「SELECT userame,password FROM registered WHERE userame ='」+ _ username +「'AND password ='」+ _ password +「'」;

+0

非常感謝:)它解決了這個問題。 –

+0

非常歡迎。 –

3

上述所有的代碼基本上是無用的。這是一個SQL語法錯誤,這意味着它的這一行:

... WHERE userame='"+_username+"', password='"+_password+"' "; 
           ^--- 

你不使用,分離where條款參數。您使用布爾操作。 andor等..

並注意你容易受到sql injection attacks

+0

非常感謝:)它解決了這個問題。 –

+0

是的,我知道,我剛學過Java中的數據庫連接。所以現在我將重點放在安全性方面。感謝tho :) –