每日练习2.20

每日总结:

  所花时间:4.5h(包含上课)

  代码量:300行

  博客量:1篇

————————————~~~~~~刷~~~~~————————————————

现在是,每日代码时间!

  bean

package com.Bean;

public class bean {
    private String date;
    private String title;
    private String main;
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getMain() {
        return main;
    }
    public void setMain(String main) {
        this.main = main;
    }
}

dao

package com.Dao;

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

import com.Bean.bean;
import com.Util.util;

public class dao {

    public void Add(bean bean)
    {
        Connection connection= util.getConnection();
        PreparedStatement preparedStatement=null;
        try
        {
            String sql="insert into day(date,title,main) values(?,?,?)";
            preparedStatement =connection.prepareStatement(sql);
            preparedStatement.setString(1,bean.getDate());
            preparedStatement.setString(2,bean.getTitle());
            preparedStatement.setString(3,bean.getMain());
            preparedStatement.executeUpdate();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            util.close(preparedStatement);
            util.close(connection);
        }
    }
    
    
}

util

package com.Util;

import java.sql.*;

public class util
{
    public static Connection getConnection()//连接数据库
    {
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            //System.out.println("加载驱动成功");
        }catch(ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        String user="root";
        String password="150372";
        String url = "jdbc:mysql://localhost:3306/days?useSSL=false&serverTimezone=GMT&characterEncoding=utf-8&autoReconnect=true";
        Connection con=null;
        try{
            con= DriverManager.getConnection(url,user,password);
            //System.out.println("数据库连接成功");
        }catch(SQLException e)
        {
            e.printStackTrace();
        }
        return con;
    }
    //**********************************************************************
    //关闭方法
    public  static void close (Connection con)
    {
        try{
            if(con!=null)
            {
                con.close();
            }
        }catch(SQLException e)
        {
            e.printStackTrace();
        }
    }
    public static void close(PreparedStatement preparedStatement)
    {
        try{
            if(preparedStatement!=null)
            {
                preparedStatement.close();
            }
        }catch(SQLException e)
        {
            e.printStackTrace();
        }
    }
    public static void close(ResultSet resultSet)
    {
        try{
            if(resultSet!=null)
            {
                resultSet.close();
            }
        }catch(SQLException e)
        {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws SQLException
    {
    }
}

menu

<%@ 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="#" method="get">
    <p style="text-align:center;font-size:20px" >
        功能选择
        <br>
        <input type="button" value="新增日志" onclick="location.href='add.jsp'" /><br>
        <input type="button" value="日志查看" onclick="location.href='check.jsp'" /><br>

        <br>
    </p>
</form>
</body>
</html>

check

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.Util.util" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="com.Bean.bean" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="com.Dao.dao" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p style="text-align:center;color: black; font-family: 宋体; font-size: 20px">
    <input type="button" value="返回菜单" onclick="location.href='menu.jsp'" /> <br>
</p>
<table border="1"cellspacing="0"style="text-align:center;">
    <tr>
        <td align="center" width=10%>日期</td>
        <td align="center" width=10%>关键字</td>
        <td align="center" width=60%>内容</td>
    </tr>
<%

Connection connection = util.getConnection();
PreparedStatement preparedStatement=null;
ResultSet rs=null;
try {
    String sql = "select * from day";
    preparedStatement=connection.prepareStatement(sql);
    rs=preparedStatement.executeQuery();
    while(rs.next()){
       
            
            
%>

    <tr>
        <td align="center"width=10%><%=rs.getObject(1) %></td>
        <td align="center"width=10%><%=rs.getObject(2) %></td>
        <td align="center"width=60%><%=rs.getObject(3) %></td>
    </tr>
<%
        
    }
} catch (SQLException  e) {
    e.printStackTrace();
}finally{
    util.close(rs);
    util.close(preparedStatement);
    util.close(connection);
}
%>

</body>
</html>

add

<%@ 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="add_back.jsp" method="get">
    <p style="text-align:center;color: black; font-family: 宋体; font-size: 20px">
        日期
        <input type="text" name="date" >
        <br>
        关键字
        <input type="text" name="title" >
        <br>
        内容
        <input type="text" name="main" >
        <br>
        <input type="submit" value="写入" >
    </p>
</form>
</body>
</html>

add_back

<%@ page import="java.sql.SQLException" %>
<%@ page import="com.Bean.bean" %>
<%@ page import="com.Dao.dao" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>

<%
    String date = request.getParameter("date");
    String title = request.getParameter("title");
    String main = request.getParameter("main");

    bean bean = new bean();
      bean.setDate(date);
      bean.setTitle(title);
      bean.setMain(main);

    dao dao = new dao();
    dao.Add(bean);

    out.print("<script language='javaScript'> alert('写入成功');</script>");
    response.setHeader("refresh", "0;url=menu.jsp");



%>

</html>

————————————~~~~~~刷~~~~~————————————————

恭喜你看完了所有内容,现在是每日一图时间!

 

posted on 2023-02-20 21:58  wardream  阅读(25)  评论(0)    收藏  举报