2、创建jdbc项目,实现数据库连接,使用逻辑三层结构完成如下功能: 1)输入一个员工的信息,完成员工的添加。 2)查询所有员工信息; 3)输入姓名,查询员工信息;
时间: 2024-01-22 10:18:25 浏览: 60
jdbc连接数据库的步骤给大家分享了
好的,我来为您解答。
首先,我们需要创建一个 Java 项目,并添加 JDBC 驱动包以便连接数据库。这里以 MySQL 数据库为例,以下是代码实现:
1. 创建数据库表
在 MySQL 中创建一个名为 employee 的表,包含 id、name、age、email 四个字段。可以使用如下 SQL 语句:
```sql
CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`age` int(11) NOT NULL,
`email` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
2. 创建实体类
创建一个名为 Employee 的实体类,包含 id、name、age、email 四个属性,并提供 getter 和 setter 方法。
```java
public class Employee {
private int id;
private String name;
private int age;
private String email;
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
```
3. 创建 DAO 层
创建一个名为 EmployeeDAO 的类,包含添加员工、查询所有员工、根据姓名查询员工三个方法,并实现数据库连接和 SQL 语句的执行。以下是代码实现:
```java
public class EmployeeDAO {
// 数据库连接信息
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/test";
private static final String USERNAME = "root";
private static final String PASSWORD = "123456";
// 添加员工
public void addEmployee(Employee employee) throws SQLException, ClassNotFoundException {
Class.forName(DRIVER);
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
PreparedStatement statement = connection.prepareStatement("INSERT INTO employee(name, age, email) VALUES (?, ?, ?)");
statement.setString(1, employee.getName());
statement.setInt(2, employee.getAge());
statement.setString(3, employee.getEmail());
statement.executeUpdate();
statement.close();
connection.close();
}
// 查询所有员工
public List<Employee> getAllEmployees() throws SQLException, ClassNotFoundException {
Class.forName(DRIVER);
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM employee");
List<Employee> employees = new ArrayList<>();
while (resultSet.next()) {
Employee employee = new Employee();
employee.setId(resultSet.getInt("id"));
employee.setName(resultSet.getString("name"));
employee.setAge(resultSet.getInt("age"));
employee.setEmail(resultSet.getString("email"));
employees.add(employee);
}
resultSet.close();
statement.close();
connection.close();
return employees;
}
// 根据姓名查询员工
public Employee getEmployeeByName(String name) throws SQLException, ClassNotFoundException {
Class.forName(DRIVER);
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
PreparedStatement statement = connection.prepareStatement("SELECT * FROM employee WHERE name = ?");
statement.setString(1, name);
ResultSet resultSet = statement.executeQuery();
Employee employee = null;
if (resultSet.next()) {
employee = new Employee();
employee.setId(resultSet.getInt("id"));
employee.setName(resultSet.getString("name"));
employee.setAge(resultSet.getInt("age"));
employee.setEmail(resultSet.getString("email"));
}
resultSet.close();
statement.close();
connection.close();
return employee;
}
}
```
4. 创建 Service 层
创建一个名为 EmployeeService 的类,包含添加员工、查询所有员工、根据姓名查询员工三个方法,并调用 DAO 层的方法进行数据库操作。以下是代码实现:
```java
public class EmployeeService {
private EmployeeDAO employeeDAO = new EmployeeDAO();
// 添加员工
public void addEmployee(Employee employee) throws SQLException, ClassNotFoundException {
employeeDAO.addEmployee(employee);
}
// 查询所有员工
public List<Employee> getAllEmployees() throws SQLException, ClassNotFoundException {
return employeeDAO.getAllEmployees();
}
// 根据姓名查询员工
public Employee getEmployeeByName(String name) throws SQLException, ClassNotFoundException {
return employeeDAO.getEmployeeByName(name);
}
}
```
5. 创建 Presentation 层
创建一个名为 EmployeeUI 的类,包含添加员工、查询所有员工、根据姓名查询员工三个方法的实现,并调用 Service 层的方法进行数据库操作。以下是代码实现:
```java
public class EmployeeUI {
private EmployeeService employeeService = new EmployeeService();
// 添加员工
public void addEmployee() throws SQLException, ClassNotFoundException {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入员工姓名:");
String name = scanner.nextLine();
System.out.print("请输入员工年龄:");
int age = scanner.nextInt();
scanner.nextLine();
System.out.print("请输入员工邮箱:");
String email = scanner.nextLine();
Employee employee = new Employee();
employee.setName(name);
employee.setAge(age);
employee.setEmail(email);
employeeService.addEmployee(employee);
System.out.println("员工添加成功!");
}
// 查询所有员工
public void getAllEmployees() throws SQLException, ClassNotFoundException {
List<Employee> employees = employeeService.getAllEmployees();
System.out.println("所有员工信息如下:");
for (Employee employee : employees) {
System.out.println("id:" + employee.getId() + ",姓名:" + employee.getName() + ",年龄:" + employee.getAge() + ",邮箱:" + employee.getEmail());
}
}
// 根据姓名查询员工
public void getEmployeeByName() throws SQLException, ClassNotFoundException {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入员工姓名:");
String name = scanner.nextLine();
Employee employee = employeeService.getEmployeeByName(name);
if (employee != null) {
System.out.println("员工信息如下:");
System.out.println("id:" + employee.getId() + ",姓名:" + employee.getName() + ",年龄:" + employee.getAge() + ",邮箱:" + employee.getEmail());
} else {
System.out.println("未找到该员工!");
}
}
}
```
6. 编写主程序
创建一个名为 Main 的主程序,包含添加员工、查询所有员工、根据姓名查询员工三个功能的实现。以下是代码实现:
```java
public class Main {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
EmployeeUI employeeUI = new EmployeeUI();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择要进行的操作:");
System.out.println("1. 添加员工");
System.out.println("2. 查询所有员工");
System.out.println("3. 根据姓名查询员工");
System.out.println("0. 退出程序");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
employeeUI.addEmployee();
break;
case 2:
employeeUI.getAllEmployees();
break;
case 3:
employeeUI.getEmployeeByName();
break;
case 0:
System.out.println("程序已退出!");
System.exit(0);
default:
System.out.println("请输入正确的操作!");
}
}
}
}
```
以上就是使用逻辑三层结构完成输入一个员工的信息,完成员工的添加;查询所有员工信息;输入姓名,查询员工信息的代码实现。
阅读全文