以下是我的代码。每当我执行它时,我的数据库中的所有字段都输入为空。我在所有字段中使用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);
}
}
您只能在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);
}
}
从表名和列名中删除单引号后尝试。
preparedStatement = connection.prepareStatement(“插入备件(商品代码,部件名称,数量,公司,保修,销售价格,购买价格,类别,说明)”值('“+ s1 +”','“+ s2 +”','“+ s3 +” ”, ' “+ S4 +”', ' “+ S5 +”', ' “+ S6 +”', ' “+ S7 +”', ' “+ S8 +”', ' “+ S9 +”')“);