我正在使用JFrame测试我的代码,在它运行并输入值之后,它既没有将值发送到我的MS sql管理工作室,也没有崩溃。我已经在我的代码的提交按钮中创建了连接,我已经尝试在类中创建处理插入的连接以及相同的结果。 Insert类中的代码在没有JFrame的情况下工作正常。
public void initialize()
{
frame = new JFrame();
frame.setBounds(100, 100, 450, 345);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblChasis = new JLabel("Chasis num:");
JLabel lblEngNum = new JLabel("Engine num:");
JLabel lblMake = new JLabel("Make:");
JLabel lblModel = new JLabel("Model:");
JLabel lblYear = new JLabel("Year od car:");
JLabel lblCapacity = new JLabel("Capacity:");
JLabel lblSupplier = new JLabel("Supplier ID:");
JLabel lblStatus = new JLabel("Status:");
txtChasisNum = new JTextField();
txtChasisNum.setColumns(10);
txtEngNum = new JTextField();
txtEngNum.setColumns(10);
txtYear = new JTextField();
txtYear.setColumns(10);
txtModel = new JTextField();
txtModel.setColumns(10);
txtMake = new JTextField();
txtMake.setColumns(10);
txtStatus = new JTextField();
txtStatus.setColumns(10);
txtSupplier = new JTextField();
txtSupplier.setColumns(10);
JButton btnNewButton = new JButton("Submit");
btnNewButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
Insert I = new Insert();
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con=DriverManager.getConnection("jdbc:sqlserver://DEEZY\\SQLEXPRESS;user=Java1234;password=barcelona;database=car_inventory");
chasisNnum = txtChasisNum.getText();
engNum = txtEngNum.getText();
year_of_car = txtYear.getText();
model = txtModel.getText();
make = txtMake.getText();
capacity = txtCapacity.getText();
description = txtDescription.getText();
statTxt = txtStatus.getText();
sup_id = Integer.parseInt(statTxt);
supTxt =txtSupplier.getText();
status = Integer.parseInt(supTxt);
I.insertVehicle(engNum, chasisNnum, make, model, year_of_car, capacity, sup_id, status, description,con);
}
catch (NumberFormatException n)
{
}
catch(Exception e1)
{
e1.printStackTrace();
}
}
});
JLabel lblDescription = new JLabel("Description");
txtDescription = new JTextField();
txtDescription.setColumns(10);
txtCapacity = new JTextField();
txtCapacity.setColumns(10);
);
frame.getContentPane().setLayout(groupLayout);
frame.add(btnSubmit);
}//end of Initialize
来自插入类的方法:
public void insertVehicle(String engNum, String chasisNum,String make,String model,String year,String capacity,int supId,int status,String description,Connection Con)
{
try
{
CallableStatement ct = Con.prepareCall("{Call addVehicle(?,?,?,?,?,?,?,?,?)}");
ct.setString(1, engNum);
ct.setString(2,chasisNum);
ct.setString(3,make);
ct.setString(4,model);
DateFormat format = new SimpleDateFormat("dd-mm-yyyy",Locale.ENGLISH);
Date dt = format.parse(year);
ct.setDate(5, new java.sql.Date(dt.getTime()));
ct.setString(6,capacity);
ct.setInt(7, supId);
ct.setInt(8, status);
ct.setString(9,description);
System.out.println(ct.execute());
}//end of try
catch(Exception e)
{
e.printStackTrace();
}// end of catch
}//end of insert into vehile