课堂练习-增加信息

1、设计思想
在add.jsp中获取addInput.jsp中传递过来的课程名称,教师姓名,上课地点,利用String类中的equals和startWith方法分别判断教师姓名和上课的地点的合理性,最后在.java文件中写出链接数据库和添加信息到数据库的函数,调用并实现。
2、程序源代码
链接数据库:
DBUtil.java

package com.jaovo.msg.Util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
    public static Connection getConnection() {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String user = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/jaovo_msg";
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return connection;
    }
    //关闭资源
    public static void close(Connection connection) {
        try {
            if(connection != null) {
                connection.close();
            }        
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void close(PreparedStatement preparedstatement) {
        try {
            if(preparedstatement != null) {
            preparedstatement.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void close(ResultSet resultset) {
        try {
            if(resultset != null) {
            resultset.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

接口:

IScourseDao.java

package com.jaovo.msg.dao;

import com.jaovo.msg.model.Scourse;

public interface IScourseDao {
    public void add(Scourse scourse);
}

添加功能实现:

ScourseDaoImpl.java

package com.jaovo.msg.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.jaovo.msg.Util.DBUtil;
import com.jaovo.msg.model.Scourse;

public class ScourseDaoImpl implements IScourseDao{

    @Override
    public void add(Scourse scourse) {
        // TODO Auto-generated method stub
        Connection connection = DBUtil.getConnection();
        PreparedStatement preparedstatement = null;
        ResultSet resultset = null;
        try {
            String sql = "insert into scourse(scoursename,teacher,place) value (?,?,?)";
            preparedstatement = connection.prepareStatement(sql);
            preparedstatement.setString(1, scourse.getScourse());
            preparedstatement.setString(2, scourse.getTeacher());
            preparedstatement.setString(3, scourse.getPlace());
            preparedstatement.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(resultset);
            DBUtil.close(preparedstatement);
            DBUtil.close(connection);
        }
    }
}

Scourse.java

package com.jaovo.msg.model;

public class Scourse {
    private String scourse;
    private String teacher;
    private String place;
    public String getScourse()
    {
        return scourse;
    }
    public void setScourse(String scourse)
    {
        this.scourse = scourse;
    }
    public String getTeacher() 
    {
        return teacher;
    }
    public void setTeacher(String teacher) 
    {
        this.teacher = teacher;
    }
    public String getPlace()
    {
        return place;
    }
    public void setPlace(String place) 
    {
        this.place = place;
    }
}

addInput.jsp

<%@page import="com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg"%>
<%@ 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>
    <title>用户添加页面</title>
</head>
<body>
    <div align = "center">
    <form action="add.jsp" method="get">
    <% 
        if(request.getAttribute("error") == null)
        {
            out.println("课程添加");
        }
        else
        {
            out.println(request.getAttribute("error"));
        }
    %>
    <table>
        <tr>
            <td>课程名称 :</td>
            <td><input type="text" name="scoursename" /></td>
        </tr>
        <tr>
            <td>任课教师:</td>
            <td><input type="text" name="teacher" /></td>
        </tr>
        <tr>
            <td>上课地点:</td>
            <td><input type="text" name="place" /></td>
        </tr>
    </table>
            <input style = "120px;height:30px;" type = "submit" value = "保存"/>
    </form>
    </div>
</body>
</html>

add.jsp

<%@page import="com.jaovo.msg.dao.ScourseDaoImpl"%>
<%@page import="com.jaovo.msg.model.Scourse"%>
<%@ 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>
<%
    //接收客户端传递过来的参数
    String scoursename = request.getParameter("scoursename");
    String teacher = request.getParameter("teacher");
    String place = request.getParameter("place");
    if(teacher.equals("王建民")||teacher.equals("刘立嘉")||teacher.equals("刘丹")||teacher.equals("王辉")||teacher.equals("杨子光"))
    {
    }
    else
    {
        request.setAttribute("error", "教师姓名有误!");
%>
        <jsp:forward page = "addInput.jsp"></jsp:forward>
<%
    }
    if(place.startsWith("一教")||place.startsWith("二教")||place.startsWith("三教")||place.startsWith("基教"))
    {
    }
    else
    {
        request.setAttribute("error", "上课地点有误!");
%>
        <jsp:forward page = "addInput.jsp"></jsp:forward>
<%
    }
    Scourse scourse = new Scourse();
    scourse.setScourse(scoursename);
    scourse.setTeacher(teacher);
    scourse.setPlace(place);
    ScourseDaoImpl userDao = new ScourseDaoImpl();
    userDao.add(scourse);
    out.print("添加成功!");
%>
</html>

运行结果截图:

 

原文地址:https://www.cnblogs.com/guo-xu/p/7911275.html