importjava.awt.Container;importjava.awt.FlowLayout;importjava.awt.HeadlessException;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.SQLException;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JInternalFrame;importjavax.swing.JLabel;importjavax.swing.JOptionPane;importjavax.swing.JTextField;public class InsertStockFrame extends JFrame implementsActionListener{privateJTextField orderIdTextField; //JTextField表示的是文本框 详情https://zhidao.baidu.com/question/288180446.htmlprivateJTextField sNameTextField;privateJTextField consignmentDateTextField;privateJTextField baleNameTextField;privateJTextField countTextField;privateJTextField moneyTextField;privateJLabel orderLabel;//JLabel 对象可以显示文本、图像或同时显示二者。 详情http://outofmemory.cn/code-snippet/1121/swing-JLabel-explain-in-detail-yiji-usage-shiliprivateJLabel sNameLabel;privateJLabel dateLabel;privateJLabel baleNameLabel;privateJLabel countLabel;privateJLabel moneyLabel;privateJLabel starLabel;privateJButton confirmButton;privateJButton cancelButton;publicInsertStockFrame(){
setTitle("进货");
setSize(220, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//用户单击窗口的关闭按钮时程序执行的操作
Container cp= this.getContentPane();//this.getContentPane()的作用是初始化一个容器,用来在容器上添加一些控件
cp.setLayout(newFlowLayout());//setLayout是对当前组件设置为流式布局.组件在窗体中从左到右依次排列
orderLabel= new JLabel("订单号");
cp.add(orderLabel);
orderIdTextField= new JTextField(10);
cp.add(orderIdTextField);
starLabel= new JLabel("*");
cp.add(starLabel);
baleNameLabel= new JLabel("商品名称");
cp.add(baleNameLabel);
baleNameTextField= new JTextField(10);
cp.add(baleNameTextField);
starLabel= new JLabel("*");
cp.add(starLabel);
dateLabel= new JLabel("交货日期");
cp.add(dateLabel);
consignmentDateTextField= new JTextField(10);
cp.add(consignmentDateTextField);
starLabel= new JLabel("*");
cp.add(starLabel);
sNameLabel= new JLabel("客 户");
cp.add(sNameLabel);
sNameTextField= new JTextField(10);
cp.add(sNameTextField);
starLabel= new JLabel("*");
cp.add(starLabel);
countLabel= new JLabel("数 量");
cp.add(countLabel);
countTextField= new JTextField(10);
cp.add(countTextField);
starLabel= new JLabel("*");
cp.add(starLabel);
moneyLabel= new JLabel("金 额");
cp.add(moneyLabel);
moneyTextField= new JTextField(10);
cp.add(moneyTextField);
starLabel= new JLabel("*");
cp.add(starLabel);
confirmButton= new JButton("添加");
confirmButton.addActionListener(this);// 建立事件监听事件 https://blog.csdn.net/qq_41978199/article/details/80642012
cp.add(confirmButton);
cancelButton= new JButton("取消");
cancelButton.addActionListener(this);
cp.add(cancelButton);
}public voidactionPerformed(ActionEvent e) { //actionPerformed 用于接收操作事件的侦听器接口if(e.getActionCommand().equals("添加")){//从各控件取出数据
String orderId =orderIdTextField.getText();
String sName=sNameTextField.getText();
String date=consignmentDateTextField.getText();
String baleName=baleNameTextField.getText();
String count=countTextField.getText();
String money=moneyTextField.getText();//此处补充代码//非空或必填字段检测
if(orderId.equals("")){ //如果内容为空
JOptionPane.showMessageDialog(getContentPane(),"请将带星号的内容填写完整!","信息提示框", JOptionPane.INFORMATION_MESSAGE);return;
}if(sName.equals("")){
JOptionPane.showMessageDialog(getContentPane(),"请将带星号的内容填写完整!","信息提示框", JOptionPane.INFORMATION_MESSAGE);return;
}if(date.equals("")){
JOptionPane.showMessageDialog(getContentPane(),"请将带星号的内容填写完整!","信息提示框", JOptionPane.INFORMATION_MESSAGE);return;
}if(baleName.equals("")){
JOptionPane.showMessageDialog(getContentPane(),"请将带星号的内容填写完整!","信息提示框", JOptionPane.INFORMATION_MESSAGE);return;
}if(count.equals("")){
JOptionPane.showMessageDialog(getContentPane(),"请将带星号的内容填写完整!","信息提示框", JOptionPane.INFORMATION_MESSAGE);return;
}if(money.equals("")){
JOptionPane.showMessageDialog(getContentPane(),"请将带星号的内容填写完整!","信息提示框", JOptionPane.INFORMATION_MESSAGE);return;
}//此处补充代码//进货日期,数量,金额的合法性检验
int c = 0;doublem ;int flag = 0; //非常关键!! 不然程序会重复输出try{
c=Integer.parseInt(count);//Integer.parseInt(String)的作用就是将String字符类型数据转换为Integer整型数据
m=Double.parseDouble(money);
}catch(NumberFormatException t)
{
JOptionPane.showMessageDialog(getContentPane(),"请将带星号的内容填写完整!","信息提示框", JOptionPane.INFORMATION_MESSAGE);return;
}
JDBCUtil genConnection= newJDBCUtil() ;
Connection conn=genConnection.getConn();
String sql= "insert into stock(orderId,sName,consignmentDate,baleName,count,money)"+
" values(?,?,?,?,?,?)";
PreparedStatement preStmt= null;try{
preStmt=conn.prepareStatement(sql);
preStmt.setString(1, orderId);
preStmt.setString(2, sName);
preStmt.setString(3, date);
preStmt.setString(4, baleName);
preStmt.setInt(5, c);
preStmt.setDouble(6, m);
preStmt.executeUpdate();
System.out.println("成功插入数据"+ preStmt.getUpdateCount() + "条");
}catch(SQLException t) {
flag= 1;
t.printStackTrace();
}finally{
JDBCUtil.closeConn(preStmt, conn);
}try{if (flag == 0)
{
JOptionPane.showMessageDialog(getContentPane(),"插入成功","信息提示框", JOptionPane.INFORMATION_MESSAGE);
orderIdTextField.setText(""); //成功或失败以后将文本框清零
sNameTextField.setText("");
consignmentDateTextField.setText("");
baleNameTextField.setText("");
countTextField.setText("");
moneyTextField.setText("");
}else{
JOptionPane.showMessageDialog(getContentPane(),"插入失败","信息提示框", JOptionPane.INFORMATION_MESSAGE);
orderIdTextField.setText("");
sNameTextField.setText("");
consignmentDateTextField.setText("");
baleNameTextField.setText("");
countTextField.setText("");
moneyTextField.setText("");
}
}catch(HeadlessException t)
{
t.printStackTrace();
}
}else{
System.exit(0); //直接退出程序
}
}public static voidmain(String[] args){
InsertStockFrame insert= newInsertStockFrame(); //调用前面方法
insert.setVisible(true);
}
}
实现如下