Stevoski
Stevoski

Reputation: 11

Java derby database not working

Is there any issue with these lines of code? All I get is "invalid entry". I have a database called production with a table called PRODUCTION.

try {
    String mk = jTextField1.getText();
    String mn = jTextField2.getText();
    String ab = (String) jComboBox1.getSelectedItem();
    String bc = (String) jComboBox2.getSelectedItem();

    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:1527/production");
        {
            String host = "jdbc:mysql://localhost:1527/production";
            JOptionPane.showMessageDialog(this, "connection success");
            Statement stmt = con.createStatement();

            String query = "update PRODUCT set FACTORY='" + ab + "' PRODUCT_NAME = '" + mk + "' UNIT= '" + bc + "' and OPENING_BALANCE'" + mn + "');";
            stmt.executeUpdate(query);
            JOptionPane.showMessageDialog(this, "Record has been inserted");
            stmt.close();
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "invalid entry");
    }
} catch (Exception e) {
    JOptionPane.showMessageDialog(null, "Error in Connectivity", "Message", 2);
}

Upvotes: 0

Views: 109

Answers (1)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59950

Your query is not correct :

  1. You have to use , between the fields
  2. You don't need to use and when you set the fields (and OPENING_BALANCE'" + mn + "')
  3. In the end of your query there are a closed parenthesis ) that you don't need it

But your way is open to sytax error and SQL Inection you have to use PreparedStatement instead, it is more secure and more helpful :

query = "update PRODUCT set FACTORY = ?, PRODUCT_NAME = ?, UNIT= ?, OPENING_BALANCE = ?";
try (PreparedStatement update = connection.prepareStatement(query)) {

    update.setString(1, ab);
    update.setString(2, mk);
    update.setString(3, bc);
    update.setString(4, mn);

    update.executeUpdate();
}

Upvotes: 1

Related Questions