上机考试的成果,
考试过程惊心动魄,
本以为胜券在握,
没想到出了差错,
还好最后修成正果。
呀!还压上韵了,OK,不搞笑了,谈正事......在对eclipse不熟悉、对javaweb不是很感兴趣的情况之下,不得不应对上机考试,还好最后交上了试卷,有考试时间限制,课下不想再深究,主要是不感兴趣和懒,嗯~~~所以内容不完全,请包涵。简单介绍一下内容吧,主要是通过eclipse对数据库进行操作,涉及到JDBC MySQL,连接数据库,servlet框架, jsp网页+渲染... ...
求求啦!!!关注、收藏+点赞,生活乐趣多一半!!!
文章下面有投票哦!希望大家表达一下看法,我也想从中得到一些慰藉和迷茫的指引,蟹蟹~
目录
学生积分管理
-
预设计功能简介:学生积分数据查询,数据删除,数据添加
-
数据库初始数据截图:
-
项目结构的软件截图:
-
项目结构:
-
实现页面及运行截图:
-
代码粘贴:
-
Index.jsp:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>简单的增删查</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
}
.center-container {
text-align: center;
margin-top: 50px;
}
h1 {
color: #333;
text-transform: uppercase;
margin-bottom: 30px;
}
.action-link {
display: inline-block;
margin: 0 10px;
padding: 10px 20px;
background-color: #007BFF;
color: #fff;
text-decoration: none;
border-radius: 5px;
transition: background-color 0.3s;
}
.action-link:hover {
background-color: #0056b3;
}
/*
.modify-link {
display: inline-block;
/* 其他样式可以与 .action-link 相同或不同 */
}
*/
</style>
</head>
<body>
<div class="center-container">
<h1>学生积分的增删查</h1>
<a href="insert.jsp" class="action-link">添加学生积分</a>
<a href="servlet?method=list" class="action-link">删除学生信息</a>
<!--
<a href="servlet?method=modify" class="action-link modify-link">修改</a>
-->
<a href="servlet?method=list" class="action-link">查询学生积分</a>
</div>
</body>
</html>
-
Insert.jsp:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加学生积分</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
}
h1 {
text-align: center;
color: #333;
}
.center-container {
text-align: center;
margin-top: 20px;
}
form {
margin-top: 20px;
}
form div {
margin-bottom: 10px;
}
form label {
display: inline-block;
width: 80px;
}
input[type="text"],
input[type="radio"] {
margin-right: 10px;
}
button {
padding: 10px 20px;
background-color: #007BFF;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
a {
text-decoration: none;
color: #007BFF;
}
a:hover {
color: #0056b3;
}
</style>
</head>
<body>
<%
Object message = request.getAttribute("message");
if (message != null && !"".equals(message)) {
%>
<script type="text/javascript">
alert("<%=request.getAttribute("message")%>"); //弹出对话框
</script>
<%
}
%>
<div class="center-container">
<h1>添加学生积分信息</h1>
<a href="index.jsp">返回主页</a>
<form action="servlet?method=insert" method="post">
<div>
学生ID<input type="text" id="id" name="id" />
</div>
<div>
学生姓名<input type="text" id="name" name="name" />
</div>
<div>
学生积分<input type="text" id="score" name="score" />
</div>
<div>
性别
<input type="radio" id="gender_male" name="gender" value="男" />男
<input type="radio" id="gender_female" name="gender" value="女" />女
</div>
<div>
<button type="submit">添 加</button>
</div>
</form>
</div>
</form>
</div>
</body>
</html>
-
List.jsp:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加学生积分</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
}
h1 {
text-align: center;
color: #333;
}
.center-container {
text-align: center;
margin-top: 20px;
}
form {
margin-top: 20px;
}
form div {
margin-bottom: 10px;
}
form label {
display: inline-block;
width: 80px;
}
input[type="text"],
input[type="radio"] {
margin-right: 10px;
}
button {
padding: 10px 20px;
background-color: #007BFF;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
a {
text-decoration: none;
color: #007BFF;
}
a:hover {
color: #0056b3;
}
</style>
</head>
<body>
<%
Object message = request.getAttribute("message");
if (message != null && !"".equals(message)) {
%>
<script type="text/javascript">
alert("<%=request.getAttribute("message")%>"); //弹出对话框
</script>
<%
}
%>
<div class="center-container">
<h1>添加学生积分信息</h1>
<a href="index.jsp">返回主页</a>
<form action="servlet?method=insert" method="post">
<div>
学生ID<input type="text" id="id" name="id" />
</div>
<div>
学生姓名<input type="text" id="name" name="name" />
</div>
<div>
学生积分<input type="text" id="score" name="score" />
</div>
<div>
性别
<input type="radio" id="gender_male" name="gender" value="男" />男
<input type="radio" id="gender_female" name="gender" value="女" />女
</div>
<div>
<button type="submit">添 加</button>
</div>
</form>
</div>
</form>
</div>
</body>
</html>
-
DButil:
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
private static String url = "jdbc:mysql://localhost:3306/stu_score?useUnicode=true&characterEncoding=utf8";
private static String user = "root";
private static String password = "123456";
private static String jdbcName="com.mysql.jdbc.Driver";
private Connection con=null;
public static Connection getConnection() {
Connection con=null;
try {
Class.forName(jdbcName);
con=DriverManager.getConnection(url, user, password);
System.out.println("数据库连接成功");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("数据库连接失败");
e.printStackTrace();
}
return con;
}
public static void close(Connection con) {
if(con!=null)
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(Statement state, Connection conn) {
if(state!=null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet rs, Statement state, Connection conn) {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(state!=null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
-
Dao:
package Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
//import org.junit.jupiter.api.Test;
import bean.Bean;
import util.DBUtil;
public class Dao {//dao层
private DBUtil dbutil=new DBUtil();
public Dao() {
// TODO Auto-generated constructor stub
}
// @Test
public boolean insert(Bean bean) {//插入数据的方法
boolean f=false;
String sql="insert into info(id,name,score,sex) values('"+bean.getId()+"','"+bean.getName()+"','"+bean.getScore()+"','"+bean.getSex()+"')";
Connection conn=DBUtil.getConnection();//数据库连接,加载驱动
Statement state=null;
try
{
state=conn.createStatement();//实例化Statement对象,方便对sql语句进行操作
System.out.println(conn);
state.executeUpdate(sql);
f=true;
//执行数据库更新操作用于执行INSERT、UPDATE或DELETE语句以及SQLDDL(数据定义语言)语句,
//例如CREATETABLE和DROPTABLE,(创建表和删除表)
}catch(Exception e)//当try语句中s出现异常时,会执行catch中的语句
{
e.printStackTrace();//捕获异常的语句
}
finally //finally作为异常处理的一部分,它只能用在try/catch语句中,并且附带一个语句块,表示这段语句最终一定会被执行(不管有没有抛出异常),经常被用在需要释放资源的情况下。
{
DBUtil.close(conn);
}
return f;
}
public boolean delete(int id ) {//删除方法
String sql="delete from info where id='"+id+"'";
boolean f=false;
Connection conn =DBUtil.getConnection();
Statement st=null;
try {
st=conn.createStatement();
st.executeUpdate(sql);
f=true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
DBUtil.close(st, conn);
}
return f;
}
public boolean update(Bean bean) {//更新方法
String sql="update info set name='"+bean.getName()+"',score='"+bean.getScore()+"',sex='"+bean.getSex()+"'where id='"+bean.getId()+"'";
Connection conn=DBUtil.getConnection();
boolean f=false;
Statement st=null;
try {
st=conn.createStatement();
st.executeUpdate(sql);
f=true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return f;
}
public List<Bean> list(){//查询所有方法
String sql="select * from info order by id ASC";
Connection conn=DBUtil.getConnection();
Statement st=null;
List<Bean> list=new ArrayList<>();
ResultSet rs=null;
Bean bean=null;
try {
st=conn.createStatement();
st.executeQuery(sql);
rs=st.executeQuery(sql);
while(rs.next()) {
int id=rs.getInt("id");
String name = rs.getString("name");
String score = rs.getString("score");
String sex = rs.getString("sex");
bean=new Bean(id,name,score,sex);
list.add(bean);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
DBUtil.close(rs, st, conn);
}
return list;
}
}
-
Bean:
package bean;
public class Bean {
private int id;
private String name;
private String score;
private String sex;
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 getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Bean [id=" + id + ", name=" + name + ", score=" + score + ", sex=" + sex + "]";
}
public Bean(int id, String name, String score, String sex) {
super();
this.id = id;
this.name = name;
this.score = score;
this.sex = sex;
}
public Bean() {
// TODO Auto-generated constructor stub
}
}
-
Servelt:
package app;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Dao.Dao;
import bean.Bean;
/**
* Servlet implementation class servlet
*/
@WebServlet("/servlet")
public class servlet extends HttpServlet {
Dao dao=new Dao();
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public servlet() {
super();
// TODO Auto-generated constructor stub
}
private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String score = request.getParameter("score");
String sex = request.getParameter("sex");
Bean bean=new Bean(id,name,score,sex);
dao.update(bean);
request.setAttribute("message", "修改成功");
request.getRequestDispatcher("servlet?method=list").forward(request, response);
}
private void list(HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
List<Bean> list = dao.list();
request.setAttribute("list", list);
request.getRequestDispatcher("list.jsp").forward(request,response);
}
private void delete(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
int id=Integer.parseInt(request.getParameter("id"));
dao.delete(id); //进行数据库的删除操作
request.setAttribute("message", "删除成功");
request.getRequestDispatcher("servlet?method=list").forward(request, response);
}
private void insert(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String score = request.getParameter("score");
String sex = request.getParameter("sex");
Bean bean=new Bean(id,name,score,sex);
if(dao.insert(bean)) {
request.setAttribute("message", "添加成功");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
String method=request.getParameter("method");
if("insert".equals(method)) {
insert(request,response);
}
else if("delete".equals(method)) {
try {
delete(request,response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if("update".equals(method)) {
update(request,response);
}
else if("list".equals(method)) {
try {
list(request,response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
以上主要是完整代码,关于代码相关详细知识:如数据库连接dbutil包,servlet框架介绍、认识JavaBean(JDBC),导包,jsp文件等... ... 请见我以下文章:
JavaWeb笔记(javaweb工程的创建、运行环境与架构、Tomcat、JDBC、数据库连接)-CSDN博客
文章下面有投票哦!希望大家表达一下看法,我也想从中得到一些慰藉和迷茫的指引,蟹蟹~