Java 为什么我的代码停止了?
我正在用JFrame测试我的代码,在它运行并输入值之后,它既不会将值发送到我的MS sql management studio,也不会崩溃。我已经在代码的submit按钮中创建了连接,我也尝试在处理插入和相同结果的类中创建连接。Insert类中的代码在没有JFrame的情况下运行良好Java 为什么我的代码停止了?,java,swing,Java,Swing,我正在用JFrame测试我的代码,在它运行并输入值之后,它既不会将值发送到我的MS sql management studio,也不会崩溃。我已经在代码的submit按钮中创建了连接,我也尝试在处理插入和相同结果的类中创建连接。Insert类中的代码在没有JFrame的情况下运行良好 public void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 450, 345);
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
请问您将
btnNewButton
添加到actuell面板的何处?因为您的代码不清楚如何将提交按钮放入面板。(甚至任何swing元素)您是否尝试过使用调试器逐步检查代码以查看它挂起在哪一行?请在您的catch(NumberFormatException n)
中执行一些操作,您可能不希望出现异常,而且不知道——特别是当你试图找出代码的问题时;试试com.microsoft.jdbc.sqlserver.SQLServerDriver
。