1、毕业设计源程序中小型超市综合信息管理系统第1章 JavaBean部分1.1 数据库连接(DataBase.java)package common;import java.sql.*;import javax.naming.*;import javax.sql.*;import java.util.*;public class DataBase private String dbUrl = jdbc:microsoft:sqlserver:/localhost:1433;DatabaseName=ManagermentDB; private String dbUser = ren; privat
2、e String dbPwd = ren;/定义数据库的连接 public DataBase() throws Exception Class.forName(com.microsoft.jdbc.sqlserver.SQLServerDriver); /加载数据库的驱动 public Connection getConnection()throws Exception return java.sql.DriverManager.getConnection(dbUrl,dbUser,dbPwd); /建立数据库的连接 public void closeConnection(Connection
3、 con) try if(con!=null) con.close(); catch(Exception e) e.printStackTrace(); public void closePrepStmt(PreparedStatement prepStmt) try if(prepStmt!=null) prepStmt.close(); catch(Exception e) e.printStackTrace(); public void closeResultSet(ResultSet rs)/关闭当前记录集 try if(rs!=null) rs.close(); catch(Exce
4、ption e) e.printStackTrace(); public static void close( ResultSet rs, Statement st, Connection conn )/关闭数据库的连接 try if( rs!=null ) rs.close(); catch( SQLException ex ) ; try if( st!=null ) st.close(); catch( SQLException ex ) ; try if( conn!=null ) conn.close(); catch( SQLException ex ) ; 1.2 人事管理部门1
5、.2.1员工实体的定义(Employees.java)package employees;public class Employees implements Comparable private String EmpId; private String DepId; private String EmpName; private String Sex; private String Nationality; private String Birth; private String Academic; private String Marital; private String Original
6、; private String Id_Card; private String Mobile; private String Memo;/员工实体的属性 Employees(String EmpId,String DepId,String EmpName ,String Sex, String Nationality,String Birth,String Academic,String Marital, String Original,String Id_Card,String Mobile,String Memo) /初始化操作 this.EmpId = EmpId; this.DepI
7、d = DepId; this.EmpName = EmpName; this.Sex = Sex; this.Nationality = Nationality; this.Birth = Birth; this.Academic = Academic ; this.Marital = Marital; this.Original = Original; this.Id_Card = Id_Card; this.Mobile = Mobile ; this.Memo = Memo; /对每一个属性赋值和取值操作 public void setEmpId(String newEmpId) th
8、is.EmpId = newEmpId; public String getEmpId() return EmpId; public void setDepId(String newDepId) this.DepId = newDepId; public String getDepId() return DepId; public void setEmpName(String newEmpName) this.EmpName = newEmpName; public String getEmpName() return EmpName; public void setSex(String ne
9、wSex) this.Sex = newSex; public String getSex() return Sex; public void setNationality(String newNationality) this.Nationality = newNationality; public String getNationality() return Nationality; public void setBirth(String newBirth) this.Birth = newBirth; public String getBirth() return Birth; publ
10、ic void setAcademic(String newAcademic) this.Academic = newAcademic; public String getAcademic() return Academic; public void setMarital(String newMarital) this.Marital = newMarital; public String getMarital() return Marital; public void setOriginal(String newOriginal) this.Original = newOriginal; p
11、ublic String getOriginal() return Original; public void setId_Card(String newId_Card) this.Id_Card = newId_Card; public String getId_Card() return Id_Card; public void setMobile(String newMobile) this.Mobile = newMobile; public String getMobile() return Mobile; public void setMemo(String newMemo) th
12、is.Memo = newMemo; public String getMemo() return Memo; public int compareTo(Object o) Employees n = (Employees)o; int lastCmp = EmpNpareTo(n.EmpName); return(lastCmp); 1.2.2员工实体的操作(EmployeesBean.java)package employees;import java.sql.*;import java.util.*;import javax.naming.*;import javax.sql.*;imp
13、ort employees.*;import common.*;public class EmployeesBean /得到员工的详细信息 public Employees getEmployeesDetails(String EmpId) throws Exception DataBase database = new DataBase(); boolean result = false; Connection con = null; PreparedStatement prepStmt = null; ResultSet rs = null; try con = database.getC
14、onnection(); String sql = select EmpId, DepId, EmpName , Sex, Nationality, Birth, Academic, Marital,Original, Id_Card, Mobile, Memo +from Employees where EmpId =?; prepStmt = con.prepareStatement(sql); prepStmt.setString(1,EmpId); rs = prepStmt.executeQuery(); if(rs.next() Employees employees = new
15、Employees(rs.getString(1),rs.getString(2),rs.getString(3), rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8), rs.getString(9),rs.getString(10),rs.getString(11),rs.getString(12); prepStmt.close(); return employees; else return null; finally database.closeResultSet(rs); d
16、atabase.closePrepStmt(prepStmt); database.closeConnection(con); /getEmployees()/得到所有员工的详细信息 public Collection getEmployees() throws Exception DataBase database = new DataBase(); boolean result = false; Connection con = null; PreparedStatement prepStmt = null; ResultSet rs = null; Employees = new Arr
17、ayList(); try con = database.getConnection(); String sql = select EmpId, DepId, EmpName , Sex, Nationality, Birth, Academic, Marital,Original, Id_Card, Mobile, Memo +from Employees ; prepStmt = con.prepareStatement(sql); rs = prepStmt.executeQuery(); while(rs.next() Employees bd = new Employees(rs.g
18、etString(1),rs.getString(2),rs.getString(3), rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8), rs.getString(9),rs.getString(10),rs.getString(11),rs.getString(12); Employees.add(bd); finally database.closeResultSet(rs); database.closePrepStmt(prepStmt); database.closeCo
19、nnection(con); Collections.sort(Employees); return Employees; / nsertEmployees 插入员工的信息 public int InsertEmployees(String EmpId,String EmpName,String Sex,String Nationality,String Birth, String Academic,String Marital,String Original,String Id_Card, String Mobile,String Memo,String DepId) throws Exce
20、ption Connection con = null; Statement Stmt = null; ResultSet rs=null; DataBase database = new DataBase(); int nResult = 0; try con = database.getConnection(); String sql = SELECT EmpId FROM Employees WHERE EmpId=+EmpId+; Stmt = con.createStatement(); rs = Stmt.executeQuery( sql ); if(rs.next() nRes
21、ult=2; else sql=insert into Employees(EmpId,EmpName,Sex,Nationality,Birth,Academic,Marital,Original,Id_Card,Mobile,Memo,DepId)VALUES( +EmpId+,+EmpName+,+Sex+,+Nationality+,+Birth+,+Academic+,+Marital+, +Original+,+Id_Card+,+Mobile+,+Memo+,+DepId+); nResult = Stmt.executeUpdate(sql); sql= insert into
22、 Users(UserId,UserName,PassWords)values(+EmpId+,+EmpName+,+666666+); nResult = Stmt.executeUpdate(sql); return nResult; catch( SQLException ex ) ex.printStackTrace( System.err ); nResult=0; return nResult; finally database.close(null,Stmt,con); /删除一个员工的所有信息 public int deleteOneEmployees(String EmpId
23、) throws Exception boolean result = false; Connection con = null; Statement Stmt = null; ResultSet rs=null; DataBase database = new DataBase(); int nResult = 0; try con = database.getConnection(); String sql = SELECT EmpId FROM Employees WHERE EmpId=+EmpId+; Stmt = con.createStatement(); rs = Stmt.e
24、xecuteQuery( sql ); if(rs.next() sql=DELETE from Employees where EmpId=+EmpId+; nResult = Stmt.executeUpdate(sql); sql=DELETE from Users where UserId=+EmpId+; nResult = Stmt.executeUpdate(sql); else nResult=2; return nResult; catch( SQLException ex ) ex.printStackTrace( System.err ); nResult=0; retu
25、rn nResult; finally database.close(null,Stmt,con); / 修改员工的信息 public int updateEmployees(String EmpId,String EmpName,String Sex,String Nationality,String Birth, String Academic,String Marital,String Original,String Id_Card, String Mobile,String Memo,String DepId) throws Exception Connection con = nul
26、l; Statement Stmt = null; ResultSet rs=null; DataBase database = new DataBase(); int nResult = 0; try con = database.getConnection(); String sql = UPDATE Employees SET EmpId=+EmpId+,EmpName=+EmpName+, Sex= +Sex+,Nationality=+Nationality+,Birth=+Birth+,Academic=+Academic+, Marital= +Marital+,Original
27、=+Original+, Id_Card=+Id_Card+,Mobile=+Mobile+, Memo= +Memo+, DepId=+DepId+ WHERE EmpId=+EmpId+; Stmt = con.createStatement(); nResult = Stmt.executeUpdate(sql); sql= update Users set UserId=+EmpId+,UserName=+EmpName+ where UserId=+EmpId+; nResult = Stmt.executeUpdate(sql); return nResult; catch( SQ
28、LException ex ) ex.printStackTrace( System.err ); nResult=0; return nResult; finally database.close(null,Stmt,con); 1.2.3部门实体的定义(departments.java)package departments;/* 部门类 */public class Departments private String DepId; /部门的编号 private String DepName; /部门的名称 Departments(String DepId,String DepName) /部门的初始化 this.DepId =DepId; this.DepName = DepName; public void setDepId(String newId) this.DepId = newId; public String getDepId() return DepId; public void setDepName(String newDepName) this.DepName = newDepName;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1