Ниже мой код. Всякий раз, когда я выполняю его, все поля в моей базе данных вводятся пустыми. Я использую 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+"')");
Я получаю много ошибок, таких как 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);
}
}