⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cant.java

📁 本程序涉及GUI编程和数据库操作
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	
	public demand(Frame owner, String title, boolean modal)
	{
		super(owner,title,modal);
		
		Container c=getContentPane();
		c.setLayout(new GridBagLayout());
		GridBagConstraints gbc=new GridBagConstraints();
		gbc.insets=new Insets(10,10,10,10);
		gbc.weightx=0.5;
		gbc.weighty=0.5;
		
		gbc.gridx=0;
		gbc.gridy=0;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.WEST;
		ln=new JLabel("请输入商品名称");
		c.add(ln,gbc);
		
		gbc.gridx=1;
		gbc.gridy=0;
		gbc.gridwidth=2;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;
		tfn=new JTextField(20);
		c.add(tfn,gbc);
		
		gbc.gridx=1;
		gbc.gridy=1;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;
		bt=new JButton("提交");
		c.add(bt,gbc);
		bt.addActionListener(this);
		
		gbc.gridx=2;
		gbc.gridy=1;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;
		btqu=new JButton("取消");
		c.add(btqu,gbc);
		btqu.addActionListener(this);
		
		this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		this.setBounds(150,150,400,200);
		this.show();
	}
	
	public void actionPerformed(ActionEvent event)
	{
		String command=event.getActionCommand();
		if(command.equals("提交"))
		{
			String name="",price="",amount="";
			name=tfn.getText();
			String sql="SELECT * FROM t1 WHERE 商品名称='"+name+"'";
			
     		try{
                	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
					
					Connection con=DriverManager.getConnection("jdbc:odbc:cantdb");
					
					Statement state=con.createStatement();
					
					ResultSet rs=state.executeQuery(sql);
                	
                	int n=0;
                	while(rs.next())
                	{
                 		name=rs.getString("商品名称");
                 		price=rs.getString("商品价格");
                 		amount=rs.getString("库存数量");
                 		n++;
                	}
                	if(n==0)
                	{
                		JOptionPane.showMessageDialog(this,"没有查到相关资料");
                	}
                	else
                	{
                		mainw.tfn.setText(name);
             			mainw.tfp.setText(price);
             			mainw.tfa.setText(amount);
             		}
                	con.close();
               }
           catch(Exception ex)
               {
                	JOptionPane.showMessageDialog(this,"查询失败");
               }
               this.dispose();
        }
		if(command.equals("取消"))
		{
			this.dispose();
		}
	}
}	

//删除模块
class delete extends JDialog implements ActionListener
{
	
	private JLabel ln;
	private JTextField tfn;
	private JButton bt,btqu;
	
	public delete(Frame owner, String title, boolean modal)
	{
		super(owner,title,modal);
		
		Container c=getContentPane();
		c.setLayout(new GridBagLayout());
		GridBagConstraints gbc=new GridBagConstraints();
		gbc.insets=new Insets(10,10,10,10);
		gbc.weightx=0.5;
		gbc.weighty=0.5;
		
		gbc.gridx=0;
		gbc.gridy=0;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.WEST;
		ln=new JLabel("请输入要删除的商品名称");
		c.add(ln,gbc);
		
		gbc.gridx=1;
		gbc.gridy=0;
		gbc.gridwidth=2;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;
		tfn=new JTextField(20);
		c.add(tfn,gbc);
		
		gbc.gridx=1;
		gbc.gridy=1;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;
		bt=new JButton("提交");
		c.add(bt,gbc);
		bt.addActionListener(this);
		
		gbc.gridx=2;
		gbc.gridy=1;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;
		btqu=new JButton("取消");
		c.add(btqu,gbc);
		btqu.addActionListener(this);
		
		this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		this.setBounds(130,150,450,200);
		this.show();
	}
	
	public void actionPerformed(ActionEvent event)
	{
		String command=event.getActionCommand();
		if(command.equals("提交"))
		{
			String name="";
			name=tfn.getText();
			String sql="DELETE FROM t1 WHERE 商品名称='"+name+"'";
			
     		try{
                	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
					
					Connection con=DriverManager.getConnection("jdbc:odbc:cantdb");
					
					PreparedStatement ps=con.prepareStatement(sql);
					
					ps.executeUpdate();
                	
                	JOptionPane.showMessageDialog(this,"删除成功");
                	
                	con.close();
               }
           catch(Exception ex)
               {
                	JOptionPane.showMessageDialog(this,"查询失败");
               }
               this.dispose();
        }
		if(command.equals("取消"))
		{
			this.dispose();
		}
	}
}	

//修改模块
class amend extends JDialog implements ActionListener
{
	
	private JLabel lnc,ln,lp,la;
	private JTextField tfnc,tfn,tfp,tfa;
	private JButton btc,bt,btqu;
	
	public amend(Frame owner, String title, boolean modal)
	{
		super(owner,title,modal);
		
		Container c=getContentPane();
		c.setLayout(new GridBagLayout());
		GridBagConstraints gbc=new GridBagConstraints();
		gbc.insets=new Insets(10,10,10,10);
		gbc.weightx=0.5;
		gbc.weighty=0.5;
		
		gbc.gridx=0;
		gbc.gridy=0;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.WEST;
		lnc=new JLabel("请输入要修改的商品名称");
		c.add(lnc,gbc);
		
		gbc.gridx=1;
		gbc.gridy=0;
		gbc.gridwidth=2;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;
		tfnc=new JTextField(20);
		c.add(tfnc,gbc);
		
		gbc.gridx=1;
		gbc.gridy=1;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;
		btc=new JButton("查询");
		c.add(btc,gbc);
		btc.addActionListener(this);
		
		gbc.gridx=0;
		gbc.gridy=2;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.WEST;
		ln=new JLabel("商品名称");
		c.add(ln,gbc);
		
		gbc.gridx=1;
		gbc.gridy=2;
		gbc.gridwidth=2;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;
		tfn=new JTextField(20);
		c.add(tfn,gbc);
		
		gbc.gridx=0;
		gbc.gridy=3;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.WEST;
		lp=new JLabel("商品价格");
		c.add(lp,gbc);
		
		gbc.gridx=1;
		gbc.gridy=3;
		gbc.gridwidth=2;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;
		tfp=new JTextField(20);
		c.add(tfp,gbc);
		
		gbc.gridx=0;
		gbc.gridy=4;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.WEST;
		la=new JLabel("库存数量");
		c.add(la,gbc);
		
		gbc.gridx=1;
		gbc.gridy=4;
		gbc.gridwidth=2;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;
		tfa=new JTextField(20);
		c.add(tfa,gbc);
		
		gbc.gridx=1;
		gbc.gridy=5;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;
		bt=new JButton("提交");
		c.add(bt,gbc);
		bt.addActionListener(this);
		
		gbc.gridx=2;
		gbc.gridy=5;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;
		btqu=new JButton("取消");
		c.add(btqu,gbc);
		btqu.addActionListener(this);
		
		this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		this.setBounds(150,150,500,500);
		this.show();
	}
	
	public void actionPerformed(ActionEvent event)
	{
		String command=event.getActionCommand();
		if(command.equals("查询"))
		{
			String name="",price="",amount="";
			name=tfnc.getText();
			String sql="SELECT * FROM t1 WHERE 商品名称='"+name+"'";
			
     		try{
                	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
					
					Connection con=DriverManager.getConnection("jdbc:odbc:cantdb");
					
					Statement state=con.createStatement();
					
					ResultSet rs=state.executeQuery(sql);
                	
                	int n=0;
                	while(rs.next())
                	{
                 		name=rs.getString("商品名称");
                 		price=rs.getString("商品价格");
                 		amount=rs.getString("库存数量");
                 		n++;
                	}
                	if(n==0)
                	{
                		JOptionPane.showMessageDialog(this,"没有查到相关资料");
                	}
                	else
                	{
                		tfn.setText(name);
             			tfp.setText(price);
             			tfa.setText(amount);
             		}
                	con.close();
               }
           catch(Exception ex)
               {
                	JOptionPane.showMessageDialog(this,"查询失败");
               }
        }
        if(command.equals("提交"))
		{
			String name="",price="",amount="",namec="";
			namec=tfnc.getText();
			name=tfn.getText();
			price=tfp.getText();
			amount=tfa.getText();
			String sql="UPDATE t1 SET 商品名称='"+name+"',商品价格='"+price+"',库存数量='"+amount+"'"+"WHERE 商品名称='"+namec+"'";
			
     		try{
                	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
					
					Connection con=DriverManager.getConnection("jdbc:odbc:cantdb");
					
					PreparedStatement ps=con.prepareStatement(sql);
					
					ps.executeUpdate();
                	
                	JOptionPane.showMessageDialog(this,"修改成功");
                	
                	con.close();
               }
           catch(Exception ex)
               {
                	JOptionPane.showMessageDialog(this,"查询失败");
               }
               this.dispose();
        }
		if(command.equals("取消"))
		{
			this.dispose();
		}
	}
}	

//退出模块
class exit extends JDialog implements ActionListener
{
	
	private JLabel lq;
	private JButton bt,btqu;
	
	public exit(Frame owner, String title, boolean modal)
	{
		super(owner,title,modal);
		
		Container c=getContentPane();
		c.setLayout(new GridBagLayout());
		GridBagConstraints gbc=new GridBagConstraints();
		gbc.insets=new Insets(10,10,10,10);
		gbc.weightx=0.5;
		gbc.weighty=0.5;
		
		gbc.gridx=0;
		gbc.gridy=0;
		gbc.gridwidth=2;
	    gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;
		lq=new JLabel("你确定要退出系统吗?");
		c.add(lq,gbc);
		
		gbc.gridx=0;
		gbc.gridy=1;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;
		bt=new JButton("确定");
		c.add(bt,gbc);
		bt.addActionListener(this);
		
		gbc.gridx=1;
		gbc.gridy=1;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;
		btqu=new JButton("取消");
		c.add(btqu,gbc);
		btqu.addActionListener(this);
		
		this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		this.setBounds(150,150,350,200);
		this.show();
	}
	
	public void actionPerformed(ActionEvent event)
	{
		String command=event.getActionCommand();
		if(command.equals("取消"))
		{
			this.dispose();
		}
		if(command.equals("确定"))
		{
			System.exit(0);
		}
	}
}	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -