?? stockmanagementdatabean.java
字號:
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 + -