package jdbc;
import model.Homework;
import model.Student;
import model.StudentHomework;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* @Author:txy
* @Date:created in 8:50 2020/3/8
*/
public class HomeworkJdbc {
//老师布置作业
public static boolean addHomework(Homework th) throws ClassNotFoundException {
String url="jdbc:mysql://127.0.0.1:3306/school?serverTimezone=UTC";
String drivername = "com.mysql.cj.jdbc.Driver";
Class.forName(drivername);//可以省略
boolean isSuccess = true;
try (Connection connection = DatabasePool.getHikariDataSource().getConnection()) {
//Preparestatement (增、删、改、查)
String sqlString = "insert into homework(id,title,content,create_time) values (?,?,?,?)";
PreparedStatement ps = connection.prepareStatement(sqlString);
ps.setLong(1,th.getId());
ps.setString(2,th.getTitle());
ps.setString(3,th.getContent());
ps.setTimestamp(4, th.getCreateTime());
//成功返回false 失败返回true
isSuccess = ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
//返回相反的结果
return !isSuccess;
}
//查询所有学生提交的作业
public static List<StudentHomework> selectAllStudentHomework() throws ClassNotFoundException {
String url="jdbc:mysql://127.0.0.1:3306/school?serverTimezone=UTC";
String drivername = "com.mysql.cj.jdbc.Driver";
String sqlString = "select * from student_homework ";
List<StudentHomework>list=new ArrayList<>();
Class.forName(drivername);//可以省略
try (Connection connection = DatabasePool.getHikariDataSource().getConnection()) {
//通过连接获取statement
try (Statement statement = connection.createStatement()) {
//statement (增、删、改、查)
try (ResultSet resultSet = statement.executeQuery(sqlString)) {
//获取执行结果
while (resultSet.next()) {
StudentHomework sh = new StudentHomework();
sh.setId(resultSet.getLong("id"));
sh.setStudentId(resultSet.getLong("student_id"));
sh.setHomeworkId(resultSet.getLong("homework_id"));
sh.setHomeworkTitle(resultSet.getString("homework_title"));
sh.setHomeworkContent(resultSet.getString("homework_content"));
sh.setCreateTime(resultSet.getTimestamp("create_time"));
sh.setUpdateTime(resultSet.getTimestamp("update_time"));
list.add(sh);
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//查询所有教师布置的作业
public static List<Homework> selectAllTeacherHomework() throws ClassNotFoundException {
String url="jdbc:mysql://127.0.0.1:3306/school?serverTimezone=UTC";
String drivername = "com.mysql.cj.jdbc.Driver";
String sqlString = "select * from homework ";
List<Homework>list=new ArrayList<>();
Class.forName(drivername);//可以省略
try (Connection connection = DatabasePool.getHikariDataSource().getConnection()) {
//通过连接获取statement
try (Statement statement = connection.createStatement()) {
//statement (增、删、改、查)
try (ResultSet resultSet = statement.executeQuery(sqlString)) {
//获取执行结果
while (resultSet.next()) {
Homework th = new Homework();
th.setId(resultSet.getLong("id"));
th.setTitle(resultSet.getString("title"));
th.setContent(resultSet.getString("content"));
th.setCreateTime(resultSet.getTimestamp("create_time"));
th.setUpdateTime(resultSet.getTimestamp("update_time"));
list.add(th);
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//查询某位学生提交的作业
public static List<StudentHomework> selectAStudentHomework(Long student_id) throws ClassNotFoundException {
String url="jdbc:mysql://127.0.0.1:3306/school?serverTimezone=UTC";
String drivername = "com.mysql.cj.jdbc.Driver";
String sqlString = "select * from student_homework where student_id="+student_id;
List<StudentHomework>list=new ArrayList<>();
Class.forName(drivername);//可以省略
try (Connection connection = DatabasePool.getHikariDataSource().getConnection()) {
//通过连接获取statement
try (Statement statement = connection.createStatement()) {
//statement (增、删、改、查)
try (ResultSet resultSet = statement.executeQuery(sqlString)) {
//获取执行结果
while (resultSet.next()) {
StudentHomework sh = new StudentHomework();
sh.setId(resultSet.getLong("id"));
sh.setStudentId(resultSet.getLong("student_id"));
sh.setHomeworkId(resultSet.getLong("homework_id"));
sh.setHomeworkTitle(resultSet.getString("homework_title"));
sh.setHomeworkContent(resultSet.getString("homework_content"));
sh.setCreateTime(resultSet.getTimestamp("create_time"));
sh.setUpdateTime(resultSet.getTimestamp("update_time"));
list.add(sh);
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//查询某次作业的提交情况
public static List<StudentHomework> selectAHomework(Long homework_id) throws ClassNotFoundException {
String url="jdbc:mysql://127.0.0.1:3306/school?serverTimezone=UTC";
String drivername = "com.mysql.cj.jdbc.Driver";
String sqlString = "select * from student_homework where homework_id="+homework_id;
List<StudentHomework>list=new ArrayList<>();
Class.forName(drivername);//可以省略
try (Connection connection = DatabasePool.getHikariDataSource().getConnection()) {
//通过连接获取statement
try (Statement statement = connection.createStatement()) {
//statement (增、删、改、查)
try (ResultSet resultSet = statement.executeQuery(sqlString)) {
//获取执行结果
while (resultSet.next()) {
StudentHomework sh = new StudentHomework();
sh.setId(resultSet.getLong("id"));
sh.setStudentId(resultSet.getLong("student_id"));
sh.setHomeworkId(resultSet.getLong("homework_id"));
sh.setHomeworkTitle(resultSet.getString("homework_title"));
sh.setHomeworkContent(resultSet.getString("homework_content"));
sh.setCreateTime(resultSet.getTimestamp("create_time"));
sh.setUpdateTime(resultSet.getTimestamp("update_time"));
list.add(sh);
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//学生提交作业
public static String handHomework(StudentHomework sh) throws ClassNotFoundException {
String ur
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于maven+jsp+servlet+mysql+java的作业管理系统源码.zip
资源推荐
资源详情
资源评论
收起资源包目录
基于maven+jsp+servlet+mysql+java的作业管理系统源码.zip (95个子文件)
code_20105
lib
javax.jms.jar 25KB
javax.annotation.jar 8KB
javax.transaction.jar 9KB
javax.servlet.jsp.jar 77KB
javax.persistence.jar 127KB
javax.servlet.jar 68KB
javax.ejb.jar 46KB
javax.servlet.jsp.jstl.jar 27KB
javax.resource.jar 43KB
pom.xml 793B
web
pom.xml 3KB
src
main
java
org
example
controller
servlet
Test_Servlet.java 728B
TeacherAddStudentServlet.java 2KB
TeacherAddHomeworkServlet.java 3KB
TeacherSearchAStudentHWServlet.java 1KB
TeacherSearchStudentServlet.java 954B
StudentAddHomeworkServlet.java 2KB
TeacherSearchShServlet.java 1022B
StudentHandInHomeworkServlet.java 646B
Test_Servlet_V3.java 932B
TeacherHomeworkServlet.java 989B
TeacherSearchAHWSubmitStatusServlet.java 1KB
Test_Servlet_V2.java 844B
webapp
WEB-INF
lib
mysql-connector-java-8.0.16.jar 2.19MB
web.xml 215B
index.jsp 765B
Tea_Add_Student.jsp 1KB
AStudentHW.jsp 1KB
Stu_HandIn_HW.jsp 813B
Tea_Add_HW.jsp 2KB
Tea_Query_HW.jsp 3KB
target
classes
org
example
controller
servlet
TeacherSearchAStudentHWServlet.class 2KB
Test_Servlet_V3.class 2KB
TeacherSearchShServlet.class 2KB
Test_Servlet.class 1KB
TeacherSearchStudentServlet.class 2KB
StudentHandInHomeworkServlet.class 1KB
StudentAddHomeworkServlet.class 3KB
TeacherHomeworkServlet.class 2KB
TeacherSearchAHWSubmitStatusServlet.class 2KB
Test_Servlet_V2.class 1KB
TeacherAddStudentServlet.class 3KB
TeacherAddHomeworkServlet.class 3KB
web
WEB-INF
classes
org
example
controller
servlet
TeacherSearchAStudentHWServlet.class 2KB
Test_Servlet_V3.class 2KB
TeacherSearchShServlet.class 2KB
Test_Servlet.class 1KB
TeacherSearchStudentServlet.class 2KB
StudentHandInHomeworkServlet.class 1KB
StudentAddHomeworkServlet.class 3KB
TeacherHomeworkServlet.class 2KB
TeacherSearchAHWSubmitStatusServlet.class 2KB
Test_Servlet_V2.class 1KB
TeacherAddStudentServlet.class 3KB
TeacherAddHomeworkServlet.class 3KB
lib
slf4j-api-1.7.25.jar 40KB
mysql-connector-java-8.0.16.jar 2.19MB
db-1.0-SNAPSHOT.jar 13KB
HikariCP-3.4.2.jar 149KB
web.xml 215B
index.jsp 765B
Tea_Add_Student.jsp 1KB
AStudentHW.jsp 1KB
Stu_HandIn_HW.jsp 813B
Tea_Add_HW.jsp 2KB
META-INF
MANIFEST.MF 96B
Tea_Query_HW.jsp 3KB
web.war 2.27MB
.idea
libraries
Java_EE_6_Java_EE_6.xml 775B
vcs.xml 172B
misc.xml 658B
compiler.xml 598B
.gitignore 39B
encodings.xml 195B
core
pom.xml 544B
Homework_Managment_System.iml 81B
db
pom.xml 1KB
src
main
java
model
Homework.java 1012B
StudentHomework.java 1KB
Student.java 828B
jdbc
Test_jdbc.java 1KB
StudentJdbc.java 3KB
HomeworkJdbc.java 10KB
Test_jdbc_v3.java 1KB
DatabasePool.java 1021B
Test_jdbc_v2.java 2KB
target
classes
model
Homework.class 1KB
StudentHomework.class 2KB
Student.class 1KB
jdbc
StudentJdbc.class 4KB
Test_jdbc_v3.class 2KB
Test_jdbc_v2.class 3KB
Test_jdbc.class 2KB
HomeworkJdbc.class 9KB
DatabasePool.class 1KB
共 95 条
- 1
资源评论
土豆片片
- 粉丝: 1843
- 资源: 5858
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功