2015-12-21 130 views
1

我的代碼:SQL語法錯誤

try{ Class.forName("com.mysql.jdbc.Driver"); 
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/lentele", "root", ""); 
String select = "SELECT * FROM darbuotojai WHERE 1"; 

    String ID = infoID.getText(); 
    String Vardas = infoV.getText(); 
    String Pavardė = infoP.getText(); 
    String Pareigos = infoPar.getSelectedItem().toString(); 
    String Alga = infoAlg.getText(); 
    String Premija = infoPre.getText(); 

    String insert = "INSERT INTO `darbuotojai`(`ID`, `Vardas`, `Pavardė`, `Pareigos`, `Alga`, `Premija`) VALUES ('"+ID+"','"+Vardas+"','"+Pavardė+"','"+Pareigos+"','"+Alga+"','"+Premija+"',)"; 


     stm.executeUpdate(insert); 
     JOptionPane.showMessageDialog(null, "Užklausa sėkminga"); 
     infoID.setText(""); 
     infoV.setText(""); 
     infoP.setText(""); 
     infoPar.setSelectedItem(""); 
     infoAlg.setText(""); 
     infoPre.setText(""); 

     display(); 
    } catch (Exception e) {JOptionPane.showMessageDialog(null, e.getMessage()); } 

,我得到象這樣的錯誤:

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 ')' at line 1. 
And ones more: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user ''@'localhost' to database 'lentele'. 

請解釋這些問題在最簡單的方法初學者。 此代碼用於添加按鈕,這將有助於將信息插入到我的表中。

+0

刪除最後一個','在''「+ Premija +」',' –

回答

1

你的右括號之前刪除逗號:

Premija+"',)"; 

成爲

Premija+"')"; 

除了不要用手構建查詢,除非你想成爲容易受到SQL注入攻擊:使用PreparedStatement

+0

你告訴我是對的。 –