Java编写资产管理系统窗体,并连接MySQL数据库,自动增加
时间: 2023-06-30 22:06:34 浏览: 151
好的,我可以为您提供一些代码示例,帮助您开始编写Java资产管理系统窗体并连接MySQL数据库。
首先,您需要下载并安装MySQL数据库,然后创建一个名为“asset_management”的数据库。在该数据库中,您需要创建一个名为“assets”的表,其中包含以下列:id(INT,主键,自动增加),name(VARCHAR),description(VARCHAR),value(DOUBLE)和acquisition_date(DATE)。
接下来,您需要使用Java连接到MySQL数据库。这可以通过使用Java JDBC驱动程序来实现。以下是一个示例代码:
```java
import java.sql.*;
public class MySQLConnector {
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost/asset_management";
private static final String USER = "your_username";
private static final String PASS = "your_password";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
```
这个类使用了静态方法`getConnection()`来连接到MySQL数据库。您需要将`your_username`和`your_password`分别替换为您的MySQL用户名和密码。
接下来,您可以编写一个Java类来表示资产,并使用该类在Java窗体中显示和管理资产。以下是一个示例代码:
```java
import java.util.Date;
public class Asset {
private int id;
private String name;
private String description;
private double value;
private Date acquisitionDate;
public Asset(int id, String name, String description, double value, Date acquisitionDate) {
this.id = id;
this.name = name;
this.description = description;
this.value = value;
this.acquisitionDate = acquisitionDate;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public double getValue() {
return value;
}
public Date getAcquisitionDate() {
return acquisitionDate;
}
}
```
这个类表示一个资产,并包含`id`,`name`,`description`,`value`和`acquisitionDate`属性。您可以使用这个类来在Java窗体中显示和管理资产。
最后,您需要编写Java窗体以显示和管理资产。以下是一个示例代码:
```java
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Date;
public class AssetManagementSystem extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private static final String[] COLUMNS = {"ID", "Name", "Description", "Value", "Acquisition Date"};
private static final String[] FIELD_NAMES = {"Name", "Description", "Value", "Acquisition Date (YYYY-MM-DD)"};
private JTextField[] fields;
private JButton addButton;
private JTable table;
private DefaultTableModel model;
private ArrayList<Asset> assets;
public AssetManagementSystem() {
super("Asset Management System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Create fields for asset details
JPanel fieldPanel = new JPanel(new GridLayout(FIELD_NAMES.length, 2));
fields = new JTextField[FIELD_NAMES.length];
for (int i = 0; i < FIELD_NAMES.length; i++) {
fieldPanel.add(new JLabel(FIELD_NAMES[i]));
fields[i] = new JTextField();
fieldPanel.add(fields[i]);
}
add(fieldPanel, BorderLayout.NORTH);
// Create add button
addButton = new JButton("Add Asset");
addButton.addActionListener(this);
add(addButton, BorderLayout.SOUTH);
// Create table to display assets
model = new DefaultTableModel(COLUMNS, 0);
table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER);
// Load assets from database
assets = new ArrayList<>();
try {
Connection conn = MySQLConnector.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM assets");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String description = rs.getString("description");
double value = rs.getDouble("value");
Date acquisitionDate = rs.getDate("acquisition_date");
Asset asset = new Asset(id, name, description, value, acquisitionDate);
assets.add(asset);
model.addRow(new Object[] {id, name, description, value, acquisitionDate});
}
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
// Configure window
setSize(800, 600);
setVisible(true);
}
public static void main(String[] args) {
new AssetManagementSystem();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
try {
Connection conn = MySQLConnector.getConnection();
PreparedStatement stmt = conn.prepareStatement("INSERT INTO assets (name, description, value, acquisition_date) VALUES (?, ?, ?, ?)");
stmt.setString(1, fields[0].getText());
stmt.setString(2, fields[1].getText());
stmt.setDouble(3, Double.parseDouble(fields[2].getText()));
stmt.setDate(4, Date.valueOf(fields[3].getText()));
stmt.executeUpdate();
stmt.close();
conn.close();
Asset asset = new Asset(assets.size() + 1, fields[0].getText(), fields[1].getText(), Double.parseDouble(fields[2].getText()), Date.valueOf(fields[3].getText()));
assets.add(asset);
model.addRow(new Object[] {asset.getId(), asset.getName(), asset.getDescription(), asset.getValue(), asset.getAcquisitionDate()});
for (JTextField field : fields) {
field.setText("");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
```
这个窗体使用`Asset`类表示资产,并在窗体中显示和管理资产。窗体包括一个表格来显示资产,一个文本框来输入新资产的详细信息,以及一个“添加资产”按钮来将新资产添加到数据库和表格中。
当用户单击“添加资产”按钮时,窗体将连接到MySQL数据库并将新资产添加到数据库和表格中。如果发生任何错误,窗体将打印出堆栈跟踪以帮助您调试代码。
希望这个示例代码可以帮助您开始编写Java资产管理系统窗体并连接MySQL数据库!
阅读全文