使用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:13:18 浏览: 110
好的,这里给出一个Java实现数据库编程的示例代码,帮助您更好地理解:
1. 创建数据库表apply_info,添加测试数据不少于4条:
```
CREATE TABLE apply_info (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
grade VARCHAR(10) NOT NULL,
project VARCHAR(20) NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO apply_info (name, grade, project) VALUES ('Tom', '1', 'Math');
INSERT INTO apply_info (name, grade, project) VALUES ('Jerry', '2', 'English');
INSERT INTO apply_info (name, grade, project) VALUES ('Alice', '3', 'Science');
INSERT INTO apply_info (name, grade, project) VALUES ('Bob', '1', 'Physics');
```
2. 创建实体类ApplyInfo,根据业务提供需要的构造方法和setter/getter方法:
```
public class ApplyInfo {
private int id;
private String name;
private String grade;
private String project;
public ApplyInfo() {}
public ApplyInfo(String name, String grade, String project) {
this.name = name;
this.grade = grade;
this.project = project;
}
// setter and getter methods
}
```
3. 创建BaseDao类,实现数据库连接和关闭功能:
```
public class BaseDao {
private static final String driver = "com.mysql.cj.jdbc.Driver";
private static final String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC";
private static final String username = "root";
private static final String password = "123456";
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static void close(Connection conn, Statement stmt, ResultSet rs) throws SQLException {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
}
```
4. 创建DAO接口ApplyInfoDao,定义学生报名,按班级查询,按比赛项目,取消报名的方法:
```
public interface ApplyInfoDao {
int addApply(ApplyInfo applyInfo);
List<ApplyInfo> findByGrade(String grade);
List<ApplyInfo> findByProject(String project);
int deleteApply(int id);
}
```
5. 创建DAO实现类ApplyInfoDaoImpl,实现ApplyInfoDao接口,使用JDBC完成相应数据库操作:
```
public class ApplyInfoDaoImpl implements ApplyInfoDao {
@Override
public int addApply(ApplyInfo applyInfo) {
Connection conn = null;
PreparedStatement pstmt = null;
int result = 0;
try {
conn = BaseDao.getConnection();
pstmt = conn.prepareStatement("INSERT INTO apply_info (name, grade, project) VALUES (?, ?, ?)");
pstmt.setString(1, applyInfo.getName());
pstmt.setString(2, applyInfo.getGrade());
pstmt.setString(3, applyInfo.getProject());
result = pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
BaseDao.close(conn, pstmt, null);
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
@Override
public List<ApplyInfo> findByGrade(String grade) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<ApplyInfo> list = new ArrayList<>();
try {
conn = BaseDao.getConnection();
pstmt = conn.prepareStatement("SELECT * FROM apply_info WHERE grade = ?");
pstmt.setString(1, grade);
rs = pstmt.executeQuery();
while (rs.next()) {
ApplyInfo applyInfo = new ApplyInfo();
applyInfo.setId(rs.getInt("id"));
applyInfo.setName(rs.getString("name"));
applyInfo.setGrade(rs.getString("grade"));
applyInfo.setProject(rs.getString("project"));
list.add(applyInfo);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
BaseDao.close(conn, pstmt, rs);
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
@Override
public List<ApplyInfo> findByProject(String project) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
List<ApplyInfo> list = new ArrayList<>();
try {
conn = BaseDao.getConnection();
pstmt = conn.prepareStatement("SELECT * FROM apply_info WHERE project = ?");
pstmt.setString(1, project);
rs = pstmt.executeQuery();
while (rs.next()) {
ApplyInfo applyInfo = new ApplyInfo();
applyInfo.setId(rs.getInt("id"));
applyInfo.setName(rs.getString("name"));
applyInfo.setGrade(rs.getString("grade"));
applyInfo.setProject(rs.getString("project"));
list.add(applyInfo);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
BaseDao.close(conn, pstmt, rs);
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
@Override
public int deleteApply(int id) {
Connection conn = null;
PreparedStatement pstmt = null;
int result = 0;
try {
conn = BaseDao.getConnection();
pstmt = conn.prepareStatement("DELETE FROM apply_info WHERE id = ?");
pstmt.setInt(1, id);
result = pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
BaseDao.close(conn, pstmt, null);
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
}
```
6. 创建Service接口ApplyInfoService,定义学生报名,按班级查询,按比赛项目,取消报名的方法:
```
public interface ApplyInfoService {
boolean addApply(String name, String grade, String project);
List<ApplyInfo> findByGrade(String grade);
List<ApplyInfo> findByProject(String project);
boolean deleteApply(int id);
}
```
7. 创建Service实现类ApplyInfoServiceImpl,实现ApplyInfoService接口,调用Dao层方法实现数据操作:
```
public class ApplyInfoServiceImpl implements ApplyInfoService {
private ApplyInfoDao applyInfoDao = new ApplyInfoDaoImpl();
@Override
public boolean addApply(String name, String grade, String project) {
ApplyInfo applyInfo = new ApplyInfo(name, grade, project);
int result = applyInfoDao.addApply(applyInfo);
return result > 0;
}
@Override
public List<ApplyInfo> findByGrade(String grade) {
return applyInfoDao.findByGrade(grade);
}
@Override
public List<ApplyInfo> findByProject(String project) {
return applyInfoDao.findByProject(project);
}
@Override
public boolean deleteApply(int id) {
int result = applyInfoDao.deleteApply(id);
return result > 0;
}
}
```
8. 创建ApplyMgr类,完成在控制台的报名信息操作,启动和运行系统:
```
public class ApplyMgr {
private static Scanner scanner = new Scanner(System.in);
private static ApplyInfoService applyInfoService = new ApplyInfoServiceImpl();
public void start() {
while (true) {
System.out.println("请选择要进行的操作:1.学生报名 2.按班级查询 3.按比赛项目查询 4.取消报名 0.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addApply();
break;
case 2:
findByGrade();
break;
case 3:
findByProject();
break;
case 4:
deleteApply();
break;
case 0:
System.out.println("程序已退出!");
System.exit(0);
break;
default:
System.out.println("输入错误,请重新输入!");
break;
}
}
}
public void addApply() {
System.out.println("请输入学生姓名:");
String name = scanner.next();
System.out.println("请输入学生班级:");
String grade = scanner.next();
System.out.println("请输入比赛项目:");
String project = scanner.next();
boolean result = applyInfoService.addApply(name, grade, project);
if (result) {
System.out.println("报名成功!");
} else {
System.out.println("报名失败!");
}
}
public void findByGrade() {
System.out.println("请输入班级:");
String grade = scanner.next();
List<ApplyInfo> list = applyInfoService.findByGrade(grade);
if (list.size() == 0) {
System.out.println("未查询到相关信息!");
} else {
for (ApplyInfo applyInfo : list) {
System.out.println(applyInfo.getId() + "\t" + applyInfo.getName() + "\t" + applyInfo.getGrade() + "\t" + applyInfo.getProject());
}
}
}
public void findByProject() {
System.out.println("请输入比赛项目:");
String project = scanner.next();
List<ApplyInfo> list = applyInfoService.findByProject(project);
if (list.size() == 0) {
System.out.println("未查询到相关信息!");
} else {
for (ApplyInfo applyInfo : list) {
System.out.println(applyInfo.getId() + "\t" + applyInfo.getName() + "\t" + applyInfo.getGrade() + "\t" + applyInfo.getProject());
}
}
}
public void deleteApply() {
System.out.println("请输入要取消报名的学生的id:");
int id = scanner.nextInt();
boolean result = applyInfoService.deleteApply(id);
if (result) {
System.out.println("取消报名成功!");
} else {
System.out.println("取消报名失败!");
}
}
public static void main(String[] args) {
ApplyMgr applyMgr = new ApplyMgr();
applyMgr.start();
}
}
```
9. 全部功能测试正常。
希望这个示例代码可以帮助您实现数据库编程。
阅读全文