1. 程式人生 > 其它 >LeetCode——714. 買賣股票的最佳時機含手續費

LeetCode——714. 買賣股票的最佳時機含手續費

10.24

1

今天完成了Javaweb和資料庫連線,課程錄入的程式碼

aaa.jsp:

<%@ 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;
        width: 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="${pageContext.request.contextPath }/addServlet" method="post" onsubmit="return check()">
            <div class="a">
                課程名稱<input type="text" id="name" name="name"/>
            </div>
            <div class="a">
                任課教師<input type="text" id="teacher" name="teacher" />
            </div>
            <div class="a">
                上課地點<input type="text" id="classroom" name="classroom" />
            </div>
            <div class="a">
                <button type="submit" class="b">保   存<tton>
            </div>
           <tr>
                <td>學歷:</td>
                <td>
                    <input type="radio"name="degree"value="專科">專科
                    <input type="radio"name="degree"value="本科"checked>本科
                    <input type="radio"name="degree"value="碩士研究生">碩士研究生
                    <input type="radio"name="degree"value="博士研究生">博士研究生
                    <input type="radio"name="degree"value="其他">其他
                </td>
            </tr>
        </form>
    </div>
    <script type="text/javascript">
        function check() {
            var name = document.getElementById("name");;
            var teacher = document.getElementById("teacher");
            var classroom = document.getElementById("classroom");
             
            //非空
            if(name.value == '') {
                alert('課程名稱為空');
                name.focus();
                return false;
            }
            if(teacher.value == '') {
                alert('教師為空');
                teacher.focus();
                return false;
            }
            if(classroom.value == '') {
                alert('上課地點為空');
                classroom.focus();
                return false;
            }
             
            //教師
            if(teacher.value != '王建民' && teacher.value != '王輝' && teacher.value != '劉丹' && teacher.value != '劉立嘉' && teacher.value != '楊子光'){
                alert('教師名稱錯誤');
                return false;
            }
             
            //教室
            if(!/^基教/.test(classroom.value) && !/^一教/.test(classroom.value) && !/^二教/.test(classroom.value) && !/^三教/.test(classroom.value)) {
                alert('上課地點錯誤');
                return false;
            }
        }
            </script type="text/javascript">

DBUtil:

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/test?serverTimezone=UTC";
    public static String db_user = "root";
    public static String db_pass = "chenhan";

    public static Connection getConn () {
        Connection conn = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(db_url, db_user, db_pass);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return conn;
    }//end getConn

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

    public static void main(String[] args) throws SQLException {
        Connection conn = getConn();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql ="select * from mmm";
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        if(rs.next()){
            System.out.println("連線成功");
        }else{
            System.out.println("連線失敗");
        }
    }
}

Dao:

package Dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


import DBUtil.DBUtil;
import domain.Course;
 

public class CourseDao {
     
    /**
     * 新增
     * @param course
     * @return
     */
    public boolean add(Course course) {
        String sql = "insert into mmm(name, teacher, classroom) values('" + course.getName() + "','" + course.getTeacher() + "','" + course.getClassroom() + "')";
        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;
    }
}

domain:

package domain;

public class Course {

    private String name;
    private String teacher;
    private String classroom;
     

    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 getClassroom() {
        return classroom;
    }
    public void setClassroom(String classroom) {
        this.classroom = classroom;
    }
     
    public Course() {}
     
    public Course(String name, String teacher, String classroom) {
        
        this.name = name;
        this.teacher = teacher;
        this.classroom = classroom;
    }
}

Servlet:

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.CourseDao;
import domain.Course;

/**
 * Servlet implementation class AddServlet
 */
@WebServlet("/addServlet")
public class AddServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        String name =request.getParameter("name");
        String teacher = request.getParameter("teacher");
        String classroom =request.getParameter("classroom");
        Course course = new Course(name, teacher, classroom);
        CourseDao cd = new CourseDao(); 
        //新增後訊息顯示
        if(cd.add(course)) {
            request.getRequestDispatcher("aaa.jsp").forward(request,response);
        } else {
            request.getRequestDispatcher("aaa.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);
    }

}

2

對於程式碼中各個類的含義還不清楚,jsp中各部分的含義也不理解。

3繼續學習jsp