请用Java+SQL SERVER 2008设计一个简单的同学通讯录数据库,数据库名为classmates,设计一个或多个表保存同学通讯信息,如 姓名、性别、年龄、电话、专业、家庭地址等;运用Java GUI设计一个数据库操作界面,功能包括: (1) 允许输入同学信息,添加到数据库中;(2) 允许按条件查询同学信息,比如 按年龄查询、按姓名查询、多个条件综合查询等,查询结果列表在界面上; (3) 可以更新已有同学的信息。
时间: 2024-03-12 22:47:28 浏览: 98
好的,我会根据您的要求进行设计和回答。
首先,我们需要创建一个名为classmates的数据库,并在其中创建一个名为student的表来保存同学通讯信息。student表的字段包括:姓名、性别、年龄、电话、专业、家庭地址。
创建student表的SQL代码如下:
```
CREATE TABLE student (
id INT NOT NULL PRIMARY KEY IDENTITY(1,1),
name VARCHAR(50) NOT NULL,
gender VARCHAR(10) NOT NULL,
age INT NOT NULL,
phone VARCHAR(20) NOT NULL,
major VARCHAR(50) NOT NULL,
address VARCHAR(100)
);
```
接下来,我们需要编写Java代码来实现数据库操作界面。首先,我们需要连接到SQL Server数据库,使用JDBC技术进行操作。以下是连接数据库的Java代码:
```java
import java.sql.*;
public class DatabaseConnection {
private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=classmates";
private static final String USERNAME = "your_username";
private static final String PASSWORD = "your_password";
private static Connection conn;
public static Connection getConnection() {
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
return conn;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
```
接下来,我们可以实现添加同学信息的功能。以下是Java代码:
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class AddStudentFrame extends JFrame {
private JLabel nameLabel, genderLabel, ageLabel, phoneLabel, majorLabel, addressLabel;
private JTextField nameField, genderField, ageField, phoneField, majorField, addressField;
private JButton addButton, resetButton;
public AddStudentFrame() {
nameLabel = new JLabel("姓名:");
genderLabel = new JLabel("性别:");
ageLabel = new JLabel("年龄:");
phoneLabel = new JLabel("电话:");
majorLabel = new JLabel("专业:");
addressLabel = new JLabel("家庭地址:");
nameField = new JTextField(10);
genderField = new JTextField(10);
ageField = new JTextField(10);
phoneField = new JTextField(10);
majorField = new JTextField(10);
addressField = new JTextField(30);
addButton = new JButton("添加");
resetButton = new JButton("重置");
JPanel panel = new JPanel(new GridLayout(7, 2));
panel.add(nameLabel);
panel.add(nameField);
panel.add(genderLabel);
panel.add(genderField);
panel.add(ageLabel);
panel.add(ageField);
panel.add(phoneLabel);
panel.add(phoneField);
panel.add(majorLabel);
panel.add(majorField);
panel.add(addressLabel);
panel.add(addressField);
panel.add(addButton);
panel.add(resetButton);
add(panel);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Connection conn = DatabaseConnection.getConnection();
String sql = "INSERT INTO student (name, gender, age, phone, major, address) " +
"VALUES (?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, nameField.getText());
pstmt.setString(2, genderField.getText());
pstmt.setInt(3, Integer.parseInt(ageField.getText()));
pstmt.setString(4, phoneField.getText());
pstmt.setString(5, majorField.getText());
pstmt.setString(6, addressField.getText());
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null, "添加成功!");
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "添加失败!");
}
}
});
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
nameField.setText("");
genderField.setText("");
ageField.setText("");
phoneField.setText("");
majorField.setText("");
addressField.setText("");
}
});
setTitle("添加同学信息");
setSize(400, 250);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
}
```
这段代码实现了一个添加同学信息的窗口,包含姓名、性别、年龄、电话、专业、家庭地址等字段,用户输入信息后,点击添加按钮即可将信息添加到数据库中。
接下来,我们可以实现按条件查询同学信息的功能。以下是Java代码:
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
public class QueryStudentFrame extends JFrame {
private JLabel nameLabel, ageLabel;
private JTextField nameField, ageField;
private JButton queryButton;
private JTable table;
private DefaultTableModel model;
public QueryStudentFrame() {
nameLabel = new JLabel("姓名:");
ageLabel = new JLabel("年龄:");
nameField = new JTextField(10);
ageField = new JTextField(10);
queryButton = new JButton("查询");
JPanel panel1 = new JPanel(new GridLayout(2, 2));
panel1.add(nameLabel);
panel1.add(nameField);
panel1.add(ageLabel);
panel1.add(ageField);
JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel2.add(queryButton);
JPanel panel3 = new JPanel(new BorderLayout());
JScrollPane scrollPane = new JScrollPane();
table = new JTable();
model = new DefaultTableModel();
model.addColumn("ID");
model.addColumn("姓名");
model.addColumn("性别");
model.addColumn("年龄");
model.addColumn("电话");
model.addColumn("专业");
model.addColumn("家庭地址");
table.setModel(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
scrollPane.setViewportView(table);
panel3.add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.add(panel1, BorderLayout.NORTH);
panel.add(panel2, BorderLayout.SOUTH);
panel.add(panel3, BorderLayout.CENTER);
add(panel);
queryButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Connection conn = DatabaseConnection.getConnection();
String sql = "SELECT * FROM student WHERE 1=1";
if (!nameField.getText().equals("")) {
sql += " AND name LIKE '%" + nameField.getText() + "%'";
}
if (!ageField.getText().equals("")) {
sql += " AND age = " + ageField.getText();
}
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
model.setRowCount(0);
while (rs.next()) {
Object[] row = new Object[7];
row[0] = rs.getInt("id");
row[1] = rs.getString("name");
row[2] = rs.getString("gender");
row[3] = rs.getInt("age");
row[4] = rs.getString("phone");
row[5] = rs.getString("major");
row[6] = rs.getString("address");
model.addRow(row);
}
rs.close();
stmt.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "查询失败!");
}
}
});
setTitle("查询同学信息");
setSize(800, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
}
```
这段代码实现了一个按条件查询同学信息的窗口,包含姓名、年龄等查询条件,用户输入条件后,点击查询按钮即可在界面上显示查询结果。
最后,我们可以实现更新已有同学的信息的功能。以下是Java代码:
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class UpdateStudentFrame extends JFrame {
private JLabel idLabel, nameLabel, genderLabel, ageLabel, phoneLabel, majorLabel, addressLabel;
private JTextField idField, nameField, genderField, ageField, phoneField, majorField, addressField;
private JButton queryButton, updateButton;
public UpdateStudentFrame() {
idLabel = new JLabel("ID:");
nameLabel = new JLabel("姓名:");
genderLabel = new JLabel("性别:");
ageLabel = new JLabel("年龄:");
phoneLabel = new JLabel("电话:");
majorLabel = new JLabel("专业:");
addressLabel = new JLabel("家庭地址:");
idField = new JTextField(10);
nameField = new JTextField(10);
genderField = new JTextField(10);
ageField = new JTextField(10);
phoneField = new JTextField(10);
majorField = new JTextField(10);
addressField = new JTextField(30);
queryButton = new JButton("查询");
updateButton = new JButton("更新");
JPanel panel1 = new JPanel(new GridLayout(7, 2));
panel1.add(idLabel);
panel1.add(idField);
panel1.add(nameLabel);
panel1.add(nameField);
panel1.add(genderLabel);
panel1.add(genderField);
panel1.add(ageLabel);
panel1.add(ageField);
panel1.add(phoneLabel);
panel1.add(phoneField);
panel1.add(majorLabel);
panel1.add(majorField);
panel1.add(addressLabel);
panel1.add(addressField);
JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panel2.add(queryButton);
panel2.add(updateButton);
JPanel panel = new JPanel(new BorderLayout());
panel.add(panel1, BorderLayout.CENTER);
panel.add(panel2, BorderLayout.SOUTH);
add(panel);
queryButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Connection conn = DatabaseConnection.getConnection();
String sql = "SELECT * FROM student WHERE id = " + idField.getText();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
nameField.setText(rs.getString("name"));
genderField.setText(rs.getString("gender"));
ageField.setText(rs.getInt("age") + "");
phoneField.setText(rs.getString("phone"));
majorField.setText(rs.getString("major"));
addressField.setText(rs.getString("address"));
} else {
JOptionPane.showMessageDialog(null, "查无此人!");
}
rs.close();
stmt.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "查询失败!");
}
}
});
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Connection conn = DatabaseConnection.getConnection();
String sql = "UPDATE student SET name = ?, gender = ?, age = ?, phone = ?, major = ?, address = ? " +
"WHERE id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, nameField.getText());
pstmt.setString(2, genderField.getText());
pstmt.setInt(3, Integer.parseInt(ageField.getText()));
pstmt.setString(4, phoneField.getText());
pstmt.setString(5, majorField.getText());
pstmt.setString(6, addressField.getText());
pstmt.setInt(7, Integer.parseInt(idField.getText()));
pstmt.executeUpdate();
JOptionPane.showMessageDialog(null, "更新成功!");
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "更新失败!");
}
}
});
setTitle("更新同学信息");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
}
```
这段代码实现了一个更新同学信息的窗口,用户输入要更新的同学的ID和更新后的信息,点击查询按钮即可在界面上显示要更新的同学的信息,点击更新按钮即可将信息更新到数据库中。
以上就是我使用Java和SQL Server 2008实现的简单同学通讯录数据库以及相应的操作界面。
阅读全文