package com.sjsq.dao.impl;
import com.sjsq.dao.AdminDao;
import com.sjsq.utils.DBUtil;
import com.sjsq.entity.Admin;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
-
@author: shuijianshiqing
-
@date: 2022-03-01
-
@description: 登录系统实现
*/
public class AdminDaoImpl implements AdminDao {
/**
-
登录系统
-
@param admin
-
@return
*/
@Override
public Admin login(Admin admin) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 1.获取数据库连接
con = DBUtil.getConnection();
// 2.写sql
String sql = “select * from admin where username = ? and password = ?”;
// 3.预编译
ps = con.prepareStatement(sql);
// 4.设置值
ps.setObject(1, admin.getUsername());
ps.setObject(2, admin.getPassword());
rs = ps.executeQuery();
Admin adminLogin = null;
if (rs.next()) {
adminLogin = new Admin();
// 从数据库中获取值到实体类的setter方法中
adminLogin.setUsername(rs.getString(“username”));
adminLogin.setPassword(rs.getString(“password”));
// 返回的是你查询出来的完整的对象
return adminLogin;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源,避免出现异常
DBUtil.close(con, ps, rs);
}
return null;
}
}
ScoreDaoImpl
package com.sjsq.dao.impl;
import com.sjsq.dao.ScoreDao;
import com.sjsq.entity.Score;
import com.sjsq.utils.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
-
@author: shuijianshiqing
-
@date: 2022-03-02
-
@description:
*/
public class ScoreDaoImpl implements ScoreDao {
@Override
public List selectAll(String sql, Object[] arr) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 1.连接数据库
con = DBUtil.getConnection();
// 2.预编译
ps = con.prepareStatement(sql);
if (arr != null) {
for (int i = 0; i < arr.length; i++) {
// 传入sql的参数,向上转型,某个栏位的查询
ps.setObject(i + 1, arr[i]);
}
}
// 3.执行sql
rs = ps.executeQuery();
// 4.保存查询出来的数据到list
List list = new ArrayList<>();
while (rs.next()) {
Score score = new Score();
score.setId(rs.getInt(“id”));
score.setName(rs.getString(“name”));
score.setSubject(rs.getString(“subject”));
score.setScore(rs.getString(“score”));
list.add(score);
}
return list;
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭链接,避免数据库连接过多
DBUtil.close(con, ps, rs);
}
return null;
}
@Override
public Score selectScore(Integer id) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 1.连接数据库
con = DBUtil.getConnection();
// 2.预编译
String sql = “select * from score where id = ?”;
ps = con.prepareStatement(sql);
ps.setInt(1, id);
// 3.执行sql
rs = ps.executeQuery();
while (rs.next()) {
Score score = new Score();
score.setId(rs.getInt(“id”));
score.setName(rs.getString(“name”));
score.setSubject(rs.getString(“subject”));
score.setScore(rs.getString(“score”));
return score;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源,避免出现异常
DBUtil.close(con, ps, rs);
}
return null;
}
@Override
public boolean addScore(Score score) {
String sql = “insert into score values (?,?,?,?)”;
List list = new ArrayList();
list.add(score.getId());
list.add(score.getName());
list.add(score.getSubject());
list.add(score.getScore());
boolean flag = DBUtil.addUpdateDelete(sql, list.toArray());
if (flag) {
return true;
} else {
return false;
}
}
@Override
public boolean updateScore(Score score) {
String sql = “update score set name=?,subject=?,score=? where id=?”;
List list = new ArrayList();
list.add(score.getName());
list.add(score.getSubject());
list.add(score.getScore());
// 注意id在最后面
list.add(score.getId());
boolean flag = DBUtil.addUpdateDelete(sql, list.toArray());
if (flag) {
return true;
} else {
return false;
}
}
@Override
public boolean deleteScore(Integer id) {
String sql = “delete from score where id=?”;
List list = new ArrayList();
list.add(id);
boolean flag = DBUtil.addUpdateDelete(sql, list.toArray());
if (flag) {
return true;
} else {
return false;
}
}
}
score-add.jsp
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
<%-- 头部 --%>
<jsp:include page=“top.jsp”/>
新增成绩
返回上一级
添加
重置
<%-- 底部 --%>
<jsp:include page=“bottom.jsp”/>
score-add-do.jsp
<%@ page import=“com.sjsq.entity.Score” %>
<%@ page import=“com.sjsq.service.ScoreService” %>
<%@ page import=“com.sjsq.service.impl.ScoreServiceImpl” %>
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
<%
// 设置获取注册时的编码为UTF-8
request.setCharacterEncoding(“UTF-8”);
//获取teacher-add.jsp页面提交的账号和密码,注意传过来的是字符串需要进行转化为对应的类型
Integer id = Integer.parseInt(request.getParameter(“id”));
String name = request.getParameter(“name”);
String subject = request.getParameter(“subject”);
String score = request.getParameter(“score”);
// 将信息保存到实体类中
Score score1 = new Score();
score1.setId(id);
score1.setName(name);
score1.setSubject(subject);
score1.setScore(score);
System.out.println(“添加的成绩信息”);
System.out.println(score1);
// 将数据写入到数据库
ScoreService scoreService = new ScoreServiceImpl();
boolean flag = scoreService.addScore(score1);
if(flag){
response.sendRedirect(“main.jsp”);
}else{
response.sendRedirect(“error.jsp”);
}
%>
score-update.jsp
<%@ page import=“com.sjsq.entity.Score” %>
<%@ page import=“com.sjsq.service.ScoreService” %>
<%@ page import=“com.sjsq.service.impl.ScoreServiceImpl” %>
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
<%-- 头部 --%>
<jsp:include page=“top.jsp”/>
修改成绩
<%
//获取main.jsp页面的id
Integer id = Integer.parseInt(request.getParameter(“id”));
ScoreService ScoreService = new ScoreServiceImpl();
Score score = ScoreService.selectScore(id);
%>
返回上一级
<%-- 学号 --%>
修改
重置
<%-- 底部 --%>
<jsp:include page=“bottom.jsp”/>
score-update-do.jsp
<%@ page import=“com.sjsq.entity.Score” %>
<%@ page import=“com.sjsq.service.ScoreService” %>
<%@ page import=“com.sjsq.service.impl.ScoreServiceImpl” %>
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
<%
// 设置获取注册时的编码为UTF-8
request.setCharacterEncoding(“UTF-8”);
//获取teacher-update.jsp页面提交的账号和密码,注意传过来的是字符串需要进行转化为对应的类型
Integer id = Integer.parseInt(request.getParameter(“id”));
最后
javascript是前端必要掌握的真正算得上是编程语言的语言,学会灵活运用javascript,将对以后学习工作有非常大的帮助。掌握它最重要的首先是学习好基础知识,而后通过不断的实战来提升我们的编程技巧和逻辑思维。这一块学习是持续的,直到我们真正掌握它并且能够灵活运用它。如果最开始学习一两遍之后,发现暂时没有提升的空间,我们可以暂时放一放。继续下面的学习,javascript贯穿我们前端工作中,在之后的学习实现里也会遇到和锻炼到。真正学习起来并不难理解,关键是灵活运用。
%>
<jsp:include page=“bottom.jsp”/>
score-update-do.jsp
<%@ page import=“com.sjsq.entity.Score” %>
<%@ page import=“com.sjsq.service.ScoreService” %>
<%@ page import=“com.sjsq.service.impl.ScoreServiceImpl” %>
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
<%
// 设置获取注册时的编码为UTF-8
request.setCharacterEncoding(“UTF-8”);
//获取teacher-update.jsp页面提交的账号和密码,注意传过来的是字符串需要进行转化为对应的类型
Integer id = Integer.parseInt(request.getParameter(“id”));
最后
javascript是前端必要掌握的真正算得上是编程语言的语言,学会灵活运用javascript,将对以后学习工作有非常大的帮助。掌握它最重要的首先是学习好基础知识,而后通过不断的实战来提升我们的编程技巧和逻辑思维。这一块学习是持续的,直到我们真正掌握它并且能够灵活运用它。如果最开始学习一两遍之后,发现暂时没有提升的空间,我们可以暂时放一放。继续下面的学习,javascript贯穿我们前端工作中,在之后的学习实现里也会遇到和锻炼到。真正学习起来并不难理解,关键是灵活运用。
[外链图片转存中…(img-Ck2bCIF7-1719214686065)]
[外链图片转存中…(img-lfnhhHIS-1719214686067)]