怎么用JAVA来写一个系统,代码怎么写
时间: 2023-08-16 09:07:06 浏览: 61
写一个系统的代码需要根据具体的需求和功能来进行设计和实现。下面是一个简单的Java系统代码示例,以医生管理为例:
1.首先需要定义一个Doctor类,包含医生的基本信息。
```
public class Doctor {
private int id;
private String name;
private int age;
private String gender;
private String department;
private String position;
// 构造函数
public Doctor(int id, String name, int age, String gender, String department, String position) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
this.position = position;
}
// getter和setter方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
}
```
2.然后需要定义一个DoctorDao接口,包含医生信息的增删改查等操作。
```
public interface DoctorDao {
// 添加医生
public void addDoctor(Doctor doctor);
// 删除医生
public void deleteDoctor(int id);
// 更新医生信息
public void updateDoctor(Doctor doctor);
// 根据id查找医生
public Doctor findDoctorById(int id);
// 获取所有医生信息
public List<Doctor> getAllDoctors();
}
```
3.接着实现DoctorDao接口,使用MySQL数据库进行数据存储。
```
public class DoctorDaoImpl implements DoctorDao {
// 数据库连接
private Connection conn;
// 构造函数
public DoctorDaoImpl(Connection conn) {
this.conn = conn;
}
@Override
public void addDoctor(Doctor doctor) {
String sql = "insert into doctor (id, name, age, gender, department, position) values (?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, doctor.getId());
pstmt.setString(2, doctor.getName());
pstmt.setInt(3, doctor.getAge());
pstmt.setString(4, doctor.getGender());
pstmt.setString(5, doctor.getDepartment());
pstmt.setString(6, doctor.getPosition());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void deleteDoctor(int id) {
String sql = "delete from doctor where id = ?";
PreparedStatement pstmt;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void updateDoctor(Doctor doctor) {
String sql = "update doctor set name=?, age=?, gender=?, department=?, position=? where id=?";
PreparedStatement pstmt;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, doctor.getName());
pstmt.setInt(2, doctor.getAge());
pstmt.setString(3, doctor.getGender());
pstmt.setString(4, doctor.getDepartment());
pstmt.setString(5, doctor.getPosition());
pstmt.setInt(6, doctor.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public Doctor findDoctorById(int id) {
String sql = "select * from doctor where id=?";
Doctor doctor = null;
PreparedStatement pstmt;
ResultSet rs;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
doctor = new Doctor(rs.getInt("id"), rs.getString("name"), rs.getInt("age"), rs.getString("gender"), rs.getString("department"), rs.getString("position"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return doctor;
}
@Override
public List<Doctor> getAllDoctors() {
String sql = "select * from doctor";
List<Doctor> doctors = new ArrayList<Doctor>();
PreparedStatement pstmt;
ResultSet rs;
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
Doctor doctor = new Doctor(rs.getInt("id"), rs.getString("name"), rs.getInt("age"), rs.getString("gender"), rs.getString("department"), rs.getString("position"));
doctors.add(doctor);
}
} catch (SQLException e) {
e.printStackTrace();
}
return doctors;
}
}
```
4.最后在主函数中进行测试。
```
public class Test {
public static void main(String[] args) {
Connection conn = DBUtil.getConnection(); // 连接数据库
DoctorDao dao = new DoctorDaoImpl(conn); // 创建DoctorDaoImpl实例
// 添加医生
Doctor doctor1 = new Doctor(1, "张三", 30, "男", "内科", "主治医师");
dao.addDoctor(doctor1);
Doctor doctor2 = new Doctor(2, "李四", 35, "女", "外科", "副主任医师");
dao.addDoctor(doctor2);
// 获取所有医生信息
List<Doctor> doctors = dao.getAllDoctors();
for (Doctor doctor : doctors) {
System.out.println(doctor.getName());
}
// 更新医生信息
Doctor doctor3 = new Doctor(1, "张三", 30, "男", "外科", "主任医师");
dao.updateDoctor(doctor3);
// 根据id查找医生
Doctor doctor4 = dao.findDoctorById(1);
System.out.println(doctor4.getName());
// 删除医生
dao.deleteDoctor(1);
dao.deleteDoctor(2);
}
}
```
以上是一个简单的医生管理系统的Java代码实现示例。具体的系统开发需要根据具体需求进行设计和实现。
阅读全文