新课程添加

实现对于新课程的添加

Userbean.java

package com.Bean;

public class Userbean
{

    private String  teachername;
    private String  place;
    private String  classname;

    public String getTeachername() {
        return teachername;
    }
    public void setTeachername(String teachername) {
        this.teachername = teachername;
    }
    public String getPlace() {
        return place;
    }
    public void setPlace(String place) {
        this.place = place;
    }
    public String getClassname() {
        return classname;     }
    public void setClassname(String classname) {
        this.classname = classname;
    }

}

Userdao.java

package com.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.Util.Userutil;
import com.Bean.Userbean;


public class Userdao {

    public void add(Userbean claz) throws ClassNotFoundException, SQLException
    {
        //获得链接对象
        Connection connection = Userutil.getConnection();
        //准备sql语句
        String sql = "insert into course(teachername,classname,place) values(?,?,?)";
        PreparedStatement preparedStatement = null;
        try {
            //创建语句传输对象
            preparedStatement = connection.prepareStatement(sql);
            // preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, claz.getTeachername());
            preparedStatement.setString(2, claz.getClassname());
            preparedStatement.setString(3, claz.getPlace());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            //关闭资源

            Userutil.close(preparedStatement);
            Userutil.close(connection);
        }

    }

Userutil.java

package com.Util;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



/**
 * JDBC快速入门
 */
public class Userutil {
    public static Connection getConnection() throws ClassNotFoundException, SQLException {

        Connection connection = null;//连接数据库
        Statement stmt = null;//Statement 对象用于将 SQL 语句发送到数据库中。
        ResultSet rs = null;
        //1. 导入驱动jar包
        //2.注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/classadd", "root", "123456");

        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();
        }
    }

}

add.jsp

<%--
  Created by IntelliJ IDEA.
  User: 榕宝
  Date: 2021/11/4
  Time: 21:25
  To change this template use File | Settings | File Templates.
--%>

<%@page import="com.Bean.Userbean"%>
<%@page import="com.Dao.Userdao"%>
<%@ page import="java.sql.SQLException" %>
<%@ page language= "java" contentType= "text/html; charset=GB18030"
         pageEncoding= "GB18030" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
    //接收客户端传递过来的参数

    String teachername = request.getParameter("teachername");
    String classname = request.getParameter("classname");
    String place = request.getParameter("place");
    //try{
    // if(!teachername.trim().equals("王建民")&&!teachername.trim().equals("刘立嘉")&&!teachername.trim().equals("刘丹")&&!teachername.trim().equals("王辉")&&!teachername.trim().equals("杨子光"))
    // {
    // throw new ClassException("不能输入规定之外的老师名称");
    //  }
    //  else if(!place.trim().startsWith("基教")&&!place.trim().startsWith("一教")&&!place.trim().startsWith("二教")&&!place.trim().startsWith("三教"))
    //  {
    // throw new ClassException("不能输入规定之外的地点名称!");
    //  }
    //   else
    //   {
    Userbean A = new Userbean();
    A.setTeachername(teachername);
    A.setClassname(classname);
    A.setPlace(place);
    Userdao Dao = new Userdao();
    try {
        Dao.add(A);
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    }
    //  }





%>

</html>

jiemian.jsp

<%--
  Created by IntelliJ IDEA.
  User: 榕宝
  Date: 2021/11/4
  Time: 21:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page language= "java" contentType= "text/html; charset=GB18030"
         pageEncoding= "GB18030" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>课程添加页面</title>
</head>
<body>
<form action="add.jsp" method="get">
    <table align="center" border="1" width="500">
        <tr>
            <td>课程名称 : </td>
            <td>
                <label>
                    <input type="text" name="classname" />
                </label>
            </td>
        </tr>
        <tr>
            <td>任课教师:</td>
            <td>
                <label>
                    <input type="text" name="teachername" />
                </label>
            </td>
        </tr>
        <tr>
            <td>上课地点:</td>
            <td>
                <label>
                    <input type="text" name="place" />
                </label>
            </td>
        </tr>
        <tr align="center">
            <td colspan="2">
                <input type="submit" value="提交" />
                <input type="reset" value="重置" />
            </td>
        </tr>
    </table>
</form>
</body>
</html>

 

posted @ 2021-10-27 20:47  zrswheart  阅读(56)  评论(0编辑  收藏  举报