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

时间:2019-04-22 04:32:34

标签: java mysql mssql-jdbc

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

当我使用时:

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

我遇到很多错误,例如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);
    }

}

2 个答案:

答案 0 :(得分:1)

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

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

此外,请勿将字符串与用户提供的值一起使用来构建SQL语句。您将获得PreparedStatement,请使用它!

并且您应该使用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);
    }   
}

答案 1 :(得分:0)

从表名和列名中删除单引号后尝试。

  

preparedStatement = connection.prepareStatement(“插入备件(项目代码,零件名称,数量,公司,保修,销售价格,购买价格,类别,说明)值('” + s1 +“','” + s2 +“','” + s3 +“','” + s4 +“','” + s5 +“','” + s6 +“','” + s7 +“','” + s8 +“','” + s9 +“')”));