创建职工表和部门信息表(字段自定义,数据自定义)用JavaEE实现
时间: 2024-02-21 13:57:46 浏览: 81
好的,以下是使用JavaEE实现创建职工表和部门信息表的步骤:
1. 首先,您需要创建一个JavaEE项目,并在其中添加JDBC驱动程序,以便连接到您的数据库。您可以使用以下代码连接到Access数据库:
```
String url = "jdbc:ucanaccess://C:/path/to/your/database.accdb";
Connection conn = DriverManager.getConnection(url);
```
2. 接下来,您需要编写Java代码来创建职工表和部门信息表。以下是一个示例:
```
// 创建职工表
String createEmployeeTable = "CREATE TABLE Employee ("
+ "id INT NOT NULL AUTO_INCREMENT,"
+ "name VARCHAR(50) NOT NULL,"
+ "employee_id VARCHAR(20) NOT NULL,"
+ "department_id VARCHAR(20) NOT NULL,"
+ "position VARCHAR(50) NOT NULL,"
+ "hire_date DATE NOT NULL,"
+ "PRIMARY KEY (id)"
+ ");";
// 创建部门信息表
String createDepartmentTable = "CREATE TABLE Department ("
+ "id INT NOT NULL AUTO_INCREMENT,"
+ "department_id VARCHAR(20) NOT NULL,"
+ "department_name VARCHAR(50) NOT NULL,"
+ "department_head VARCHAR(50) NOT NULL,"
+ "PRIMARY KEY (id)"
+ ");";
// 执行SQL语句
Statement statement = conn.createStatement();
statement.executeUpdate(createEmployeeTable);
statement.executeUpdate(createDepartmentTable);
```
3. 然后,您需要添加一些示例数据到职工表和部门信息表中。以下是一个示例:
```
// 添加示例数据到职工表
String insertEmployeeData = "INSERT INTO Employee (name, employee_id, department_id, position, hire_date) VALUES "
+ "('张三', '1001', 'D001', '经理', '2021-01-01'), "
+ "('李四', '1002', 'D002', '工程师', '2021-02-01'), "
+ "('王五', '1003', 'D002', '销售员', '2021-03-01');";
// 添加示例数据到部门信息表
String insertDepartmentData = "INSERT INTO Department (department_id, department_name, department_head) VALUES "
+ "('D001', '研发部门', '张三'), "
+ "('D002', '销售部门', '李四');";
// 执行SQL语句
Statement statement = conn.createStatement();
statement.executeUpdate(insertEmployeeData);
statement.executeUpdate(insertDepartmentData);
```
4. 最后,您需要在JavaEE项目中创建一个Servlet或JSP页面,以便向用户显示职工表和部门信息表中的数据。您可以使用以下代码查询数据库并将结果显示在页面上:
```
// 查询职工表中的数据
String selectEmployeeData = "SELECT * FROM Employee;";
ResultSet employeeResultSet = statement.executeQuery(selectEmployeeData);
// 查询部门信息表中的数据
String selectDepartmentData = "SELECT * FROM Department;";
ResultSet departmentResultSet = statement.executeQuery(selectDepartmentData);
// 将结果显示在页面上
while (employeeResultSet.next()) {
String name = employeeResultSet.getString("name");
String employeeId = employeeResultSet.getString("employee_id");
String departmentId = employeeResultSet.getString("department_id");
String position = employeeResultSet.getString("position");
Date hireDate = employeeResultSet.getDate("hire_date");
// 在页面上显示数据...
}
while (departmentResultSet.next()) {
String departmentId = departmentResultSet.getString("department_id");
String departmentName = departmentResultSet.getString("department_name");
String departmentHead = departmentResultSet.getString("department_head");
// 在页面上显示数据...
}
```
以上是使用JavaEE实现创建职工表和部门信息表的步骤,您可以根据自己的需要进行修改和调整。
阅读全文