每日总结
每日总结打卡:内容包括:日期、每日关键字、每日总结、坚持天数(自动计数,显示上次天数)、连续最长天数。
要求:连接数据库,并且可以通过web界面实现数据的增加。
连接数据库如下:
package com.student.test;
import java.sql.*;
public class MySQLDemo {
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/student?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
static final String USER = "root";
static final String PASS = "123456";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
Class.forName(JDBC_DRIVER);
System.out.println("连接数据库...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println(" 实例化Statement对象...");
stmt = conn.createStatement();
String sql;
sql = "SELECT data, word, more FROM apps";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int data = rs.getInt("data");
String word = rs.getString("word");
String more = rs.getString("more");
System.out.print("DATA: " + data);
System.out.print(", 每日关键字: " + word);
System.out.print(", 每日总结: " + more);
System.out.print("\n");
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null) stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null) conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
进行输入的web界面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> 每日打卡</title>
<style type="text/css">
body{
background: #d7c7e9;
align-items: center;
text-align: center;
}
</style>
<script type="text/javascript">
function check(form){
with(form){
if(data.value === ""){
alert("日期不能为空");
return false;
}
if(word.value === ""){
alert("每日关键字不能为空");
return false;
}
if(more.value === ""){
alert("每日总结不能为空");
return false;
}
if(days.value === ""){
alert("连续天数不能为空");
return false;
}
return true;
}
}
</script>
</head>
<body>
<form action="NewFile1.jsp" method="post" onsubmit="return check(this);">
<table align="center" width="450">
<tr>
<td align="center" colspan="2">
<h2>打卡</h2>
<hr>
</td>
</tr>
<tr>
<td align="right">日期:</td>
<td>
<label>
<input type="text" name="name"/>
</label>
</td>
</tr>
<tr>
<td align="right">每日关键字:</td>
<td>
<label>
<input type="text" name="price"/>
</label>
</td>
</tr>
<tr>
<td align="right">每日总结:</td>
<td>
<label>
<input type="text" name="bookCount"/>
</label>
</td>
</tr>
<tr>
<td align="right">连续天数:</td>
<td>
<label>
<input type="text" name="author"/>
</label>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</body>
</html>
输入之后的web界面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.PreparedStatement" %>
<html>
<head>
<title>添加结果</title>
<style type="text/css">
body{
background: #d7c7e9;
align-content: center;
text-align: center;
}
</style>
</head>
<body>
<%request.setCharacterEncoding("UTF-8");%>
<jsp:useBean id="book" class="com.student.test.book"></jsp:useBean>
<jsp:setProperty name="book" property="*"/>
<%
Connection conn =null;
try{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
out.print("驱动器加载失败");
}
String url = "jdbc:mysql://localhost:3306/student?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
String username = "root";
String password = "123456";
try{
conn = DriverManager.getConnection(url,username,password);
}catch (SQLException e){
out.println(e.getMessage());
out.println("数据库连接失败");
}
if(conn != null){
String sql = "insert into tb_books(name,price,bookCount,author)values(?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,book.getName());
ps.setDouble(2,book.getPrice());
ps.setInt(3,book.getBookCount());
ps.setString(4,book.getAuthor());
int row = ps.executeUpdate();
if(row>0){
out.println("成功添加了" + row + "条数据!");
}
}
}catch (Exception e){
e.printStackTrace();
out.print("信息添加失败!");
}
%>
<br>
<a href="NewFile.jsp">返回</a>
</body>
</html>
界面操作:
package com.student.test;
public class book {
private int data;
private String word;
private double more;
private int days;
private String thedays;
public int getId() {
return data;
}
public void setId(int data) {
this.data = data;
}
public String getName() {
return word;
}
public void setName(String word) {
this.word = word;
}
public double getPrice() {
return more;
}
public void setPrice(double more) {
this.more = more;
}
public int getBookCount() {
return days;
}
public void setBookCount(int days) {
this.days = days;
}
public String getAuthor() {
return thedays;
}
public void setAuthor(String thedays) {
this.thedays = thedays;
}
}
通过web进行数据库操作的代码尚未完成。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现