这是我的代码.当我执行它时,一个空字符串入到数据库中.但是,我对数据库中的所有字段使用 varchar 20



下面是我的代码。每当我执行它时,数据库中的所有字段都输入为空。我对所有字段使用 varchar20,并且我已将所有字段设置为不接受空。

当我使用:

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+"')");

我收到很多错误,例如抽象按钮,空指针异常

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

}

您只将值分配给s1.. s9initAPGUI()方法中。那时,JTextField对象仍然是空的(用户甚至还没有看到(。

移动分配给s1的语句。 s9 actionPerformed() 方法,并将它们更改为局部变量,而不是字段。

此外,不要将字符串串联与用户提供的值一起使用来构建 SQL 语句。你得到一个PreparedStatement所以使用它!!

并且您应该使用试用资源:

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

相关内容

  • 没有找到相关文章

最新更新