Search code examples
javamysqlmssql-jdbc

Here is my code. When I execute it an empty string is inserted into the database. However, I am using varchar 20 for all fields in the Database


Below is my code. Whenever I execute it, all the fields in my database are entered empty. I am using varchar20 for all the fields and I have made all fields not to accept null.

When I use:

preparedStatement=connection.prepareStatement("insert into 'spareparts'('itemcode','partname','quantity','company','warranty','salesprice','purchaseprice','category','description') values ('"+s1+"','"+s2+"','"+s3+"','"+s4+"','"+s5+"','"+s6+"','"+s7+"','"+s8+"','"+s9+"')");

instead of:

preparedStatement=connection.prepareStatement("insert into spareparts values('"+s1+"','"+s2+"','"+s3+"','"+s4+"','"+s5+"','"+s6+"','"+s7+"','"+s8+"','"+s9+"')");

I get many errors like abstractbutton, nullpointerexception

package package1;
import com.mysql.jdbc.Statement;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class AddProductGUI{
Connection connection;
PreparedStatement preparedStatement;
ResultSet resultSet;
Statement statement;


    JFrame frame;
    AbstractButton submitButton;
    JTextField itemcodetf, partnametf, 
            quantitytf, companytf, 
            warrantytf, salespricetf, 
            purchasepricetf, categorytf, 
            descriptiontf;
    JLabel itemcodelbl, partnamelbl,
            quantitylbl, companylbl,
            warrantylbl, salespricelbl,
            purchasepricelbl, categorylbl,
            descriptionlbl;
    public AddProductGUI()
    {
        initAPGUI();
    }
    public void initAPGUI() 
    {
        frame=new JFrame("SSSS Traders Add_Product_GUI");
        FlowLayout fl= new FlowLayout();
        frame.setLayout(fl);
        itemcodetf=new JTextField(20);
        itemcodelbl=new JLabel("Item Code");
        partnametf=new JTextField(20);
        partnamelbl=new JLabel("Part Name");
        quantitytf=new JTextField(20);
        quantitylbl=new JLabel("  Quantity");
        companytf=new JTextField(20);
        companylbl=new JLabel("Company");
        warrantytf=new JTextField(20);
        warrantylbl=new JLabel("Warranty");
        salespricetf=new JTextField(20);
        salespricelbl=new JLabel("Sales Price");
        purchasepricetf=new JTextField(20);
        purchasepricelbl=new JLabel("Purchase Price");
        categorytf=new JTextField(20);
        categorylbl=new JLabel("Category");
        descriptiontf=new JTextField(20);
        descriptionlbl=new JLabel("Description");
        submitButton =new JButton("Submit");
        frame.add(itemcodelbl);
        frame.add(itemcodetf);

        frame.add(partnamelbl);
        frame.add(partnametf);

        frame.add(quantitylbl);
        frame.add(quantitytf);

        frame.add(companylbl);
        frame.add(companytf);

        frame.add(warrantylbl);
        frame.add(warrantytf);

        frame.add(salespricelbl);
        frame.add(salespricetf);

        frame.add(purchasepricelbl);
        frame.add(purchasepricetf);

        frame.add(categorylbl);
        frame.add(categorytf);

        frame.add(descriptionlbl);
        frame.add(descriptiontf);


        String s1=itemcodetf.getText().toString();
        String s2=partnametf.getText().toString();
        String s3=quantitytf.getText().toString();
        String s4=companytf.getText().toString();
        String s5=warrantytf.getText().toString();
        String s6=salespricetf.getText().toString();
        String s7=purchasepricetf.getText().toString();
        String s8=categorytf.getText().toString();
        String s9=descriptiontf.getText().toString();


        frame.add(submitButton);

        submitButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try
            {
                Class.forName("com.mysql.jdbc.Driver");
                connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","");
                //preparedStatement=connection.prepareStatement("insert into 'spareparts' ('itemcode','partname','quantity','company','warranty','salesprice','purchaseprice','category','description') values ('"+s1+"','"+s2+"','"+s3+"','"+s4+"','"+s5+"','"+s6+"','"+s7+"','"+s8+"','"+s9+"')");
                preparedStatement=connection.prepareStatement("insert into spareparts values('"+s1+"','"+s2+"','"+s3+"','"+s4+"','"+s5+"','"+s6+"','"+s7+"','"+s8+"','"+s9+"')");

                preparedStatement.execute();                           
            }
            catch (SQLException ex) {
                // TODO Auto-generated catch block
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(AddProductGUI.class.getName()).log(Level.SEVERE, null, ex);
            }   
        }
    });
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setSize(300, 600);
        frame.setLocation(500, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}


Solution

  • You only assign values to s1 .. s9 in the initAPGUI() method. At that time, the JTextField objects are still empty (user hasn't even seen the yet).

    Move the statements assigning to s1 .. s9 into the actionPerformed() method, and change them to be local variables, not fields.

    Also, do not use string concatenation with user-supplied values to build a SQL statement. You're getting a PreparedStatement so use it!!

    And you should use try-with-resources:

    public void actionPerformed(ActionEvent e) {
        String sql = "insert into spareparts (itemcode, partname, quantity, company, warranty," +
                                            " salesprice, purchaseprice, category, description)" +
                    " values (?, ?, ?, ?, ?, ?, ?, ?, ?)";
        try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "")) {
            try (PreparedStatement stmt = conn.prepareStatement(sql)) {
                stmt.setString(1, itemcodetf.getText().toString());
                stmt.setString(2, partnametf.getText().toString());
                stmt.setString(3, quantitytf.getText().toString());
                stmt.setString(4, companytf.getText().toString());
                stmt.setString(5, warrantytf.getText().toString());
                stmt.setString(6, salespricetf.getText().toString());
                stmt.setString(7, purchasepricetf.getText().toString());
                stmt.setString(8, categorytf.getText().toString());
                stmt.setString(9, descriptiontf.getText().toString());
                stmt.executeUpdate();
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AddProductGUI.class.getName()).log(Level.SEVERE, null, ex);
        }   
    }