本系统采用Java Swing开发桌面应用,结合SQL Server数据库实现企业固定资产的全生命周期管理。系统支持资产入库、领用、调拨、维修、报废等业务流程,提供资产盘点、报表统计等功能,满足企业对固定资产信息化管理的需求。
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── fixedasset
│ │ │ ├── controller (控制器层)
│ │ │ ├── model (模型层)
│ │ │ ├── view (视图层)
│ │ │ ├── dao (数据访问层)
│ │ │ └── utils (工具类)
│ │ └── resources
│ │ └── db.properties (数据库配置)
// DBConnectionUtil.java
public class DBConnectionUtil {
private static Connection connection;
private static final String URL;
private static final String USERNAME;
private static final String PASSWORD;
private static final String DRIVER;
static {
try {
Properties properties = new Properties();
InputStream inputStream = DBConnectionUtil.class.getClassLoader().getResourceAsStream("db.properties");
properties.load(inputStream);
URL = properties.getProperty("url");
USERNAME = properties.getProperty("username");
PASSWORD = properties.getProperty("password");
DRIVER = properties.getProperty("driver");
Class.forName(DRIVER);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection() throws SQLException {
if (connection == null || connection.isClosed()) {
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
return connection;
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// Asset.java
public class Asset {
private int id;
private String assetCode;
private String assetName;
private String specification;
private String model;
private String categoryId;
private double purchasePrice;
private Date purchaseDate;
private String supplier;
private String departmentId;
private String location;
private String custodian;
private int status; // 1:在用, 2:闲置, 3:维修, 4:报废
private Date createTime;
private Date updateTime;
// getters and setters
}
// AssetDAO.java
public class AssetDAO {
public List<Asset> getAllAssets() {
List<Asset> assets = new ArrayList<>();
String sql = "SELECT * FROM assets ORDER BY createTime DESC";
try (Connection conn = DBConnectionUtil.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
Asset asset = new Asset();
asset.setId(rs.getInt("id"));
asset.setAssetCode(rs.getString("assetCode"));
asset.setAssetName(rs.getString("assetName"));
asset.setSpecification(rs.getString("specification"));
asset.setModel(rs.getString("model"));
asset.setCategoryId(rs.getString("categoryId"));
asset.setPurchasePrice(rs.getDouble("purchasePrice"));
asset.setPurchaseDate(rs.getDate("purchaseDate"));
asset.setSupplier(rs.getString("supplier"));
asset.setDepartmentId(rs.getString("departmentId"));
asset.setLocation(rs.getString("location"));
asset.setCustodian(rs.getString("custodian"));
asset.setStatus(rs.getInt("status"));
asset.setCreateTime(rs.getDate("createTime"));
asset.setUpdateTime(rs.getDate("updateTime"));
assets.add(asset);
}
} catch (SQLException e) {
e.printStackTrace();
}
return assets;
}
public boolean addAsset(Asset asset) {
String sql = "INSERT INTO assets (assetCode, assetName, specification, model, " +
"categoryId, purchasePrice, purchaseDate, supplier, departmentId, " +
"location, custodian, status, createTime, updateTime) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, GETDATE(), GETDATE())";
try (Connection conn = DBConnectionUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, asset.getAssetCode());
pstmt.setString(2, asset.getAssetName());
pstmt.setString(3, asset.getSpecification());
pstmt.setString(4, asset.getModel());
pstmt.setString(5, asset.getCategoryId());
pstmt.setDouble(6, asset.getPurchasePrice());
pstmt.setDate(7, new java.sql.Date(asset.getPurchaseDate().getTime()));
pstmt.setString(8, asset.getSupplier());
pstmt.setString(9, asset.getDepartmentId());
pstmt.setString(10, asset.getLocation());
pstmt.setString(11, asset.getCustodian());
pstmt.setInt(12, asset.getStatus());
int rows = pstmt.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public boolean updateAsset(Asset asset) {
String sql = "UPDATE assets SET assetName = ?, specification = ?, model = ?, " +
"categoryId = ?, purchasePrice = ?, purchaseDate = ?, supplier = ?, " +
"departmentId = ?, location = ?, custodian = ?, status = ?, " +
"updateTime = GETDATE() WHERE id = ?";
try (Connection conn = DBConnectionUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, asset.getAssetName());
pstmt.setString(2, asset.getSpecification());
pstmt.setString(3, asset.getModel());
pstmt.setString(4, asset.getCategoryId());
pstmt.setDouble(5, asset.getPurchasePrice());
pstmt.setDate(6, new java.sql.Date(asset.getPurchaseDate().getTime()));
pstmt.setString(7, asset.getSupplier());
pstmt.setString(8, asset.getDepartmentId());
pstmt.setString(9, asset.getLocation());
pstmt.setString(10, asset.getCustodian());
pstmt.setInt(11, asset.getStatus());
pstmt.setInt(12, asset.getId());
int rows = pstmt.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public boolean deleteAsset(int id) {
String sql = "DELETE FROM assets WHERE id = ?";
try (Connection conn = DBConnectionUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, id);
int rows = pstmt.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
}
// AssetUseDAO.java
public class AssetUseDAO {
public boolean recordAssetUse(AssetUseRecord record) {
String sql = "INSERT INTO asset_use_records (assetId, userId, useDate, reason, status, createTime) " +
"VALUES (?, ?, ?, ?, ?, GETDATE())";
try (Connection conn = DBConnectionUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, record.getAssetId());
pstmt.setInt(2, record.getUserId());
pstmt.setDate(3, new java.sql.Date(record.getUseDate().getTime()));
pstmt.setString(4, record.getReason());
pstmt.setInt(5, record.getStatus());
int rows = pstmt.executeUpdate();
// 更新资产状态
if (rows > 0) {
updateAssetStatus(record.getAssetId(), 1); // 在用
}
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
private void updateAssetStatus(int assetId, int status) {
String sql = "UPDATE assets SET status = ? WHERE id = ?";
try (Connection conn = DBConnectionUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, status);
pstmt.setInt(2, assetId);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<AssetUseRecord> getAssetUseRecordsByAssetId(int assetId) {
List<AssetUseRecord> records = new ArrayList<>();
String sql = "SELECT * FROM asset_use_records WHERE assetId = ? ORDER BY useDate DESC";
try (Connection conn = DBConnectionUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, assetId);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
AssetUseRecord record = new AssetUseRecord();
record.setId(rs.getInt("id"));
record.setAssetId(rs.getInt("assetId"));
record.setUserId(rs.getInt("userId"));
record.setUseDate(rs.getDate("useDate"));
record.setReason(rs.getString("reason"));
record.setStatus(rs.getInt("status"));
record.setCreateTime(rs.getDate("createTime"));
records.add(record);
}
} catch (SQLException e) {
e.printStackTrace();
}
return records;
}
}
// AssetInventoryService.java
public class AssetInventoryService {
private AssetDAO assetDAO = new AssetDAO();
private InventoryDAO inventoryDAO = new InventoryDAO();
public Inventory createInventory(String inventoryName, String creatorId, List<Integer> assetIds) {
Inventory inventory = new Inventory();
inventory.setInventoryName(inventoryName);
inventory.setCreatorId(creatorId);
inventory.setCreateTime(new Date());
inventory.setStatus(1); // 进行中
// 保存盘点单
inventoryDAO.saveInventory(inventory);
// 添加盘点资产
for (Integer assetId : assetIds) {
Asset asset = assetDAO.getAssetById(assetId);
if (asset != null) {
InventoryItem item = new InventoryItem();
item.setInventoryId(inventory.getId());
item.setAssetId(assetId);
item.setAssetCode(asset.getAssetCode());
item.setAssetName(asset.getAssetName());
item.setLocation(asset.getLocation());
item.setCustodian(asset.getCustodian());
item.setStatus(0); // 未盘点
inventoryDAO.saveInventoryItem(item);
}
}
return inventory;
}
public void completeInventoryItem(int itemId, int actualStatus, String notes) {
inventoryDAO.updateInventoryItemStatus(itemId, actualStatus, notes, new Date());
}
public InventoryReport generateInventoryReport(int inventoryId) {
InventoryReport report = new InventoryReport();
Inventory inventory = inventoryDAO.getInventoryById(inventoryId);
report.setInventory(inventory);
List<InventoryItem> items = inventoryDAO.getInventoryItemsByInventoryId(inventoryId);
report.setItems(items);
// 统计盘点结果
int totalCount = items.size();
int foundCount = 0;
int missingCount = 0;
int damagedCount = 0;
for (InventoryItem item : items) {
if (item.getStatus() == 1) { // 已盘点-存在
foundCount++;
} else if (item.getStatus() == 2) { // 已盘点-缺失
missingCount++;
} else if (item.getStatus() == 3) { // 已盘点-损坏
damagedCount++;
}
}
report.setTotalCount(totalCount);
report.setFoundCount(foundCount);
report.setMissingCount(missingCount);
report.setDamagedCount(damagedCount);
return report;
}
}
// MainFrame.java
public class MainFrame extends JFrame {
private JTabbedPane tabbedPane;
private AssetManagerPanel assetManagerPanel;
private AssetUsePanel assetUsePanel;
private InventoryPanel inventoryPanel;
private ReportPanel reportPanel;
public MainFrame() {
setTitle("企业固定资产管理系统");
setSize(1200, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
initComponents();
}
private void initComponents() {
// 菜单栏
JMenuBar menuBar = new JMenuBar();
JMenu systemMenu = new JMenu("系统");
JMenuItem loginItem = new JMenuItem("登录");
JMenuItem logoutItem = new JMenuItem("退出");
systemMenu.add(loginItem);
systemMenu.addSeparator();
systemMenu.add(logoutItem);
JMenu assetMenu = new JMenu("资产管理");
JMenuItem addAssetItem = new JMenuItem("新增资产");
JMenuItem manageAssetItem = new JMenuItem("资产列表");
JMenuItem categoryManageItem = new JMenuItem("资产分类");
assetMenu.add(addAssetItem);
assetMenu.add(manageAssetItem);
assetMenu.addSeparator();
assetMenu.add(categoryManageItem);
JMenu operationMenu = new JMenu("资产操作");
JMenuItem useItem = new JMenuItem("资产领用");
JMenuItem returnItem = new JMenuItem("资产归还");
JMenuItem transferItem = new JMenuItem("资产调拨");
JMenuItem repairItem = new JMenuItem("资产维修");
JMenuItem scrapItem = new JMenuItem("资产报废");
operationMenu.add(useItem);
operationMenu.add(returnItem);
operationMenu.add(transferItem);
operationMenu.add(repairItem);
operationMenu.add(scrapItem);
JMenu inventoryMenu = new JMenu("资产盘点");
JMenuItem createInventoryItem = new JMenuItem("创建盘点单");
JMenuItem manageInventoryItem = new JMenuItem("盘点管理");
inventoryMenu.add(createInventoryItem);
inventoryMenu.add(manageInventoryItem);
JMenu reportMenu = new JMenu("报表统计");
JMenuItem assetReportItem = new JMenuItem("资产报表");
JMenuItem inventoryReportItem = new JMenuItem("盘点报表");
JMenuItem depreciationReportItem = new JMenuItem("折旧报表");
reportMenu.add(assetReportItem);
reportMenu.add(inventoryReportItem);
reportMenu.add(depreciationReportItem);
menuBar.add(systemMenu);
menuBar.add(assetMenu);
menuBar.add(operationMenu);
menuBar.add(inventoryMenu);
menuBar.add(reportMenu);
setJMenuBar(menuBar);
// 标签页
tabbedPane = new JTabbedPane();
assetManagerPanel = new AssetManagerPanel();
assetUsePanel = new AssetUsePanel();
inventoryPanel = new InventoryPanel();
reportPanel = new ReportPanel();
tabbedPane.addTab("资产管理", assetManagerPanel);
tabbedPane.addTab("资产领用", assetUsePanel);
tabbedPane.addTab("资产盘点", inventoryPanel);
tabbedPane.addTab("报表统计", reportPanel);
add(tabbedPane, BorderLayout.CENTER);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
new MainFrame().setVisible(true);
});
}
}
// AssetManagerPanel.java
public class AssetManagerPanel extends JPanel {
private JTable assetTable;
private AssetTableModel tableModel;
private JComboBox<String> statusComboBox;
private JComboBox<String> categoryComboBox;
private JTextField searchField;
public AssetManagerPanel() {
setLayout(new BorderLayout());
// 初始化工具栏
initToolBar();
// 初始化表格
tableModel = new AssetTableModel();
assetTable = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(assetTable);
add(scrollPane, BorderLayout.CENTER);
// 加载资产数据
loadAssets();
}
private void initToolBar() {
JPanel toolBarPanel = new JPanel();
toolBarPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
// 添加资产按钮
JButton addButton = new JButton("添加资产");
addButton.addActionListener(e -> openAddAssetDialog());
// 编辑资产按钮
JButton editButton = new JButton("编辑资产");
editButton.addActionListener(e -> openEditAssetDialog());
// 删除资产按钮
JButton deleteButton = new JButton("删除资产");
deleteButton.addActionListener(e -> deleteSelectedAsset());
// 刷新按钮
JButton refreshButton = new JButton("刷新");
refreshButton.addActionListener(e -> loadAssets());
// 导出按钮
JButton exportButton = new JButton("导出");
exportButton.addActionListener(e -> exportAssets());
// 搜索组件
statusComboBox = new JComboBox<>(new String[] {"全部状态", "在用", "闲置", "维修", "报废"});
initCategoryComboBox();
searchField = new JTextField(20);
JButton searchButton = new JButton("搜索");
searchButton.addActionListener(e -> searchAssets());
toolBarPanel.add(addButton);
toolBarPanel.add(editButton);
toolBarPanel.add(deleteButton);
toolBarPanel.add(new JSeparator(SwingConstants.VERTICAL));
toolBarPanel.add(refreshButton);
toolBarPanel.add(exportButton);
toolBarPanel.add(new JSeparator(SwingConstants.VERTICAL));
toolBarPanel.add(statusComboBox);
toolBarPanel.add(categoryComboBox);
toolBarPanel.add(searchField);
toolBarPanel.add(searchButton);
add(toolBarPanel, BorderLayout.NORTH);
}
// 其他方法省略...
}
// AssetDAOTest.java
public class AssetDAOTest {
private AssetDAO assetDAO = new AssetDAO();
@Before
public void setUp() {
// 初始化测试环境
}
@Test
public void testAddAsset() {
Asset asset = new Asset();
asset.setAssetCode("A0001");
asset.setAssetName("联想笔记本电脑");
asset.setSpecification("i7-12700H/16G/512G");
asset.setModel("ThinkPad X1 Carbon");
asset.setCategoryId("1"); // 电子设备
asset.setPurchasePrice(12999.00);
asset.setPurchaseDate(new Date());
asset.setSupplier("联想科技有限公司");
asset.setDepartmentId("101"); // 研发部
asset.setLocation("A栋301室");
asset.setCustodian("张三");
asset.setStatus(1); // 在用
boolean result = assetDAO.addAsset(asset);
assertTrue(result);
}
@Test
public void testUpdateAsset() {
// 先添加一个资产
Asset asset = new Asset();
asset.setAssetCode("A0002");
asset.setAssetName("戴尔台式机");
asset.setSpecification("i5-12400/8G/512G");
asset.setModel("OptiPlex 7010");
asset.setCategoryId("1"); // 电子设备
asset.setPurchasePrice(5999.00);
asset.setPurchaseDate(new Date());
asset.setSupplier("戴尔科技有限公司");
asset.setDepartmentId("102"); // 市场部
asset.setLocation("B栋201室");
asset.setCustodian("李四");
asset.setStatus(1); // 在用
assetDAO.addAsset(asset);
// 获取资产并更新
List<Asset> assets = assetDAO.getAllAssets();
Asset toUpdate = assets.get(0);
toUpdate.setLocation("B栋202室");
toUpdate.setCustodian("王五");
boolean result = assetDAO.updateAsset(toUpdate);
assertTrue(result);
}
@Test
public void testDeleteAsset() {
// 先添加一个资产
Asset asset = new Asset();
asset.setAssetCode("A0003");
asset.setAssetName("打印机");
asset.setSpecification("黑白激光");
asset.setModel("HP M402dne");
asset.setCategoryId("2"); // 办公设备
asset.setPurchasePrice(3299.00);
asset.setPurchaseDate(new Date());
asset.setSupplier("惠普科技有限公司");
asset.setDepartmentId("103"); // 财务部
asset.setLocation("C栋101室");
asset.setCustodian("赵六");
asset.setStatus(1); // 在用
assetDAO.addAsset(asset);
// 获取资产ID并删除
List<Asset> assets = assetDAO.getAllAssets();
int id = assets.get(0).getId();
boolean result = assetDAO.deleteAsset(id);
assertTrue(result);
}
}
本系统实现了企业固定资产的信息化管理,采用Java+SQL Server技术栈,具有良好的可扩展性和维护性。系统覆盖了固定资产全生命周期管理,提供了完善的功能模块和报表统计,可有效提升企业固定资产管理效率和水平。