javaweb课程信息管理系统

Wesley13
• 阅读 834

eclipse+tomcat+MySQL+SQLyog

1、在SQLyog中新建sqltestdb数据库,其中新建all数据表。包含四个字段:id、course、teacher、area,将id设为自动递增,否则后面递增会出错。

 javaweb课程信息管理系统

2、

新建web项目,点击两次next,勾选自动生成web.xml。

javaweb课程信息管理系统

3、

将连接mysql的驱动jar包(mysql-connector-java-8.0.13-bin.jar)复制到WEB-INF下的lib目录下,直接拖拽即可。

javaweb课程信息管理系统

4.新建3个文档包,在每个包下建对应的Java类。

Stu.java

javaweb课程信息管理系统

package example.bean.stu;  
public class Stu { 
 
  private int id; 
  
  private String name; 
  
  private String teacher; 
  
  private String workplace;
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 getTeacher() {
    return teacher;
}
public void setTeacher(String teacher) {
    this.teacher = teacher;
}
public String getWorkplace() {
    return workplace;
}
public void setWorkplace(String workplace) {
    this.workplace = workplace;
}

}

javaweb课程信息管理系统

BookJdbcDao.java

javaweb课程信息管理系统

package example.dao.stu; 
  
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
  
import example.bean.stu.Stu; 
  
public class StuJdbcDao { 
    
  private PreparedStatement ptmt = null; 
  private ResultSet rs = null; 
  
  public StuJdbcDao() { 
  } 
    
  public void findAll(Connection conn) throws SQLException 
  { 
    //to do    
  } 
    
  public void delete(Connection conn, int id) throws SQLException 
  { 
    String sql = "delete from tb_books where id=?"; 
    try{ 
      ptmt = conn.prepareStatement(sql); 
      // 对SQL语句中的第一个占位符赋值 
      ptmt.setInt(1, id); 
      // 执行更新操作 
      ptmt.executeUpdate(); 
        
    }finally{ 
      if (null!=ptmt) { 
        ptmt.close(); 
      } 
        
      if (null!=conn) { 
        conn.close(); 
      } 
        
    } 
      
  } 
    
  public void update(Connection conn, int id ) throws SQLException 
  { 
    //to do 
      
  } 
}

javaweb课程信息管理系统

ConnectionFactory.java

javaweb课程信息管理系统

package example.dao.stu; 
  
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
  
public class ConnectionFactory { 
    
  private String driverClassName = "com.mysql.cj.jdbc.Driver"; 
  private String url = "jdbc:mysql://localhost:3306/db_book?&useSSL=false&serverTimezone=UTC"; 
  private String userName = "root"; 
  private String password = "password"; 
    
  private static ConnectionFactory connectionFactory=null; 
  
  private ConnectionFactory() { 
      
    try { 
      Class.forName(driverClassName); 
    } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
    } 
  } 
    
  public Connection getConnection() throws SQLException 
  { 
    return DriverManager.getConnection(url, userName, password); 
      
  } 
    
  public static ConnectionFactory getInstance() 
  {   
    if (null==connectionFactory) { 
      connectionFactory=new ConnectionFactory(); 
    } 
    return connectionFactory; 
      
  } 
}

javaweb课程信息管理系统

DeleteServlet.java

javaweb课程信息管理系统

package example.servlet.stu; 
import java.io.IOException; 
import java.sql.Connection; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import example.dao.stu.StuJdbcDao; 
import example.dao.stu.ConnectionFactory; 
  
/** 
 * Servlet implementation class DeleteServlet 
 */
public class DeleteServlet extends HttpServlet { 
  private static final long serialVersionUID = 1L; 
  
  /** 
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 
   *   response) 
   */
  protected void doGet(HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, IOException { 
    int id = Integer.valueOf(request.getParameter("id")); 
    try { 
//     
      StuJdbcDao stuDao=new StuJdbcDao(); 
      Connection conn=ConnectionFactory.getInstance().getConnection(); 
      stuDao.delete(conn,id); 
        
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
    // 重定向到FindServlet 
    response.sendRedirect("FindServlet"); 
  } 
  
  /** 
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 
   *   response) 
   */
  protected void doPost(HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, IOException { 
    doGet(request, response); 
  } 
  
}

javaweb课程信息管理系统

FindServlet.java

javaweb课程信息管理系统

package example.servlet.stu;  
import java.io.IOException; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.Statement; 
import java.util.ArrayList; 
import java.util.List; 
  
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
  
import example.bean.stu.Stu;//导入包下的Stu类
  
/** 
 * Servlet implementation class FindServlet 
 */
public class FindServlet extends HttpServlet { 
  private static final long serialVersionUID = 1L; 
  
  /** 
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 
   *   response) 
   */
  protected void doGet(HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, IOException { 
    try { 
      // 加载数据库驱动,注册到驱动管理器 
      Class.forName("com.mysql.cj.jdbc.Driver"); 
      // 数据库连接字符串 
      String url = "jdbc:mysql://localhost:3306/db_book?&useSSL=false&serverTimezone=UTC"; 
      // 数据库用户名 
      String username = "root"; 
      // 数据库密码 
      String password = "root"; 
      // 创建Connection连接 
      Connection conn = DriverManager.getConnection(url, username, 
          password); 
      // 添加图书信息的SQL语句 
      String sql = "select * from tb_books"; 
      // 获取Statement 
      Statement statement = conn.createStatement(); 
  
      ResultSet resultSet = statement.executeQuery(sql); 
  
      List<Stu> list = new ArrayList<Stu>(); 
      while (resultSet.next()) { 
  
        Stu stu = new Stu(); 
        stu.setId(resultSet.getInt("id")); 
        stu.setName(resultSet.getString("name")); 
        stu.setTeacher(resultSet.getString("teacher")); 
        stu.setWorkplace(resultSet.getString("workplace")); 
        list.add(stu); 
      } 
      request.setAttribute("list", list); 
      resultSet.close(); 
      statement.close(); 
      conn.close(); 
  
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
  
    request.getRequestDispatcher("stu_list.jsp") 
        .forward(request, response); 
  
  } 
  
  /** 
   * @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); 
  } 
  
}

javaweb课程信息管理系统

UpdateServlet.java

javaweb课程信息管理系统

package example.servlet.stu; 
  
import java.io.IOException; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
  
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
  
/** 
 * Servlet implementation class UpdateServlet 
 */
public class UpdateServlet extends HttpServlet { 
  private static final long serialVersionUID = 1L; 
  
  /** 
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 
   *   response) 
   */
  protected void doGet(HttpServletRequest request, 
      HttpServletResponse response) throws ServletException, IOException { 
      request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
    int id = Integer.valueOf(request.getParameter("id"));
    String name = request.getParameter("name"); 
    String teacher = request.getParameter("teacher"); 
    String workplace = request.getParameter("workplace"); 
    try { 
      // 加载数据库驱动,注册到驱动管理器 
      Class.forName("com.mysql.cj.jdbc.Driver"); 
      // 数据库连接字符串 
      String url = "jdbc:mysql://localhost:3306/db_book?&useSSL=false&serverTimezone=UTC"; 
      // 数据库用户名 
      String username = "root"; 
      // 数据库密码 
      String password = "root"; 
      // 创建Connection连接 
      Connection conn = DriverManager.getConnection(url, username, 
          password); 
      // 更新SQL语句 
      String sql = "UPDATE tb_books SET name=?,teacher=?,workplace=? WHERE id=?"; 
      // 获取PreparedStatement 
      PreparedStatement ps = conn.prepareStatement(sql); 
      // 对SQL语句中的第一个参数赋值 
      ps.setString(1, name); 
      ps.setString(2, teacher); 
      ps.setString(3, workplace);
      ps.setInt(4, id);
      // 对SQL语句中的第二个参数赋值 
     
      // 执行更新操作 
      ps.executeUpdate(); 
      // 关闭PreparedStatement 
      ps.close(); 
      // 关闭Connection 
      conn.close(); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
    // 重定向到FindServlet 
    response.sendRedirect("FindServlet"); 
  
  } 
  
  /** 
   * @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); 
  } 
  
}

javaweb课程信息管理系统

5.右击Webcontent新建5个jsp页面。

addbook.jsp

javaweb课程信息管理系统

<%@ page language="java" contentType="text/html; charset=utf-8"
  pageEncoding="utf-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  
<%@page import="java.sql.Connection"%> 
<%@page import="java.sql.DriverManager"%> 
<%@page import="java.sql.PreparedStatement"%> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>添加结果</title> 
</head> 
<body> 
  <% 
    request.setCharacterEncoding("utf-8"); 
  %> 
  <jsp:useBean id="stu" class="example.bean.stu.Stu"></jsp:useBean> 
  <jsp:setProperty property="*" name="stu" /> 
  <% 
    try { 
      // 加载数据库驱动,注册到驱动管理器 
      Class.forName("com.mysql.cj.jdbc.Driver"); 
      // 数据库连接字符串 
      String url = "jdbc:mysql://localhost:3306/db_book?&useSSL=false&serverTimezone=UTC"; 
      // 数据库用户名 
      String username = "root"; 
      // 数据库密码 
      String password = "root"; 
      // 创建Connection连接 
      Connection conn = DriverManager.getConnection(url, username, 
          password); 
      // 添加图书信息的SQL语句 
      String sql = "insert into tb_books(name,teacher,workplace) values(?,?,?)"; 
      // 获取PreparedStatement 
      PreparedStatement ps = conn.prepareStatement(sql); 
      // 对SQL语句中的第1个参数赋值 
      ps.setString(1, stu.getName()); 
      // 对SQL语句中的第2个参数赋值 
      ps.setString(2, stu.getTeacher()); 
      ps.setString(3, stu.getWorkplace()); 
      // 对SQL语句中的第3个参数赋值 
      // 对SQL语句中的第4个参数赋值 
      // 执行更新操作,返回所影响的行数 
      int row = ps.executeUpdate(); 
      // 判断是否更新成功 
      if (row > 0) { 
        // 更新成输出信息 
        out.print("成功添加了 " + row + "条数据!"); 
      } 
      // 关闭PreparedStatement,释放资源 
      ps.close(); 
      // 关闭Connection,释放资源 
      conn.close(); 
    } catch (Exception e) { 
      out.print("课程信息添加失败!"); 
      e.printStackTrace(); 
    } 
  %> 
  <br> 
  <a href="main.jsp">返回</a> 
</body> 
</html>

javaweb课程信息管理系统

book_list.jsp

javaweb课程信息管理系统

<!--<%@page import="sun.awt.SunHints.Value"%>-->
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="java.util.List"%>
<%@page import="example.bean.stu.Stu"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>所有课程信息</title>
<style type="text/css">
td {
    font-size: 12px;
}

h2 {
    margin: 0px
}
</style>
</head>
<body>
    <table align="center" width="450" border="1" height="180"
        bordercolor="white" bgcolor="black" cellpadding="1" cellspacing="1">
        <tr bgcolor="white">
            <td align="center" colspan="7">
                <h2>所有课程信息</h2>
            </td>
        </tr>
        <tr align="center" bgcolor="#e1ffc1">
            <td><b>ID</b></td>
            <td><b>课程名称</b></td>
            <td><b>老师</b></td>
            <td><b>上课地点</b></td>
            <td><b>删除</b></td>
        </tr>
        <%
            // 获取图书信息集合 
            List<Book> list = (List<Book>) request.getAttribute("list");
            // 判断集合是否有效 
            if (list == null || list.size() < 1) {
                out.print("没有数据!");
            } else {
                // 遍历图书集合中的数据 
                for (Book book : list) {
        %>
        <tr align="center" bgcolor="white">
            <td><%=book.getId()%></td>
            <td><%=book.getName()%></td>
            <td><%=book.getTeacher()%></td>
            <td><%=book.getWorkplace()%></td>
            <td><a href="DeleteServlet?id=<%=book.getId()%>">删除</a></td>


        </tr>
        <%
            }
            }
        %>
    </table>
    <h2 align="center">
        <a href="main.jsp">返回主菜单</a>
    </h2>

</body>
</html>

javaweb课程信息管理系统

Update.jsp

javaweb课程信息管理系统

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>课程信息修改</title>
</head>
<script type="text/javascript">
function check(form) {
        with (form) {
            if (name.value == "") { 
                alert("课程名称不能为空"); 
                return false; 
              }   
              if (teacher.value == "") { 
                alert("老师名字不能为空"); 
                return false; 
              } 
              if (workplace.value == "") { 
                alert("上课地点不能为空"); 
                return false; 
              } 
        }
    }
</script>
<body>
<td>
                <form style="align: center;  line-height: 1.5 !important;">
                    action="UpdateServlet" method="post" onsubmit=" return check(this);">
                    <input type="text" name="id" size=3> 
                    <input type="text" name="name" size="3">
                    <input type="text" name="teacher" size="3">
                    <input type="text" name="workplace" size="3">
                     <input type="submit" value="修改">
                </form>
            </td>
            <h2 align="center">
        <a href="main.jsp">返回主菜单</a>
    </h2>
</body>
</html>

javaweb课程信息管理系统

index.jsp

javaweb课程信息管理系统

<%@page import="java.sql.SQLException"%> 
<%@page import="java.sql.DriverManager"%> 
<%@page import="java.sql.Connection"%> 
<%@ page language="java" contentType="text/html; charset=utf-8"
  pageEncoding="utf-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>添加课程信息</title> 
  
<script type="text/javascript"> 
  function check(form) { 
    with (form) { 
      if (name.value == "") { 
        alert("课程名称不能为空"); 
        return false; 
      } 
  
      if (teacher.value == "") { 
        alert("老师名字不能为空"); 
        return false; 
      } 
  
      if (workplace.value == "") { 
        alert("上课地点不能为空"); 
        return false; 
      } 
  
    } 
  } 
</script> 
  
</head> 
  
  
<body> 
  
<form action="addbook.jsp" method="post" onsubmit="check(this)"> 
  <table align="center" width="450"> 
    <tr> 
      <td align="center" colspan="2"> 
        <h2>添加课程信息</h2> 
        <hr> 
      </td> 
    </tr> 
  
    <tr> 
      <td align="right">课程名称:</td> 
      <td><input type="text" name="name"></td> 
    </tr> 
  
    <tr> 
      <td align="right">老师:</td> 
      <td><input type="text" name="teacher"></td> 
    </tr> 
 
    <tr> 
      <td align="right">上课地点:</td> 
      <td><input type="text" name="workplace" /></td> 
    </tr> 
    <tr> 
      <td align="center" colspan="2"><input type="submit" value="添 加"> 
      </td> 
    </tr> 
  </table> 
</form> 
  
<h2 align="center">
        <a href="main.jsp">返回主菜单</a>
    </h2>
  
</body> 
</html>

javaweb课程信息管理系统

main.jsp

javaweb课程信息管理系统

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>课程信息管理系统</title>
</head>
<body>
<center><h1>主菜单</h1><center>
<table><center>
<td><A href="index.jsp"><font size=2>课程信息录入</font></A></td>
<td><A href="Update.jsp"><font size=2>课程信息修改</font></A></td>
<h2 align="center"> 
  <a href="FindServlet">查询课程信息</a> 
</h2> 
<h2 align="center"> 
  <a href="FindServlet">删除课程信息</a> 
</h2> 
</table></center>
</body>
</html>

javaweb课程信息管理系统

接下来配置web.xml

javaweb课程信息管理系统

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
 <display-name>JdbcConnection</display-name> 
 <welcome-file-list> 
  <welcome-file>index.html</welcome-file> 
  <welcome-file>index.htm</welcome-file> 
  <welcome-file>index.jsp</welcome-file> 
  <welcome-file>default.html</welcome-file> 
  <welcome-file>default.htm</welcome-file> 
  <welcome-file>default.jsp</welcome-file> 
 </welcome-file-list> 
 <servlet> 
  <description></description> 
  <display-name>FindServlet</display-name> 
  <servlet-name>FindServlet</servlet-name> 
  <servlet-class>example.servlet.stu.FindServlet</servlet-class> 
 </servlet> 
 <servlet-mapping> 
  <servlet-name>FindServlet</servlet-name> 
  <url-pattern>/FindServlet</url-pattern> 
 </servlet-mapping> 
 <servlet> 
  <description></description> 
  <display-name>UpdateServlet</display-name> 
  <servlet-name>UpdateServlet</servlet-name> 
  <servlet-class>example.servlet.stu.UpdateServlet</servlet-class> 
 </servlet> 
 <servlet-mapping> 
  <servlet-name>UpdateServlet</servlet-name> 
  <url-pattern>/UpdateServlet</url-pattern> 
 </servlet-mapping> 
 <servlet> 
  <description></description> 
  <display-name>DeleteServlet</display-name> 
  <servlet-name>DeleteServlet</servlet-name> 
  <servlet-class>example.servlet.stu.DeleteServlet</servlet-class> 
 </servlet> 
 <servlet-mapping> 
  <servlet-name>DeleteServlet</servlet-name> 
  <url-pattern>/DeleteServlet</url-pattern> 
 </servlet-mapping> 
</web-app>
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
为什么mysql不推荐使用雪花ID作为主键
作者:毛辰飞背景在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究
Python进阶者 Python进阶者
11个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这