1、使用JAVA语言建立一个信息管理系统 2、使用SQLServer数据库存储数据。 3、使用swing开发界面,界面尽量的协调、美观。
时间: 2023-12-03 16:46:08 浏览: 30
好的,建立一个信息管理系统需要考虑以下几个方面:
1. 系统功能
根据需求,分析出系统需要实现哪些功能,如添加、删除、修改、查询等。
2. 数据库设计
设计数据库结构,包括数据表、字段、关系等,考虑数据的完整性和一致性。
3. 编写Java代码
根据功能需求,编写Java代码实现相应的功能,并与SQLServer数据库交互。
4. 设计界面
使用swing开发界面,设计协调美观的界面,提高用户体验。
以下是一个简单的信息管理系统的示例代码:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class InfoSystem extends JFrame implements ActionListener {
// 定义组件
JLabel label1, label2, label3, label4, label5, label6;
JTextField tf1, tf2, tf3, tf4, tf5, tf6;
JButton btn1, btn2, btn3, btn4, btn5;
JTable table;
// 数据库连接相关
Connection conn;
Statement stmt;
ResultSet rs;
// 构造方法
public InfoSystem() {
// 初始化组件
label1 = new JLabel("ID");
label2 = new JLabel("Name");
label3 = new JLabel("Gender");
label4 = new JLabel("Age");
label5 = new JLabel("Phone");
label6 = new JLabel("Address");
tf1 = new JTextField(10);
tf2 = new JTextField(10);
tf3 = new JTextField(10);
tf4 = new JTextField(10);
tf5 = new JTextField(10);
tf6 = new JTextField(10);
btn1 = new JButton("Add");
btn2 = new JButton("Delete");
btn3 = new JButton("Update");
btn4 = new JButton("Search");
btn5 = new JButton("Refresh");
table = new JTable();
// 添加组件到容器
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(6, 2));
p1.add(label1);
p1.add(tf1);
p1.add(label2);
p1.add(tf2);
p1.add(label3);
p1.add(tf3);
p1.add(label4);
p1.add(tf4);
p1.add(label5);
p1.add(tf5);
p1.add(label6);
p1.add(tf6);
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(btn1);
p2.add(btn2);
p2.add(btn3);
p2.add(btn4);
p2.add(btn5);
JPanel p3 = new JPanel();
p3.setLayout(new BorderLayout());
p3.add(new JScrollPane(table), BorderLayout.CENTER);
Container c = getContentPane();
c.setLayout(new BorderLayout());
c.add(p1, BorderLayout.NORTH);
c.add(p2, BorderLayout.SOUTH);
c.add(p3, BorderLayout.CENTER);
// 添加事件监听器
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
// 初始化数据库连接
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=InfoSystem", "username", "password");
stmt = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
// 窗口设置
setTitle("Info System");
setSize(800, 600);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
// 事件处理方法
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) { // 添加数据
try {
String sql = "INSERT INTO Info (ID, Name, Gender, Age, Phone, Address) VALUES ('" + tf1.getText() + "', '" + tf2.getText() + "', '" + tf3.getText() + "', " + tf4.getText() + ", '" + tf5.getText() + "', '" + tf6.getText() + "')";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(this, "Add success!");
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Add failed!");
}
} else if (e.getSource() == btn2) { // 删除数据
try {
String sql = "DELETE FROM Info WHERE ID='" + tf1.getText() + "'";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(this, "Delete success!");
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Delete failed!");
}
} else if (e.getSource() == btn3) { // 更新数据
try {
String sql = "UPDATE Info SET Name='" + tf2.getText() + "', Gender='" + tf3.getText() + "', Age=" + tf4.getText() + ", Phone='" + tf5.getText() + "', Address='" + tf6.getText() + "' WHERE ID='" + tf1.getText() + "'";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(this, "Update success!");
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Update failed!");
}
} else if (e.getSource() == btn4) { // 查询数据
try {
String sql = "SELECT * FROM Info WHERE ID='" + tf1.getText() + "'";
rs = stmt.executeQuery(sql);
if (rs.next()) {
tf2.setText(rs.getString("Name"));
tf3.setText(rs.getString("Gender"));
tf4.setText(rs.getString("Age"));
tf5.setText(rs.getString("Phone"));
tf6.setText(rs.getString("Address"));
} else {
JOptionPane.showMessageDialog(this, "No result!");
}
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Search failed!");
}
} else if (e.getSource() == btn5) { // 刷新数据
try {
String sql = "SELECT * FROM Info";
rs = stmt.executeQuery(sql);
table.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
// 入口方法
public static void main(String[] args) {
new InfoSystem();
}
}
```