📄 stockmanagementdatabean.java~3455~
字号:
package stockmanagementpro;
import javax.ejb.*;
import javax.naming.*;
import user.*;
import java.util.*;
import javax.rmi.*;
import javax.sql.DataSource;
import java.sql.*;
import method.*;
public class StockManagementDataBean implements SessionBean {
SessionContext sessionContext;
private UserTableHome userTableHome = null;
private UserTable userTable = null;
private UserLogHome userLogHome = null;
private UserLog userLog = null;
private GoodsCategoryHome goodsCategoryHome = null;
private GoodsCategory goodsCategory = null;
private GoodsHome goodsHome = null;
private Goods goods = null;
private SupplierHome supplierHome = null;
private Supplier supplier = null;
private CustomerHome customerHome = null;
private Customer customer = null;
private WarehouseHome warehouseHome = null;
private Warehouse warehouse = null;
private AccountNameHome accountNameHome = null;
private AccountName accountName = null;
//创建数据处理方法类
private DataMethod dataMethod = new DataMethod();
//创建EJB的创建接口
public void ejbCreate() throws CreateException {
try{
Context context = new InitialContext();
userTableHome = (UserTableHome)context.lookup("UserTable");
userLogHome = (UserLogHome)context.lookup("UserLog");
goodsCategoryHome = (GoodsCategoryHome)context.lookup("GoodsCategory");
goodsHome = (GoodsHome)context.lookup("Goods");
supplierHome = (SupplierHome)context.lookup("Supplier");
customerHome = (CustomerHome)context.lookup("Customer");
warehouseHome = (WarehouseHome)context.lookup("Warehouse");
accountNameHome = (AccountNameHome)context.lookup("AccountName");
}catch (Exception ex){
}
}
public void ejbRemove() {
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
//检查用户的方法
public int[] checkUser(String userName, String userPassword){
int[] functions = new int[4];
try{
//根据用户名字取得EJB的接口
userTable = userTableHome.findByPrimaryKey(userName);
//取得用户的名字和密码
String name = userTable.getUserName();
String password = userTable.getUserPassword();
//检查用户名和密码
if(name.equals(userName) && password.equals(userPassword)){
functions[0] = userTable.getBaseInforFunction();
functions[1] = userTable.getStockFunction();
functions[2] = userTable.getStockManageFunction();
functions[3] = userTable.getSaleFunction();
}else{
functions[0] = -1;
functions[1] = -1;
functions[2] = -1;
functions[3] = -1;
}
}catch(Exception ex){
functions[0] = -1;
functions[1] = -1;
functions[2] = -1;
functions[3] = -1;
}
return functions;
}
//创建用户
public int createUser(User user) {
int result = 0;
try{
userTable = userTableHome.create(user.getUserName(), user.getUserPassword(),
user.getBaseInforFunction(),
user.getStockFunction(),
user.getStockManageFunction(),
user.getSaleFunction());
result = 1;
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
//更新用户
public int updateUser(User user) {
int result = 0;
try{
//根据主键寻找记录
userTable = userTableHome.findByPrimaryKey(user.getUserName());
//更新用户密码
userTable.setUserPassword(user.getUserPassword());
//更新用户权限
userTable.setBaseInforFunction(user.getBaseInforFunction());
userTable.setStockFunction(user.getStockFunction());
userTable.setStockManageFunction(user.getStockManageFunction());
userTable.setSaleFunction(user.getSaleFunction());
result = 1;
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
//删除用户
public int deleteUser(User user) {
int result = 0;
try{
//根据主键寻找记录
userTable = userTableHome.findByPrimaryKey(user.getUserName());
//删除用户
userTable.remove();
result = 1;
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
//根据用户名查询用户
public String[][] getUserByUserName(String userName){
//创建存取用户数据的数组
String[][] detail = null;
try{
//取得用户的所有记录
Collection col = userTableHome.findByUserName("%" + userName + "%");
if(col.size() > 0){
Iterator iterator = col.iterator();
//重新创建数组
detail = new String[col.size()][6];
int i = 0;
//填写用户数组
while (iterator.hasNext()) {
//取得远程接口
userTable = (UserTable) PortableRemoteObject.narrow(
iterator.next(), UserTable.class);
detail[i][0] = userTable.getUserName();
detail[i][1] = userTable.getUserPassword();
detail[i][2] = String.valueOf(userTable.getBaseInforFunction());
detail[i][3] = String.valueOf(userTable.getStockFunction());
detail[i][4] = String.valueOf(userTable.getStockManageFunction());
detail[i][5] = String.valueOf(userTable.getSaleFunction());
i++;
}
}else{
detail = new String[0][6];
}
}catch(Exception ex){
detail = new String[0][6];
ex.printStackTrace();
}
//返回数组
return detail;
}
//创建用户日志
public int createUserLog(String programName, String operationContent, String userName) {
int result = 0;
try{
//创建日期类
java.util.Calendar now = java.util.Calendar.getInstance();
java.sql.Timestamp operationDate = new java.sql.Timestamp(
now.getTime().getTime());
Collection col = userLogHome.findAll();
//根据集合创建Vector集合类
Vector vector = new Vector(col);
Integer id = null;
if (col.size() > 0) {
//取得最后一条记录
UserLog userLog = (UserLog) PortableRemoteObject.narrow(
vector.lastElement(), UserLog.class);
//创建新序号
int newInt = userLog.getId().intValue() + 1;
id = new Integer(newInt);
}
else {
//如果集合不返回记录,开始序号是1
id = new Integer(1);
}
//添加记录
userLogHome.create(id, programName, operationContent, userName, operationDate);
result = 1;
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
//删除用户日志记录
public int deleteUserLog(Integer id) {
int result = 0;
try{
userLog = userLogHome.findByPrimaryKey(id);
userLog.remove();
result = 1;
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
//联接数据库缓冲池的方法
public java.sql.Connection getConnection() {
InitialContext initCtx = null;
try {
initCtx = new InitialContext();
DataSource ds = (javax.sql.DataSource)initCtx.lookup("stockManagement");
return ds.getConnection();
} catch(Exception ne) {
System.out.println("找不到数据源");
throw new EJBException(ne);
} finally {
try {
if(initCtx != null) initCtx.close();
} catch(NamingException ne) {
System.out.println(ne.getMessage());
throw new EJBException(ne);
}
}
}
//关闭数据库联接的方法
public void cleanup(Connection con) {
//将数据库联接返回缓冲池
try {
if (con != null) con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//返回数据库所有数据表名字的方法
public String[] getTableNames() {
String[] tableNames = new String[0];
//创建集合类
Vector tableNameVector = new Vector();
//取得数据库联接
Connection conn = getConnection();
try{
//取得联接信息类
DatabaseMetaData databaseMetaData = conn.getMetaData();
//取得数据表名字记录集
ResultSet rs = databaseMetaData.getTables("stockmanagement", null, null,
new String[] {"TABLE"});
String tableName = new String();
while(rs.next()){
tableName = rs.getString(3);
if(!tableName.equals("dtproperties")){
tableNameVector.addElement(rs.getString(3));
}
}
tableNames = new String[tableNameVector.size()];
for(int i = 0; i < tableNames.length; i++){
tableNames[i] = (String)tableNameVector.get(i);
}
}catch(Exception ex){
ex.printStackTrace();
}
//清空数据库联接
cleanup(conn);
return tableNames;
}
//返回数据表数据的方法
public String[][] getDataByTableName(String tableName) {
String[][] data = new String[0][0];
//取得数据库联接
Connection conn = getConnection();
String countSql = "select count(*) from " + tableName;
String sql = "select * from " + tableName;
try{
Statement stmt = conn.createStatement();
//取得数据表的记录数
ResultSet rs = stmt.executeQuery(countSql);
int rowCount = 0;
if(rs.next()){
rowCount = rs.getInt(1);
}
//取得数据表的记录
rs = stmt.executeQuery(sql);
//取得数据表的列对象
ResultSetMetaData resultSetMetaData = rs.getMetaData();
//取得列的总数
int colCount = resultSetMetaData.getColumnCount();
//根据数据表的行与列总数创建数组
data = new String[rowCount][colCount];
//将数据记录存放在数组
int row = 0;
while(rs.next()){
for(int col =0; col < colCount; col++){
data[row][col] = rs.getString(col + 1);
}
row++;
}
}catch(Exception ex){
data = new String[0][0];
ex.printStackTrace();
}
//清空数据库联接
cleanup(conn);
return data;
}
//将数组写入数据表的方法
public int setDataByTableName(String tableName, String[][] data) {
int result = 0;
//取得数据库联接
Connection conn = getConnection();
String deleteSql = "delete from " + tableName;
String selectSql = "select * from " + tableName;
String insertSql = "insert into " + tableName + " values(";
try{
//开始事务
conn.setAutoCommit(false);
//创建不带参数的SQL语句执行类
Statement stmt = conn.createStatement();
//删除数据表的记录
stmt.executeUpdate(deleteSql);
//取得数据表的记录
ResultSet rs = stmt.executeQuery(selectSql);
//取得数据表的列对象
ResultSetMetaData resultSetMetaData = rs.getMetaData();
//取得列的总数
int colCount = resultSetMetaData.getColumnCount();
for (int col = 0; col < colCount; col++) {
if(col == colCount -1){
insertSql += "?" + ")";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -