zoukankan      html  css  js  c++  java
  • 2009’河北省高教网络技能大赛一网站建设部分

    实现了录入功能

    package dao;
    //对数据库的操作
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    import entity.Course;
    import util.DBUtil;

    /**
    * 课程Dao
    * Dao层操作数据
    * @author Zheng
    *
    */
    public class CourseDao {

    /**
    * 添加 增
    * @param course
    * @return
    */
    public boolean add(Course course) {
    String sql = "insert into course(title, workname, man) values('" + course.getTitle() + "','" + course.getWorkname() + "','" + course.getMan() + "')";
    Connection conn = DBUtil.getConn();
    Statement state = null;
    boolean f = false;
    int a = 0;

    try {
    state = conn.createStatement();
    state.executeUpdate(sql);
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    DBUtil.close(state, conn);
    }

    if (a > 0) {
    f = true;
    }
    return f;
    }

    public boolean title(String title) {
    boolean flag = false;
    String sql = "select name from course where name = '" + title + "'";
    Connection conn = DBUtil.getConn();
    Statement state = null;
    ResultSet rs = null;

    try {
    state = conn.createStatement();
    rs = state.executeQuery(sql);
    while (rs.next()) {
    flag = true;
    }
    } catch (SQLException e) {
    e.printStackTrace();
    } finally {
    DBUtil.close(rs, state, conn);
    }
    return flag;
    }
    }

    package entity;

    public class Course {


    private String title;
    private String workname;
    private String man;

    public String getTitle() {
    return title;
    }
    public void setTitle(String title) {
    this.title = title;
    }
    public String getWorkname() {
    return workname;
    }
    public void setWorkname(String workname) {
    this.workname = workname;
    }
    public String getMan() {
    return man;
    }
    public void setMan(String man) {
    this.man = man;
    }

    public Course() {}
    //*构造方法


    public Course(String title, String workname, String man) {
    this.title = title;
    this.workname = workname;
    this.man = man;
    }
    }

    package service;

    import java.util.List;

    import dao.CourseDao;
    import entity.Course;

    /**
    * CourseService
    * 服务层
    * @author Zheng
    *
    */
    @SuppressWarnings("unused")
    public class CourseService {

    CourseDao cDao = new CourseDao();

    /**
    * 添加
    * @param course
    * @return
    */
    public boolean add(Course course) {
    boolean f = false;
    if(!cDao.title(course.getTitle())) {
    cDao.add(course);
    f = true;
    }
    return f;
    }}

    package servlet;

    import java.io.IOException;
    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 entity.Course;
    import service.CourseService;

    @SuppressWarnings("unused")
    @WebServlet("/CourseServlet")
    public class CourseServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    CourseService service = new CourseService();

    /**
    * 方法选择
    */
    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);
    }
    }

    /**
    * 添加
    * @param req
    * @param resp
    * @throws IOException
    * @throws ServletException
    */
    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    req.setCharacterEncoding("utf-8");
    String title = req.getParameter("title");
    String workname = req.getParameter("workname");
    String man = req.getParameter("man");
    Course course = new Course(title,workname,man);

    //添加后消息显示
    if(service.add(course)) {
    req.setAttribute("message", "添加成功");
    req.getRequestDispatcher("add.jsp").forward(req,resp);
    } else {
    req.setAttribute("message", "课程名称重复,请重新录入");
    req.getRequestDispatcher("add.jsp").forward(req,resp);
    }
    }}

    package util;
    //数据库连接
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    /**
    * 数据库连接工具
    * @author Zheng
    *
    */
    public class DBUtil {

    public static String db_url = "jdbc:mysql://localhost:3306/test?useSSL=false";
    public static String db_user = "root";
    public static String db_pass = "root";

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

    /**
    * 关闭连接
    * @param state
    * @param 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();
    }
    }
    }

    }

    录入界面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    <style>
    .a{
    margin-top: 20px;
    }
    .b{
    font-size: 20px;
    160px;
    color: white;
    background-color: greenyellow;
    }
    </style>
    </head>
    <body>
    <%
    Object message = request.getAttribute("message");
    if(message!=null && !"".equals(message)){

    %>
    <script type="text/javascript">
    alert("<%=request.getAttribute("message")%>");
    </script>
    <%} %>
    <div align="center">
    <h1 style="color: red;">公文录入</h1>
    <a href="index.jsp">返回主页</a>
    <form action="CourseServlet?method=add" method="post" onsubmit="return check()">
    <div class="a">
    公文标题<input type="text" id="title" name="title"/>
    </div>
    <div class="a">
    部门名称<input type="text" id="workname" name="workname" />
    </div>
    <div class="a">
    起草人<input type="text" id="man" name="man" />
    </div>
    <div class="a">
    <button type="submit" class="b">保&nbsp;&nbsp;&nbsp;存</button>
    </div>
    </form>
    </div>
    <script type="text/javascript">
    function check() {
    var title = document.getElementById("title");;
    var workname = document.getElementById("workname");
    var man = document.getElementById("man");

    //非空
    if(title.value == '') {
    alert('公文为空');
    name.focus();
    return false;
    }
    if(workname.value == '') {
    alert('部门为空');
    teacher.focus();
    return false;
    }
    if(man.value == '') {
    alert('起草人');
    classroom.focus();
    return false;
    }


    }
    </script>
    </body>
    </html>

     

     做出了一个注册登陆界面(其中有参考网上部分)

    <%@ page import="java.sql.*" 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>
    </head>
    <body>
    <center>
    <h1 style="color:red">登录</h1>
    <form id="indexform" name="indexForm" action="logincheck.jsp" method="post">
    <table border="0">
    <tr>
    <td>账号:</td>
    <td><input type="text" name="username"></td>
    </tr>
    <tr>
    <td>密码:</td>
    <td><input type="password" name="password">
    </td>
    </tr>
    </table>
    <br>
    <input type="submit" value="登录" style="color:#BC8F8F">
    </form>
    <form action="zhuce.jsp">
    <input type="submit" value="注册" style="color:#BC8F8F">
    </form>
    </center>
    </body>
    </html>

    <%@ page import="java.sql.*" 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=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <jsp:useBean id="db" class="Bean.DBBean" scope="page" />
    <%
    request.setCharacterEncoding("UTF-8");
    String username=(String)request.getParameter("username");
    String password=(String)request.getParameter("password");//取出login.jsp的值

    //下面是数据库操作
    String sql="select * from login where username="+"'"+username+"'";//定义一个查询语句
    ResultSet rs=db.executeQuery(sql);//运行上面的语句
    if(rs.next())
    {
    /* if(password.equals(rs.getString(2)))
    {

    } */
    if(password.equals(rs.getObject("password"))){
    response.sendRedirect("loginsuccess.jsp");
    }
    else{
    out.print("<script language='javaScript'> alert('密码错误');</script>");
    response.setHeader("refresh", "0;url=login.jsp");
    }
    }
    else
    {
    out.print("<script language='javaScript'> alert('账号错误——else');</script>");
    response.setHeader("refresh", "0;url=login.jsp");
    }

    %>
    </body>
    </html>

    <%@ page import="java.sql.*" language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <h1>登陆成功</h1>
    </body>
    </html>

     

  • 相关阅读:
    分分钟搞定Python之排序与列表
    分分钟搞定Python之排序与列表
    联系我
    联系我
    联系我
    联系表单 1_copy
    联系表单 1_copy
    联系表单 1_copy
    联系表单 1
    联系表单 1
  • 原文地址:https://www.cnblogs.com/yyl141/p/12012362.html
Copyright © 2011-2022 走看看