1

My code :

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()); }

And i get errors like this:

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

Please, explain these problems for beginner in the easiest way. This code is for the add button, which would help to insert info into my table.

1
  • remove the last , in '"+Premija+"', Commented Dec 21, 2015 at 14:20

1 Answer 1

1

Remove the comma before your closing bracket:

Premija+"',)";

becomes

Premija+"')";

Except don't build your query by hand, unless you want to be vulnerable to SQL injection attacks: use a PreparedStatement.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.