使用Java实现数据库编程 1.创建数据库表apply_info,添加测试数据不少于4条, 2.创建实体类ApplyInfo,根据业务提供需要的构造方法和setter/getter方法 3.创建BaseDao类,实现数据库连接和关闭功能 4.创建DAO接口ApplyInfoDao,定义学生报名,按班级查询,按比赛项目,取消报名的方法 5.创建DAO实现类ApplyInfoDaoImpl,实现ApplyInfoDao接口,使用JDBC完成相应数据库操作 6.创建Service接口ApplyInfoService,定义学生报名,按班级查询,按比赛项目,取消报名的方法 7.创建Service实现类ApplyInfoServiceImpl,实现ApplyInfoService接口,调用Dao层方法实现数据操作 8.创建ApplyMgr类,完成在控制台的报名信息操作,启动和运行系统 9.全部功能测试正常
时间: 2023-10-08 08:12:12 浏览: 81
下面是使用Java实现数据库编程的详细步骤:
1. 创建数据库表apply_info,添加测试数据不少于4条
首先,需要在数据库中创建一个名为apply_info的表,并添加至少4条测试数据。可以使用以下SQL语句:
```
CREATE TABLE apply_info (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
gender VARCHAR(10) NOT NULL,
class VARCHAR(50) NOT NULL,
project VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO apply_info (name, gender, class, project) VALUES ('Tom', 'Male', 'Class 1', 'Running');
INSERT INTO apply_info (name, gender, class, project) VALUES ('Lucy', 'Female', 'Class 2', 'Swimming');
INSERT INTO apply_info (name, gender, class, project) VALUES ('John', 'Male', 'Class 3', 'Long jump');
INSERT INTO apply_info (name, gender, class, project) VALUES ('Jane', 'Female', 'Class 4', 'High jump');
```
2. 创建实体类ApplyInfo,根据业务提供需要的构造方法和setter/getter方法
创建一个名为ApplyInfo的Java类,该类对应数据库中的apply_info表。该类应该包含以下属性:
```
private int id;
private String name;
private String gender;
private String class;
private String project;
```
同时,应该提供构造方法和setter/getter方法。
```
public class ApplyInfo {
private int id;
private String name;
private String gender;
private String class;
private String project;
public ApplyInfo() {}
public ApplyInfo(String name, String gender, String class, String project) {
this.name = name;
this.gender = gender;
this.class = class;
this.project = project;
}
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 String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getClass() {
return class;
}
public void setClass(String class) {
this.class = class;
}
public String getProject() {
return project;
}
public void setProject(String project) {
this.project = project;
}
}
```
3. 创建BaseDao类,实现数据库连接和关闭功能
创建一个名为BaseDao的Java类,该类应该包含连接数据库和关闭数据库连接的方法。
```
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class BaseDao {
private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
private static final String USERNAME = "root";
private static final String PASSWORD = "root";
public Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
public void closeConnection(Connection conn) throws SQLException {
if (conn != null) {
conn.close();
}
}
}
```
4. 创建DAO接口ApplyInfoDao,定义学生报名,按班级查询,按比赛项目,取消报名的方法
创建一个名为ApplyInfoDao的Java接口,该接口定义了对数据库中apply_info表的各种操作方法,包括学生报名、按班级查询、按比赛项目查询和取消报名。
```
import java.sql.SQLException;
import java.util.List;
public interface ApplyInfoDao {
void add(ApplyInfo info) throws SQLException;
void delete(int id) throws SQLException;
List<ApplyInfo> getByClass(String className) throws SQLException;
List<ApplyInfo> getByProject(String projectName) throws SQLException;
}
```
5. 创建DAO实现类ApplyInfoDaoImpl,实现ApplyInfoDao接口,使用JDBC完成相应数据库操作
创建一个名为ApplyInfoDaoImpl的Java类,该类实现了ApplyInfoDao接口,并使用JDBC完成相应的数据库操作。
```
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ApplyInfoDaoImpl extends BaseDao implements ApplyInfoDao {
private static final String ADD_SQL = "INSERT INTO apply_info (name, gender, class, project) VALUES (?, ?, ?, ?)";
private static final String DELETE_SQL = "DELETE FROM apply_info WHERE id=?";
private static final String GET_BY_CLASS_SQL = "SELECT * FROM apply_info WHERE class=?";
private static final String GET_BY_PROJECT_SQL = "SELECT * FROM apply_info WHERE project=?";
@Override
public void add(ApplyInfo info) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = getConnection();
stmt = conn.prepareStatement(ADD_SQL);
stmt.setString(1, info.getName());
stmt.setString(2, info.getGender());
stmt.setString(3, info.getClass());
stmt.setString(4, info.getProject());
stmt.executeUpdate();
} finally {
if (stmt != null) {
stmt.close();
}
closeConnection(conn);
}
}
@Override
public void delete(int id) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = getConnection();
stmt = conn.prepareStatement(DELETE_SQL);
stmt.setInt(1, id);
stmt.executeUpdate();
} finally {
if (stmt != null) {
stmt.close();
}
closeConnection(conn);
}
}
@Override
public List<ApplyInfo> getByClass(String className) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
List<ApplyInfo> result = new ArrayList<>();
try {
conn = getConnection();
stmt = conn.prepareStatement(GET_BY_CLASS_SQL);
stmt.setString(1, className);
rs = stmt.executeQuery();
while (rs.next()) {
ApplyInfo info = new ApplyInfo();
info.setId(rs.getInt("id"));
info.setName(rs.getString("name"));
info.setGender(rs.getString("gender"));
info.setClass(rs.getString("class"));
info.setProject(rs.getString("project"));
result.add(info);
}
return result;
} finally {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
closeConnection(conn);
}
}
@Override
public List<ApplyInfo> getByProject(String projectName) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
List<ApplyInfo> result = new ArrayList<>();
try {
conn = getConnection();
stmt = conn.prepareStatement(GET_BY_PROJECT_SQL);
stmt.setString(1, projectName);
rs = stmt.executeQuery();
while (rs.next()) {
ApplyInfo info = new ApplyInfo();
info.setId(rs.getInt("id"));
info.setName(rs.getString("name"));
info.setGender(rs.getString("gender"));
info.setClass(rs.getString("class"));
info.setProject(rs.getString("project"));
result.add(info);
}
return result;
} finally {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
closeConnection(conn);
}
}
}
```
6. 创建Service接口ApplyInfoService,定义学生报名,按班级查询,按比赛项目,取消报名的方法
创建一个名为ApplyInfoService的Java接口,该接口定义了对学生报名信息的各种操作方法,包括学生报名、按班级查询、按比赛项目查询和取消报名。
```
import java.sql.SQLException;
import java.util.List;
public interface ApplyInfoService {
void add(ApplyInfo info) throws SQLException;
void delete(int id) throws SQLException;
List<ApplyInfo> getByClass(String className) throws SQLException;
List<ApplyInfo> getByProject(String projectName) throws SQLException;
}
```
7. 创建Service实现类ApplyInfoServiceImpl,实现ApplyInfoService接口,调用Dao层方法实现数据操作
创建一个名为ApplyInfoServiceImpl的Java类,该类实现了ApplyInfoService接口,并调用Dao层方法实现数据操作。
```
import java.sql.SQLException;
import java.util.List;
public class ApplyInfoServiceImpl implements ApplyInfoService {
private ApplyInfoDao dao = new ApplyInfoDaoImpl();
@Override
public void add(ApplyInfo info) throws SQLException {
dao.add(info);
}
@Override
public void delete(int id) throws SQLException {
dao.delete(id);
}
@Override
public List<ApplyInfo> getByClass(String className) throws SQLException {
return dao.getByClass(className);
}
@Override
public List<ApplyInfo> getByProject(String projectName) throws SQLException {
return dao.getByProject(projectName);
}
}
```
8. 创建ApplyMgr类,完成在控制台的报名信息操作,启动和运行系统
创建一个名为ApplyMgr的Java类,该类用于在控制台上完成学生报名信息的各种操作。
```
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
public class ApplyMgr {
private ApplyInfoService service = new ApplyInfoServiceImpl();
private Scanner scanner = new Scanner(System.in);
public void start() throws SQLException {
System.out.println("Welcome to apply information system!");
while (true) {
System.out.println("Please choose your operation:");
System.out.println("1. Add apply information");
System.out.println("2. Delete apply information");
System.out.println("3. Get apply information by class");
System.out.println("4. Get apply information by project");
System.out.println("5. Exit");
String choice = scanner.nextLine();
switch (choice) {
case "1":
addApplyInfo();
break;
case "2":
deleteApplyInfo();
break;
case "3":
getApplyInfoByClass();
break;
case "4":
getApplyInfoByProject();
break;
case "5":
System.out.println("Thanks for using apply information system!");
return;
default:
System.out.println("Invalid choice, please try again.");
}
}
}
private void addApplyInfo() throws SQLException {
System.out.println("Please input name:");
String name = scanner.nextLine();
System.out.println("Please input gender:");
String gender = scanner.nextLine();
System.out.println("Please input class:");
String clazz = scanner.nextLine();
System.out.println("Please input project:");
String project = scanner.nextLine();
ApplyInfo info = new ApplyInfo(name, gender, clazz, project);
service.add(info);
System.out.println("Add apply information successfully!");
}
private void deleteApplyInfo() throws SQLException {
System.out.println("Please input id:");
int id = Integer.parseInt(scanner.nextLine());
service.delete(id);
System.out.println("Delete apply information successfully!");
}
private void getApplyInfoByClass() throws SQLException {
System.out.println("Please input class:");
String clazz = scanner.nextLine();
List<ApplyInfo> result = service.getByClass(clazz);
if (result.isEmpty()) {
System.out.println("No apply information found.");
} else {
System.out.println("Apply information list:");
for (ApplyInfo info : result) {
System.out.println(info.getId() + "\t" + info.getName() + "\t" + info.getGender() + "\t" + info.getClass() + "\t" + info.getProject());
}
}
}
private void getApplyInfoByProject() throws SQLException {
System.out.println("Please input project:");
String project = scanner.nextLine();
List<ApplyInfo> result = service.getByProject(project);
if (result.isEmpty()) {
System.out.println("No apply information found.");
} else {
System.out.println("Apply information list:");
for (ApplyInfo info : result) {
System.out.println(info.getId() + "\t" + info.getName() + "\t" + info.getGender() + "\t" + info.getClass() + "\t" + info.getProject());
}
}
}
public static void main(String[] args) throws SQLException {
ApplyMgr mgr = new ApplyMgr();
mgr.start();
}
}
```
9. 全部功能测试正常
运行ApplyMgr类,测试所有功能是否正常。如果全部功能都能够正常使用,则说明Java数据库编程已经成功实现。
阅读全文