📄 productupdate.java
字号:
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ProductUpdate extends JFrame {
JLabel lbName,lbFuzeren,lbDepartment,lbDanwei,lbDanjia,lbDemo;
JTextField jtName,jtFuzeren,jtDepartment,jtDanwei,jtDanjia,jtDemo;
JButton clear,ok,update,delete,cancel;
private DB_Manager db= new DB_Manager();
ResultSet rs;
private void makeGUI(){
lbName = new JLabel("名称:",JLabel.RIGHT);
lbFuzeren = new JLabel("负责人:",JLabel.RIGHT);
lbDepartment = new JLabel("所属部门:",JLabel.RIGHT);
lbDanwei = new JLabel("单位:",JLabel.RIGHT);
lbDanjia = new JLabel("单价:",JLabel.RIGHT);
lbDemo = new JLabel("描述:",JLabel.RIGHT);
jtName = new JTextField(10);
jtFuzeren= new JTextField(10);
jtDepartment= new JTextField(10);
jtDanwei= new JTextField(10);
jtDanjia= new JTextField(10);
jtDemo= new JTextField(10);
clear = new JButton("清空");
ok = new JButton("确定");
update = new JButton("更新");
delete = new JButton("删除");
cancel = new JButton("取消");
Container cp = getContentPane();
JPanel jp = new JPanel();
JPanel jp2 = new JPanel();
jp.setLayout(new GridLayout(6,2,20,18));
//jp.setLayout(new FlowLayout());
jp2.setLayout(new FlowLayout());
jp.add(lbName);jp.add(jtName);
jp.add(lbFuzeren);jp.add(jtFuzeren);
jp.add(lbDepartment);jp.add(jtDepartment);
jp.add(lbDanwei); jp.add(jtDanwei);
jp.add(lbDanjia); jp.add(jtDanjia);
jp.add(lbDemo); jp.add(jtDemo);
cp.setLayout(new BorderLayout());
jp2.add(clear);
jp2.add(ok);
jp2.add(update);
jp2.add(delete);
jp2.add(cancel);
cp.add(jp,BorderLayout.CENTER);
cp.add(new JLabel(" "),BorderLayout.EAST);
cp.add(jp2,BorderLayout.SOUTH);
}
private void addAction(){
clear.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
jtName.setText("");
jtFuzeren.setText("");
jtDepartment.setText("");
jtDanwei.setText("");
jtDanjia.setText("");
jtDemo.setText("");
}
});
ok.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
String strSQL ;
//生成sql语句
strSQL="select * from productinfo where Name='"+jtName.getText().trim() +"' " ;
//由DBManager对象执行过程,若成功返回成功信息,若失败返回失败提示
rs=db.getResult(strSQL) ;
//判断结果集是否为空
boolean isexist=false;
try {
isexist = rs.first();
}
catch (SQLException ex1) {
}
//若为空,则说明产品不存在,弹出警告信息
if(!isexist)
{JOptionPane.showMessageDialog(null,"此产品不存在!");
}
//若不为空,则说明产品存在
else
{
try {
//将数据集中的数据显示在对应文本框中
rs.first();
jtFuzeren.setText(rs.getString("Principal") );
jtDepartment.setText(rs.getString("Department"));
jtDanwei.setText(rs.getString("Units"));
jtDanjia.setText(rs.getString("Unitprice"));
jtDemo.setText(rs.getString("Comment"));
}
catch (SQLException ex) {
}
}
}
});
update.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
String sql;
if(jtName.getText().trim().equals("")){
JOptionPane.showMessageDialog(null,"产品名称不可为空,请确认");
return;
}
if(jtFuzeren.getText().trim().equals("")){
JOptionPane.showMessageDialog(null,"负责人不可为空,请确认");
return;
}
if(jtDepartment.getText().trim().equals("")){
JOptionPane.showMessageDialog(null,"产品所属部门不可为空,请确认");
return;
}
sql = "insert into productinfo(Name,Principal,Department,Units,Unitprice,Comment) values('";
sql += jtName.getText().trim()+"','";
sql+= jtFuzeren.getText().trim() +"','";
sql += jtDepartment.getText().trim() +"','";
sql += jtDanwei.getText().trim() +"','";
sql += jtDanjia.getText().trim() +"','";
sql+=jtDemo.getText().trim()+"')";
if(db.executeSql(sql)){
JOptionPane.showMessageDialog(null,"成功更新");}
else{
JOptionPane.showMessageDialog(null,"更新失败,请重新操作!!!");}
}
});
delete.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
String strSQL ;
//生成sql语句
strSQL="delete from productinfo where Name='"+jtName.getText().trim() +"' " ;
//由DBManager对象执行过程,若成功返回成功信息,若失败返回失败提示
if(db.executeSql(strSQL))
{JOptionPane.showMessageDialog(null,"成功删除!"); }
else
{ JOptionPane.showMessageDialog(null," 删除失败,请重新操作!"); }
}
});
cancel.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
exit();
}
});
}
public void exit(){
this.dispose();
}
ProductUpdate(String title){
super(title);
makeGUI();
addAction();
addWindowListener(new WinLis());
}
class WinLis extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
exit();
}
}
public static void main(String[] args) {
ProductUpdate newAdd = new ProductUpdate("添加产品信息");
newAdd.setBounds(120,120,400,350);
newAdd.validate();
newAdd.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -