题目
每日总结打卡:内容包括:日期、每日关键字、每日总结、坚持天数(自动计数,显示上次天数)、连续最长天数。
代码如下
package 每日打卡;
public class Bean {
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getDays() {
return Days;
}
public void setDays(String days) {
Days = days;
}
public String getLongestDays() {
return LongestDays;
}
public void setLongestDays(String longestDays) {
LongestDays = longestDays;
}
private String date;
private String keyword;
private String summary ;
private String Days ;
private String LongestDays ;
}
package 每日打卡;
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 Util {
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.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/daka ?useUnicode=true&characterEncoding=utf-8", "root", "root");
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();
}
}
}
package 每日打卡;
import java.sql.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import 每日打卡.Bean;
import 每日打卡.Util;
public class Dao {
public int add(Bean claz) throws ClassNotFoundException, SQLException{
Connection connection = Util.getConnection();
//准备sql语句
String sql = "insert into daka(date,keyword,summary,Days,LongestDays) values(?,?,?,?,?)";
PreparedStatement preparedStatement = null;
try {
//创建语句传输对象
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, claz.getDate());
preparedStatement.setString(2, claz.getKeyword());
preparedStatement.setString(3, claz.getSummary());
preparedStatement.setString(4, claz.getDays());
preparedStatement.setString(5, claz.getLongestDays());
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//关闭资源
Util.close(preparedStatement);
Util.close(connection);
}
return 1;
}
}
<%@page import="每日打卡.Bean"%>
<%@page import="每日打卡.Dao"%>
<%@ page import="java.sql.SQLException" %>
<%@ 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>
<meta charset="UTF-8">
<%
int i=0;
String date = request.getParameter("date");
String keyword = request.getParameter("keyword");
String summary = request.getParameter("summary");
String Days = request.getParameter("Days");
String LongestDays = request.getParameter("LongestDays");
Bean bean = new Bean();
bean.setDate(date);
bean.setKeyword(keyword);
bean.setSummary(summary);
bean.setDays(Days);
bean.setLongestDays(LongestDays);
Dao dao =new Dao();
try {
i=dao.add(bean);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
if(i==1)
response.getWriter().append("填报成功!");
else
response.getWriter().append("信息填报错误!");
response.sendRedirect("index.jsp");
%>
</html>
<%!
%>
<%@ 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="back.jsp" method="get">
<table align="center" border="1" width="500">
<tr>
<td>1.日期: </td>
<td>
<label>
<input type="text" name="date" />
</label>
</td>
</tr>
<tr>
<td>2.每日关键字: </td>
<td>
<label>
<input type="text" name="keyword" />
</label>
</td>
</tr>
<tr>
<td>3.每日总结: </td>
<td>
<label>
<input type="text" name="summary" />
</label>
</td>
</tr>
<tr>
<td>4.坚持天数: </td>
<td>
<label>
<input type="text" name="Days" />
</label>
</td>
</tr>
<tr>
<td>5.最长天数: </td>
<td>
<label>
<input type="text" name="LongestDays" />
</label>
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>
</body>
</html>
代码还不够完善,只能实现添加
标签:总结,preparedStatement,20,String,java,2023.2,sql,import,public From: https://www.cnblogs.com/fuchuchu/p/17139069.html