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

📄 cant.txt

📁 可以实现商品的添加、删除、修改等的数据库操作
💻 TXT
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.*;
import java.sql.*;
import java.util.*;

//登陆模块
class login extends JFrame implements ActionListener
{
	
	private JLabel ly,lm;
	private JTextField tfy;
	private JPasswordField pfm;
	private JButton btq,btqu;
	
	public login(String title)
	{  
		super(title);
		Container c=getContentPane();
		c.setLayout(new GridBagLayout());//网格包布局
		c.setBackground(Color.pink);
		GridBagConstraints gbc=new GridBagConstraints();//利用类声明属性 
		gbc.insets=new Insets(10,10,10,10);//类Insets 它的是用来定义组件容器周围的空间大小,上左下右
		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;//西
		ly=new JLabel("用户名");
		c.add(ly,gbc);
	
		
		gbc.gridx=1;
		gbc.gridy=0;
		gbc.gridwidth=2;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;//中
		tfy=new JTextField(20);
		c.add(tfy,gbc);
		
		gbc.gridx=0;
		gbc.gridy=1;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.WEST;//西
		lm=new JLabel("密码");
		c.add(lm,gbc);
		
		gbc.gridx=1;
		gbc.gridy=1;
		gbc.gridwidth=2;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;//中
		pfm=new JPasswordField(20);
		c.add(pfm,gbc);
		
		gbc.gridx=1;
		gbc.gridy=2;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;
		btq=new JButton("登陆");
		c.add(btq,gbc);
		btq.addActionListener(this);
		
		gbc.gridx=2;
		gbc.gridy=2;
		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(JFrame.EXIT_ON_CLOSE);
		this.setBounds(100,100,350,350);
		this.show(); 
		
		try{
          		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		   }
       catch(ClassNotFoundException e)
       	   {
          		JOptionPane.showMessageDialog(null,"数据库连接失败");
           }
        try{
            	Connection con=DriverManager.getConnection("jdbc:odbc:cantdb");
            	Statement state=con.createStatement();
           }
       catch(SQLException e)
           {
            	JOptionPane.showMessageDialog(null,"数据库连接失败");
           }
	}
	
	public void actionPerformed(ActionEvent event)//按键监听事件
	{
		String command=event.getActionCommand();
		if(command.equals("登陆"))
		{
			String username="",passwords="",password="";
			username=tfy.getText();
			passwords=pfm.getText();
			String sql="SELECT password FROM t2 WHERE 用户名='"+username+"'";
			int y=0;
			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())
                	{
                 		password=rs.getString("password");
                 		n++;
                	}
                	if(n==0)
                	{
                		JOptionPane.showMessageDialog(this,"用户名错误");
                		y++;
                	}
                	con.close();
               }
           catch(Exception ex)
               {
                	JOptionPane.showMessageDialog(this,"查询失败");
               }
			if(passwords.equals(password))
			{
				this.dispose();
				mainw mw=new mainw("主窗口");
			}
			else
			{
				if(y==0)
				JOptionPane.showMessageDialog(this,"密码错误");
			}
		}
		if(command.equals("退出"))
		{
			System.exit(0);
		}
	}
	
	public static void main(String args[])
	{
		login log=new login("登陆");
	}
}

//主窗口
 class mainw extends JFrame implements ActionListener
{
	
	private JMenuBar mb;
	private JMenu function,system;
	private JMenuItem append,demand,exit,delete,amend,appendp,demandp,exitp,deletep,amendp;
	private JPopupMenu pm;
	static JLabel ln,lp,la;
	static JTextField  tfn,tfp,tfa;
	Connection con;
 	Statement st;
 	ResultSet r; 
	JTable table;
	
	private Connection getConnection() {
		try { 
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
			con= DriverManager.getConnection("jdbc:odbc:cantdb"); 			
		} 
		catch (ClassNotFoundException e) { 
			System.err.println("装载 JDBC/ODBC 驱动程序失败。"+e); 
			System.exit(1);
		} 
		catch (SQLException  e) { 
			System.err.println("无法连接数据库"+e); 
			System.exit(1); 
		}
		
		return con;
	}
	
	public mainw(String title)
	{
		super(title);
		this.setSize(1500,1500);
		
		Container c=getContentPane();//定义容器
		c.setLayout(null);
		
		c.setSize(1500,1500);
		
		
        
        //p.add(c);
		
		/*GridBagConstraints gbc=new GridBagConstraints();
		gbc.insets=new Insets(10,10,10,10);
		gbc.weightx=0.5;
		gbc.weighty=0.5;*/
		
		NewPanel p=new NewPanel();//定义面板,存放图片
        p.setLayout(null);
        p.setSize(1024,1024);
        c.add(p);
        
        JLabel lb=new JLabel("欢迎进入网上购物中心--店主部分");
        lb.setSize(500,500);
        lb.setLocation(400,300);
        Font f6=new Font("华文彩云",Font.BOLD,30);
        lb.setFont(f6);
        lb.setForeground(Color.red);
	    p.add(lb);//LB置于图片之上
	    
	    
	    /*JLabel lc=new JLabel("孙萍,胡凌康,杜科,张伟,金阳,柯超杰");
        lc.setSize(200,200);
        lc.setLocation(10,10);
        Font f7=new Font("华文彩云",Font.BOLD,6);
        lc.setFont(f6);
        lc.setForeground(Color.red);
	    p.add(lc);*/
	    
	    
		mb=new JMenuBar();//菜单栏类
		setJMenuBar(mb);
		
		function=new JMenu("功能");
		mb.add(function);
		
		system=new JMenu("系统");
		mb.add(system);
		
		append=new JMenuItem("添加商品");//JMenu是JMenuItem的子类.MenuItem在选中时触发操作
		function.add(append);
		append.addActionListener(this);//本类对象引用
		
		demand=new JMenuItem("查询商品信息");
		function.add(demand);
		demand.addActionListener(this);
		
		amend=new JMenuItem("修改商品信息");
		function.add(amend);
		amend.addActionListener(this);
		
		delete=new JMenuItem("删除商品");
		function.add(delete);
		delete.addActionListener(this);
		
		exit=new JMenuItem("退出");
		system.add(exit);
		exit.addActionListener(this);
		
		appendp=new JMenuItem("添加商品");
		appendp.addActionListener(this);
		demandp=new JMenuItem("查询商品信息");
		demandp.addActionListener(this);
		amendp=new JMenuItem("修改商品信息");
		amendp.addActionListener(this);
		deletep=new JMenuItem("删除商品");
		deletep.addActionListener(this);
		exitp=new JMenuItem("退出");
		exitp.addActionListener(this);
		
		pm=new JPopupMenu();//弹出式菜单
		pm.add(appendp);
		pm.add(demandp);
		pm.add(amendp);
		pm.add(deletep);
		pm.addSeparator();
		pm.add(exitp);
		
		
		JLabel ln=new JLabel("商品名称");
        ln.setSize(200,200);
        ln.setLocation(50,10);
        Font f1=new Font("宋体",Font.BOLD,26);
        /*c.gridx=0;
		c.gridy=0;
		c.gridwidth=1;
		c.gridheight=1;
		c.fill=GridBagConstraints.NONE;
		c.anchor=GridBagConstraints.CENTER;*/
        ln.setFont(f1);
        ln.setForeground(Color.red);
	    p.add(ln);
		 //c.add(ln);
	
	
		
		/*gbc.gridx=1;
		gbc.gridy=0;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;*/
		tfn=new JTextField(20);
		tfn.setEditable(false);
		tfn.setSize(160,26);
        tfn.setLocation(200,100);
		p.add(tfn);
		
		/*gbc.gridx=0;
		gbc.gridy=1;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;*/
		JLabel lp=new JLabel("商品价格");
        lp.setSize(200,200);
        lp.setLocation(50,115);
        Font f2=new Font("宋体",Font.BOLD,26);
        lp.setFont(f2);
        lp.setForeground(Color.red);
		//lp=new JLabel("商品价格");
		p.add(lp);
	
		
		/*gbc.gridx=1;
		gbc.gridy=1;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;*/
		tfp=new JTextField(20);
		tfp.setEditable(false);
		tfp.setSize(160,26);
        tfp.setLocation(200,200);
		p.add(tfp);
		
		/*gbc.gridx=0;
		gbc.gridy=2;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;*/
		JLabel la=new JLabel("库存数量");
        la.setSize(200,200);
        la.setLocation(50,215);
        Font f3=new Font("宋体",Font.BOLD,26);
        la.setFont(f3);
        la.setForeground(Color.red);
		//la=new JLabel("库存数量");
		p.add(la);
	
		
		/*gbc.gridx=1;
		gbc.gridy=2;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.CENTER;*/
		tfa=new JTextField(20);
		tfa.setEditable(false);
		tfa.setSize(160,26);
        tfa.setLocation(200,300);
		p.add(tfa);
		
	
		
		this.addMouseListener(new MouseAdapter()//鼠标监听事件
		{
			public void mouseReleased(MouseEvent event)
			{
				if(event.isPopupTrigger())
				{
					pm.show(event.getComponent(),event.getX(),event.getY());
				}
			}
		}
		);
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(100,100,500,500);
		table = new JTable();
		//p.add(table);
		p.add(table, BorderLayout.EAST);
		displayTable();		 
		setSize(500, 650);//
		this.show();
	}
	
	public void actionPerformed(ActionEvent event)
	{
		String command=event.getActionCommand();
		if(command.equals("添加商品"))
		{
			append ap=new append(this,"添加商品",false);
		}
		if(command.equals("查询商品信息"))
		{
			demand de=new demand(this,"查询商品信息",false);
		}	
		if(command.equals("修改商品信息"))
		{	      
			amend am=new amend(this,"修改商品信息",false);
		}
		if(command.equals("删除商品"))
		{	      
			delete del=new delete(this,"删除商品",false);
		}
		if(command.equals("退出"))
		{	      
			exit ex=new exit(this,"退出",false);
		}
	}
public void displayTable() 
	{ 
		try {
			con= getConnection();
			st= con.createStatement(); 
			r= st.executeQuery("SELECT * FROM t1");

			displayResult(r);	
		} 
		catch (SQLException sqle) { 
			sqle.printStackTrace(); 
		}
		finally {
			try {
			    r.close();st.close();con.close();
			}
		    catch (SQLException sqle) {
		    }
		}			 	 
	} 

private void displayResult(ResultSet rs) throws SQLException { 
		try {
			table = new JTable(getResultRow(rs), getResultHeader(rs));
	table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){   
                public void valueChanged(ListSelectionEvent e) {  
                	Vector vec = null;

        			try {
        				con= getConnection();
        				st= con.createStatement();
        			    r = st.executeQuery("SELECT * FROM t1");
        			    Vector rows = getResultRow(r);
        			    vec =(Vector) rows.get(table.getSelectedRow());	
        			    String zhonglei = (String) vec.get(0);
        			    String deptName = (String) vec.get(1);
        				String no = (String) vec.get(2);
        				String price = (String) vec.get(3);
        				String num = (String) vec.get(4);
        			//	NumT.setText(num);
        				//NumT.disable();
        				//NameT.setText(deptName); 
        				//NoT.setText(no);
        				//PriceT.setText(price); 
        			//	ZhongleiT.setText(zhonglei);          			   
        			} 
        			catch (SQLException sqle) {
        				sqle.printStackTrace();
        			}
        			finally {
        				try {
        				    r.close();st.close();con.close();
        				}
        			    catch (SQLException sqle) {
        			    }
        			}		
                }   
		    }); 
			JScrollPane scroller = new JScrollPane(table);		 
			Container c = getContentPane();	
			//NewPanel p=new NewPanel();	
			c.add(scroller, BorderLayout.EAST);
			scroller.setVisible(true);			
            c.remove(1);
			c.validate();	
			table.validate();
			scroller.validate(); 
		} 
		catch (SQLException sqle) { 
			sqle.printStackTrace(); 
		} 
	}
    
private Vector getResultHeader(ResultSet rs) throws SQLException {
	Vector headers = new Vector();
		try {
		    ResultSetMetaData rsmd = rs.getMetaData();
		    
			for (int i = 1; i <= rsmd.getColumnCount(); ++i) { 
				headers.add(rsmd.getColumnName(i)); 
			}
		}
		catch (SQLException sqle) {
			sqle.printStackTrace(); 
		}
		
		return headers;
	}

private Vector getResultRow(ResultSet rs) throws SQLException  {
	Vector rows = new Vector();
		try {
		    ResultSetMetaData rsmd = rs.getMetaData();

			if (!rs.next()) { 
				JOptionPane.showMessageDialog(this, "No record"); 
				setTitle("No record"); 
				return null; 
			}

			do {
		        Vector currentRow = new Vector();

				for (int i = 1; i <= rsmd.getColumnCount(); ++i) {
				    currentRow.add(rs.getString(i));
				}

				rows.add(currentRow);
			} while (rs.next());
		}
		catch (SQLException sqle) {
			sqle.printStackTrace(); 
		}

		return rows;
	}

public void shutDown() { 
		try {
			if (!con.isClosed()) {
			    con.close();
			}
		} 
		catch (SQLException sqle) { 
			System.err.println( "Unable to disconnect" ); 
			sqle.printStackTrace(); 
		} 
	}


//添加模块
class append extends JDialog implements ActionListener
{
	
	private JLabel ln,lp,la;
	private JTextField tfn,tfp,tfa;
	private JButton bt,btqu;
	
	public append(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=0;
		gbc.gridy=1;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.fill=GridBagConstraints.NONE;
		gbc.anchor=GridBagConstraints.WEST;
		lp=new JLabel("商品价格");
		c.add(lp,gbc);
		

⌨️ 快捷键说明

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