每日博客

“课程添加”课堂测试全部完成。

功能:

 

 

 代码如下:

package Dao;
import java.sql.Connection;
import java.sql.Statement;
import DBUtil.DBUtil;
import Entity.ClassAdd;
public class Dao 
{
    public boolean add(ClassAdd cd) 
    {
        String sql = "insert into user(classname,teacher,place,placenum) values('"+ cd.getClassname() + "','"+ cd.getteacher() +"','"+ cd.getplace() +"','"+cd.getPlacenum() +"')";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        boolean f = false;
        int a = 0;
        try 
        {
            state = conn.createStatement();
            a=state.executeUpdate(sql);    
        } 
        catch (Exception e) {e.printStackTrace();} 
        finally {DBUtil.close(state, conn);}
        if (a>0) {f = true;}
        return f;
      }
}
package DBUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil
{
    public static String db_url = "jdbc:mysql://localhost:3306/users?useSSL=false&useUnicode=true&characterEncoding=utf-8";//登录数据库
    public static String db_user = "root";
    public static String db_pass = "fx30001225";
    public static void main(String[] args)throws SQLException 
    {
        Connection conn = getConn();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql ="select * from user";
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        if(rs.next()){System.out.println("");}
        else{System.out.println("不空");}
    }
    public static Connection getConn () 
    {
        Connection conn = null;
        try 
        {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(db_url, db_user, db_pass);
        } 
        catch (Exception e) {e.printStackTrace();}
        return conn;
    }
    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();}}
    }
}
package Entity;
public class ClassAdd 
{
    private String classname;
    private String teacher;
    private String place;
    private String placenum;
    public String getPlacenum() {return placenum;}
    public String getClassname() {return classname;}
    public String getteacher() {return teacher;}
    public String getplace() {return place;}
    public ClassAdd(String classname,String teacher,String place,String placenum)
    {
        this.placenum=placenum;
        this.classname=classname;
        this.teacher=teacher;
        this.place=place;
    }
}
package Servlet;
import java.io.IOException;
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 Entity.ClassAdd;
@WebServlet("/Servlet")
public class Servlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;
    public Servlet() {super();}
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
    {
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        if ("add".equals(method)) {add(req, resp); }
    }
    public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
    {
        req.setCharacterEncoding("utf-8");
        String classname= req.getParameter("classname");
        String teacher= req.getParameter("teacher");
        String place= req.getParameter("place");
        String placenum= req.getParameter("placenum");
        ClassAdd cd = new ClassAdd(classname,teacher,place,placenum);
        Dao dao =new Dao();
        boolean f=dao.add(cd);
        if(f) 
        {
            req.setAttribute("message", "添加成功!");
            req.getRequestDispatcher("classadd.jsp").forward(req,resp);
        }
        else 
        {
            req.setAttribute("message", "此课程已存在!");
            req.getRequestDispatcher("classadd.jsp").forward(req,resp);
        }
    }
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> 课程添加 </title>
</head>
<body>
   <form action="Servlet?method=add" method="post">
        <table border="0px" cellpadding="10px" cellspacing="0px"
                style="width: 50%;margin:auto;background:rgb(195,195,195)"  bordercolor="red" >    
            <caption> 添加课程  </caption>
            <tr>
       <td>课程名称: </td>
       <td><input type="text" name="classname"></td>
    </tr>
    <tr>
       <td>教师姓名: </td>
       <td>
           <select name="teacher">
               <option value="王建民"> 王建民 </option>
               <option value="刘立嘉"> 刘立嘉 </option>
               <option value="刘丹"> 刘丹 </option>
               <option value="王辉"> 王辉 </option>
               <option value="杨子光"> 杨子光 </option>
           </select>
       </td>
     </tr>
     <tr>
       <td>地点: </td>
       <td>
           <select name="place">
               <option value="基教"> 基教 </option>
               <option value="一教"> 一教 </option>
               <option value="二教"> 二教 </option>
               <option value="三教"> 三教 </option>
           </select>
       </td>
       <td>教室:  </td>
       <td><input type="text" name="placenum"></td>
       </tr>
                <tr>
                    <th colspan="2">
                        <input type="submit" value="添加">
                    </th>
                </tr>
          </table>
        </form>
    </body>
</html>

运行结果如图:

 

 不足:可视化mysql尚未实现,界面与用户提示信息亦不完美。

 

posted @ 2020-10-23 23:53  谦寻  阅读(96)  评论(0编辑  收藏  举报