货存管理系统 java sql

或存管理系统

终于结束了数据库的课设,做了货存管理系统,我做了java部分,完成了很简单的界面的交互功能的实现和与数据库的链接,做的很简单,功能也很不好^^{{{!!!!

将与数据库的链接进行了封装,每次只需调用传入账号和密码

public class jdbcUtil {
	Connection con = null;
	PreparedStatement ps1 =null;
	ResultSet rs = null;
	InputStream is=null;
	OutputStream os= null;
	public static Connection getMysqlConn(String name,String pwd) {
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			return DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=货存管理系统",name,pwd);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
	public static void close(ResultSet rs,Statement ps,Connection con) {
		if(rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(ps!=null) {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(con!=null) {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	
}

	

}

登陆界面

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login extends JFrame implements ActionListener{
	private static final long serialVersionUID = -5978502250361767960L;
	//按钮
	JButton btnLogin,btnRegister,btnCancel;
	//创建中间容器
	JPanel pnlSouth,pnlNorth,pnlCenter,pnlCenter1,pnlCenter2;
	//标签
	JLabel lbl1,JLabelNum,JLabelPwd;
	//用户名文本框
	JTextField tfNum;
	//密码文本框
	JPasswordField tfPwd;
	//创建窗口
	String url = "com.mysql.jdbc.Driver";
	Login(String title){
		super(title);
		
		//north
		pnlNorth = new JPanel();
		lbl1 = new JLabel("欢迎进入货存系统!");
		pnlNorth.add(lbl1);
		this.add(pnlNorth,BorderLayout.NORTH);
		
		//center
		pnlCenter=new JPanel();
		pnlCenter1=new JPanel();
		pnlCenter2=new JPanel();
		pnlCenter.setLayout(new BorderLayout());
		JLabelNum=new JLabel("学   号:");
		tfNum=new JTextField(15);
		pnlCenter1.add(JLabelNum);
		pnlCenter1.add(tfNum);
		pnlCenter.add(pnlCenter1,BorderLayout.NORTH);
		JLabelPwd=new JLabel("密    码:");
		tfPwd=new JPasswordField(15);
		pnlCenter2.add(JLabelPwd);
		pnlCenter2.add(tfPwd);
		pnlCenter.add(pnlCenter2,BorderLayout.SOUTH);
		this.add(pnlCenter,BorderLayout.CENTER); 

		//south
		pnlSouth = new JPanel();
		
		//生成按钮
		btnLogin = new JButton("登录");
		btnLogin.addActionListener(this);

		btnRegister = new JButton("注册");
		btnRegister.addActionListener(this);

		
		btnCancel = new JButton("取消");
		btnCancel.addActionListener(this);

		//将三个按钮放在一个中间容器中
		pnlSouth.add(btnLogin);
		
		pnlSouth.add(btnRegister);
		pnlSouth.add(btnCancel);
		//将按钮添加到图形界面
		//this.add(btnLogin);
		//this.add(btnRegister);
		//this.add(btnCancel);
		this.add(pnlSouth,BorderLayout.SOUTH);
		this.setSize(500, 180);
		GUIUtil.toCenter(this);//使窗口居中
		this.setVisible(true);//可视化
		this.setResizable(false);//关闭放大窗口
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置错误关闭操作
		
	}
	
	
public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnLogin){
			String num = tfNum.getText();
			String pwd = tfPwd.getText();
			
			if(num==null){
				JOptionPane.showMessageDialog(this, "您输入的账号不存在,请重新输入!");
				return;
			}
			
			Connection con=null;
			con = jdbcUtil.getMysqlConn(num, pwd);
			if(con==null){
				JOptionPane.showMessageDialog(this, "您输入的密码错误,请重新输入!");
				return;
			}
			JOptionPane.showMessageDialog(this,"恭喜您,登陆成功!"+"\n  数据库连接成功");
			this.dispose();
			new AdminFunction(num,pwd);
			return;
			
	}
		
		if(e.getSource()==btnRegister) {
			new ModifyDialog("注册用户");
		}
		if(e.getSource()==btnCancel) {
			  JOptionPane.showMessageDialog(this,"拜拜!!!!");
	            System.exit(0);
		}
		
}}

管理员

public class AdminFunction extends Frame implements ActionListener{
	//存储货物编号的文件
	String s,d;//账号密码
	
	//定义各个控件
	private JButton btLookSelf=new JButton("查看个人资料");
	private JButton btModify=new JButton("修改个人资料");
	private JButton btShowhc=new JButton("查看存货信息");
	//private JButton btModifyhc=new JButton("修改存货信息");
	private JButton btDeletehc=new JButton("出库");
	private JButton btAddhc=new JButton("入库");
	private JButton btExit=new JButton("退出系统");
	ImageIcon img = new ImageIcon("images\\bg.jpg");
	JLabel bg = new JLabel(img);
	
	
	public AdminFunction(String s,String d){
		super("账号:"+s);//串口标题
		
		this.s=s;
		this.d=d;//传入账号密码
		
		this.setLayout(null);//设置为手工设置各个组件的位置和大小
		
		
		bg.setBounds(0,0,img.getIconHeight(),img.getIconWidth());
		btLookSelf.setBounds(new Rectangle(50,80,300,50));//修改个人资料
		
		btShowhc.setBounds(new Rectangle(50,150,300,50));//查看存货信息
	//	btModifyhc.setBounds(new Rectangle(50,220,300,50));//修改存货信息
		btDeletehc.setBounds(new Rectangle(50,220,300,50));//出库
		btAddhc.setBounds(new Rectangle(50,290,300,50));//入库
		btExit.setBounds(new Rectangle(50,360,300,50));//退出系统

		
		
		btLookSelf.addActionListener(this);
		
		btShowhc.addActionListener(this);
		//btModifyhc.addActionListener(this);
		btDeletehc.addActionListener(this);
		btAddhc.addActionListener(this);
		btExit.addActionListener(this);
		
	
		
		this.add(bg);
		this.add(btLookSelf);
	
		this.add(btShowhc);
		//this.add(btModifyhc);
		this.add(btDeletehc);
		this.add(btAddhc);
		this.add(btExit);
		
		this.setSize(400,500);
		
		GUIUtil.toCenter(this);//使窗口居中
		this.setVisible(true);//可视化
		this.setResizable(false);//关闭放大窗口
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置错误关闭操作
		
		//用于关闭窗体事件
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}
	
	private void setDefaultCloseOperation(int exitOnClose) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
			
			//建立连接(建立对象内部其实包含了Socket对象,是一个远程的连接。比较耗时!这是connection对象管理的要点)
			//真正开发中,为了提高效率,都会使用连接池来管理连接对象;
			Connection con = null;
			con = jdbcUtil.getMysqlConn(s, d);
			
		
		
		
		
		// TODO Auto-generated method stub
		if(e.getSource()==btLookSelf) {//查看个人资料
			String message="您的详细资料为:\n";
			
            message+="账号:"+s+"\n";
            message+="姓名:"+"测试员666"+"\n";
            message+="性别:"+"男"+"\n";
            message+="年龄:"+"18"+"\n";
            message+="班级:"+"网络工程1702"+"\n";
            
            JOptionPane.showMessageDialog(this,message);
        }else if(e.getSource()==btShowhc) {//查看货物信息
        	this.dispose();
            new selecthc(s,d);
        }
        else if(e.getSource()==btDeletehc) {//出库
            this.dispose();
        	new deletehc(s,d);
        	
        	
        }else if(e.getSource()==btAddhc) {//入库
        	this.dispose();
        	new addhc(s,d);
        }else if(e.getSource()==btExit){//退出系统
            JOptionPane.showMessageDialog(this,"谢谢光临,欢迎下次继续使用本系统!");
            System.exit(0);
        }
	}
}

出库

public class deletehc extends Frame implements ActionListener{



	String s,d;//账号密码
	
	private JTextField Text1,Text2,Text3;
	private JLabel jlabel1,jlabel2,jlabel3;
	Font font=new Font("楷体",1,15);
	JButton b1,b2;
	
	


public deletehc(String s, String d) {
	super("出库");//串口标题
	
	this.s=s;
	this.d=d;
	
	
	setLayout(new FlowLayout());
	
	jlabel1 = new JLabel(" 出库物品:");
	this.add(jlabel1);
	jlabel1.setFont(font);
	Text1 = new JTextField(15);
	this.add(Text1);
	
	jlabel2 = new JLabel(" 货   号: ");
	this.add(jlabel2);
	jlabel2.setFont(font);
	Text2 = new JTextField(15);
	this.add(Text2);
	
	jlabel3 = new JLabel("  出库量: ");
	this.add(jlabel3);
	jlabel3.setFont(font);
	Text3 = new JTextField(15);
	this.add(Text3);
	
	b1 = new JButton("出库");
	b1.setFont(font);
	b1.addActionListener(this);
	this.add(b1);
	
	b2 = new JButton("返回");
	b2.setFont(font);
	b2.addActionListener(this);
	this.add(b2);
	
	this.setSize(300,150);
	
	GUIUtil.toCenter(this);//使窗口居中
	this.setVisible(true);//可视化
	this.setResizable(false);//关闭放大窗口
	
	
	//用于关闭窗体事件
	this.addWindowListener(new WindowAdapter(){
		public void windowClosing(WindowEvent e){
			System.exit(0);
		}
	});
	
	}



public void actionPerformed(ActionEvent a) {
	
	
	
	Connection con = null;
	PreparedStatement ps1 =null;
	PreparedStatement ps2 =null;
	PreparedStatement ps3 =null;
	ResultSet rs =null;
	if(a.getSource()==b1) {
		String Gno = Text1.getText();
		String Cno = Text2.getText();	
		String Outnum = Text3.getText();	
				try {
		con = jdbcUtil.getMysqlConn(s, d);
		con.setAutoCommit(false);//jdbc默认自动提交事务
		
		ps1 = con.prepareStatement("update Stock_Out set Outnum=? where Gno=? and Cno=?");
		ps1.setString(1, Outnum);
		ps1.setString(2, Gno);
		ps1.setString(3, Cno);
		ps1.execute();
		
		
		ps3=con.prepareStatement("select * from Stock_Out where Gno=? and Cno=?");
		ps3.setString(1, Gno);
		ps3.setString(2, Cno);
		rs = ps3.executeQuery();
		while(rs.next()) {
			Double jg = rs.getDouble("Goutprice")*Integer.valueOf(Outnum);		
			ps2 = con.prepareStatement("update Stock_Out set Outprice=? where Gno=? and Cno=?");
			ps2.setDouble(1,jg);
			ps2.setString(2, Gno);
			ps2.setString(3, Cno);
			ps2.execute();
					}
		
	
		
		
		con.commit();//提交事务
		JOptionPane.showMessageDialog(this, "出库成功");
		new deletehc(s,d);
	}catch (SQLException e) {
		e.printStackTrace();
	}finally {
		//关闭顺序遵循从 ResultSet--》 PreparedStatement--》 connection,一定要把三个catch块分开写!!
		if(ps1!=null) {
			try {
				ps1.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(con!=null) {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
	
	if(a.getSource()==b2) {
		this.dispose();
		new AdminFunction(s,d);
	}
	
}
}


	


	


入库

public class addhc extends Frame implements ActionListener{

	

	String s,d;//账号密码
	
	private JTextField Text1,Text2,Text3;
	private JLabel jlabel1,jlabel2,jlabel3;
	Font font=new Font("楷体",1,15);
	JButton b1,b2;
	
	public addhc(String s,String d) {
		
		
		super("入库");//串口标题
		
		this.s=s;
		this.d=d;
		
		
		setLayout(new FlowLayout());
		
		jlabel1 = new JLabel(" 入库物品:");
		this.add(jlabel1);
		jlabel1.setFont(font);
		Text1 = new JTextField(15);
		this.add(Text1);
		
		jlabel2 = new JLabel(" 货   号: ");
		this.add(jlabel2);
		jlabel2.setFont(font);
		Text2 = new JTextField(15);
		this.add(Text2);
		
		jlabel3 = new JLabel("  入库量: ");
		this.add(jlabel3);
		jlabel3.setFont(font);
		Text3 = new JTextField(15);
		this.add(Text3);
		
		b1 = new JButton("入库");
		b1.setFont(font);
		b1.addActionListener(this);
		this.add(b1);
		
		b2 = new JButton("返回");
		b2.setFont(font);
		b2.addActionListener(this);
		this.add(b2);
		
		this.setSize(300,150);
		
		GUIUtil.toCenter(this);//使窗口居中
		this.setVisible(true);//可视化
		this.setResizable(false);//关闭放大窗口
		
		
		//用于关闭窗体事件
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		
	}
	
	

public void actionPerformed(ActionEvent a) {
	
	
	
	Connection con = null;
	PreparedStatement ps1 =null;
	PreparedStatement ps2= null;
	PreparedStatement ps3 = null;
	ResultSet rs = null;
	if(a.getSource()==b1) {
		String Gno = Text1.getText();
		String Pno = Text2.getText();	
		String Innum = Text3.getText();
		
		
		try {
			con = jdbcUtil.getMysqlConn(s, d);
			con.setAutoCommit(false);//jdbc默认自动提交事务
			
			ps3=con.prepareStatement("select * from Stock_In where Gno=? and Pno=?");
			ps3.setString(1, Gno);
			ps3.setString(2, Pno);
			rs = ps3.executeQuery();
			
			while(rs.next()){
				int num=rs.getInt("Innum");
				
			ps1 = con.prepareStatement("update Stock_In set Innum=? where Gno=? and Pno=?");
			ps1.setString(1, num+Innum);
			ps1.setString(2, Gno);
			ps1.setString(3, Pno);
			ps1.execute();
			double jg=0;
			jg += rs.getDouble("Ginprice")*Integer.valueOf(Innum);	
			
			ps2 = con.prepareStatement("update Stock_In set Inprice=? where Gno=? and Pno=?");
			ps2.setDouble(1,jg);
			ps2.setString(2, Gno);
			ps2.setString(3, Pno);
			ps2.execute();
			con.commit();//提交事务
			JOptionPane.showMessageDialog(this, "入库成功");
			new addhc(s,d);
			}
			
			
			
		}catch (SQLException e) {
			e.printStackTrace();
		}finally {
			//关闭顺序遵循从 ResultSet--》 PreparedStatement--》 connection,一定要把三个catch块分开写!!
			if(ps1!=null) {
				try {
					ps1.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(con!=null) {
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
		
		if(a.getSource()==b2) {
			this.dispose();
			new AdminFunction(s,d);
		}
		
	
}
}

查询库存

public class selecthc extends JFrame implements ActionListener{
	String s,d;
	
	Font font=new Font("楷体",1,15);
	private JTextField Text1,Text2;
	private JLabel jlabel1,jlabel2;
	
	JButton b1,b2;

	selecthc(String s,String d){

		
super("查询");
this.s=s;
this.d=d;
setLayout(new FlowLayout());
		//north

jlabel1 = new JLabel("物品编号:");
this.add(jlabel1);
jlabel1.setFont(font);
Text1 = new JTextField(15);
this.add(Text1);


jlabel2 = new JLabel("  仓库号:");
this.add(jlabel2);
jlabel2.setFont(font);
Text2 = new JTextField(15);
this.add(Text2);


b1 = new JButton("查询");
b1.setFont(font);
b1.addActionListener(this);
this.add(b1);	
b2 = new JButton("返回");
b2.setFont(font);
b2.addActionListener(this);
this.add(b2);


this.setSize(300,150);

GUIUtil.toCenter(this);//使窗口居中
this.setVisible(true);//可视化
this.setResizable(false);//关闭放大窗口


//用于关闭窗体事件
this.addWindowListener(new WindowAdapter(){
	public void windowClosing(WindowEvent e){
		System.exit(0);
	}
});


}

public void actionPerformed(ActionEvent a) {
	//加载驱动类
	Connection con = null;
	PreparedStatement ps =null;
	ResultSet rs = null;
	if(a.getSource()==b1) {
		String name = Text1.getText();
		String num = Text2.getText();
		
		try{
		
		con = jdbcUtil.getMysqlConn(s, d); 
		
		ps=con.prepareStatement("select * from Stock where Gno=? and Sno=?");
		ps.setObject(1, name);
		ps.setObject(2, num);
		rs = ps.executeQuery();
		
		
		
		while(rs.next()) {
String message="              查询结果\n";
			
            message+="物品编号:"+rs.getString("Gno")+"\n";
            message+="    仓库号:"+rs.getString("Sno")+"\n";
            message+="物品名称:"+rs.getString("Gname")+"\n";
            message+="类型名称:"+rs.getString("Tname")+"\n";
            message+="物品数量:"+rs.getInt("Gnum")+"\n";
			JOptionPane.showMessageDialog(this,message);
			this.dispose();
			new selecthc(s,d);
			
		}
		
		
		
		
	
		
		
	} catch (SQLException e) {
		e.printStackTrace();
	}finally {
		//关闭顺序遵循从 ResultSet--》 PreparedStatement--》 connection,一定要把三个catch块分开写!!
		if(ps!=null) {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(con!=null) {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}
	if(a.getSource()==b2) {
		 this.dispose();
		new AdminFunction(s,d);
	}
}
}

在以上的代码中数据库名表名都是我的数据库中的名字!!!!!

数据库考试也考完了,接下来就是计算机组成原理了,加油加油!!!!
马上也该放假了,回去开始苦逼的学车,加自学

猜你喜欢

转载自blog.csdn.net/weixin_45130905/article/details/94433902