首页 > 其他分享 >今日总结

今日总结

时间:2023-02-22 23:34:28浏览次数:31  
标签:总结 preparedStatement sign static import 今日 com id

今天写了一堆代码(一堆BUG),明天来改

package com.example.stusign;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.service.autofill.OnClickAction;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.stusign.pojo.Sign;
import com.example.stusign.pojo.StuDay;
import com.example.stusign.utils.MysqlUtils;
import com.example.stusign.utils.SignUtils;
import com.example.stusign.utils.StuUtils;

import java.sql.Connection;
import java.time.LocalDate;

public class SignActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.everyday);
        Button button=findViewById(R.id.add);
        button.setOnClickListener(this);
        button=findViewById(R.id.close);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.add:
                EditText editText=findViewById(R.id.id);
                int id=Integer.parseInt(editText.getText().toString());
                editText=findViewById(R.id.keywords);
                String keywords=editText.getText().toString();
                editText=findViewById(R.id.summary);
                String summary=editText.getText().toString();
                String sqlpath=getFilesDir()+"/sign.db";
                SignUtils signUtils=SignUtils.getSignUtils(this);
                SQLiteDatabase sqLiteDatabase=openOrCreateDatabase(sqlpath, Context.MODE_PRIVATE,null);
                AlertDialog.Builder builder=new AlertDialog.Builder(this);
                builder.setTitle("打卡结果");
                if(signUtils.add(sqLiteDatabase,new Sign(id, LocalDate.now().toString(),keywords,summary))){
                   StuDay stuDay=MysqlUtils.getdays(id);
                    builder.setMessage("打卡成功\n已坚持天数:\n"+Integer.toString(stuDay.getDays())+"最长坚持天数:"+Integer.toString(stuDay.getLongdays()));
                }
                else{
                    builder.setMessage("打卡失败");
                }
                builder.setPositiveButton("确定",null);
                sqLiteDatabase.close();
                builder.show();
                break;
            case R.id.close:
                finish();
                break;
        }
    }
}
package com.example.stusign;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.stusign.utils.MysqlUtils;
import com.example.stusign.utils.SignUtils;
import com.example.stusign.utils.StuUtils;
import com.example.stusign.pojo.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button b1 = findViewById(R.id.b1);
        b1.setOnClickListener(this);
        Button b2 = findViewById(R.id.b2);
        b2.setOnClickListener(this);
        Button b3 = findViewById(R.id.b3);
        b3.setOnClickListener(this);
        SQLiteDatabase sqLiteDatabase1 = openOrCreateDatabase(getFilesDir() + "/stu.db", Context.MODE_PRIVATE, null);
        SQLiteDatabase sqLiteDatabase2 = openOrCreateDatabase(getFilesDir() + "/sign.db", Context.MODE_PRIVATE, null);
        StuUtils stuUtils = StuUtils.getStuUtils(this);
        stuUtils.onCreate(sqLiteDatabase1);
        Stu stu = stuUtils.getstu(sqLiteDatabase1);
        SignUtils signUtils = SignUtils.getSignUtils(this);
        signUtils.onCreate(sqLiteDatabase2);
        Sign sign = signUtils.getsign(sqLiteDatabase2);
        TextView textView=findViewById(R.id.test);
        textView.setText(String.valueOf(MysqlUtils.getConnection()));
       /* MysqlUtils.addstu(stu);
        MysqlUtils.addsign(sign);
        stuUtils.delete(sqLiteDatabase1);
        signUtils.delete(sqLiteDatabase2);
        sqLiteDatabase1.close();
        sqLiteDatabase2.close();
        if (sign != null) {
            List<Sign> signs = MysqlUtils.select(sign.getId());
            int res = 0;
            int k = 0;
            for (int i = 0; i < signs.size() - 1; i++) {
                Sign st = signs.get(i);
                Sign en = signs.get(i + 1);
                try {
                    DateFormat dft = new SimpleDateFormat("yyyy-mm-dd");
                    Date start = dft.parse(st.getDate());
                    Date end = dft.parse(en.getDate());
                    if (((long) end.getTime() - (long) start.getTime()) / 24 / 3600 / 1000 == 1) {
                        k++;
                    } else {
                        res = Math.max(res, k);
                        k = 0;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            MysqlUtils.addStuDay(new StuDay(sign.getId(), signs.size(), res));
            MysqlUtils.update(new StuDay(sign.getId(), signs.size(), res));
        }
        MysqlUtils.close();*/
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.b1:
                startActivity(new Intent(MainActivity.this,ResigerActivity.class));
                break;
            case R.id.b2:
                startActivity(new Intent(MainActivity.this,SignActivity.class));
                break;
            case R.id.b3:
                break;
        }
    }
}
package com.example.stusign.utils;

import android.database.Cursor;

import com.example.stusign.pojo.Sign;
import com.example.stusign.pojo.Stu;
import com.example.stusign.pojo.StuDay;

import java.sql.*;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

public class MysqlUtils {
    private static  String driver;
    private static  String url;
    private static  String username;
    private static  String password;
    private static Connection connection;
    private static PreparedStatement preparedStatement;
//    static {
//        try {
//            driver="com.mysql.cj.jdbc.Driver";
//            url="jdbc:mysql://172.18.56.110:3306/exam";
//            username="root";
//            password="111111";
//            Class.forName(driver);
//            connection=DriverManager.getConnection(url,username,password);
//        }
//        catch (Exception e){
//            e.printStackTrace();
//        }
//    }
    public static Connection getConnection(){
        try {
            driver="com.mysql.cj.jdbc.Driver";
            url="jdbc:mysql://172.18.56.110:3306/exam";
            username="root";
            password="111111";
            Class.forName(driver);
            connection=DriverManager.getConnection(url,username,password);
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return connection;
    }
    public static void addstu(Stu stu){
        try {
            if(stu!=null) {
                preparedStatement = connection.prepareStatement("insert ignore into stu values(?,?,?,?)", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
                preparedStatement.setInt(0, stu.getId());
                preparedStatement.setString(1, stu.getName());
                preparedStatement.setString(2, stu.getPhone());
                preparedStatement.setString(3, stu.getUnit());
                preparedStatement.execute();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void addsign(Sign sign){
        try {
            if(sign!=null) {
                preparedStatement = connection.prepareStatement("insert ignore into `sign` values(?,?,?,?)", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
                preparedStatement.setInt(0, sign.getId());
                preparedStatement.setString(1, sign.getDate());
                preparedStatement.setString(2, sign.getKeywords());
                preparedStatement.setString(3, sign.getSummary());
                preparedStatement.execute();
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static List<Sign> select(int id){
        try {
            preparedStatement=connection.prepareStatement("select * from  `sign` where id=? order by date",ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            preparedStatement.setInt(0,id);
            ResultSet resultSet=preparedStatement.executeQuery();
            List<Sign> signs=new ArrayList<>();
            while(resultSet.next()){
                signs.add(new Sign(resultSet.getInt("id"),resultSet.getString("date"), resultSet.getString("keywords"),resultSet.getString("summary")));
            }
            return signs;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public static void update(StuDay stuDay){
        try {
            if(stuDay!=null) {
                preparedStatement = connection.prepareStatement("update studay set days=?,longdays=? where id=?", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
                preparedStatement.setInt(2, stuDay.getId());
                preparedStatement.setInt(0, stuDay.getDays());
                preparedStatement.setInt(1, stuDay.getLongdays());
                preparedStatement.execute();
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void addStuDay(StuDay stuDay){
        try {
            if(stuDay!=null) {
                preparedStatement = connection.prepareStatement("insert ignore into studay values(?,?,?)", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
                preparedStatement.setInt(0, stuDay.getId());
                preparedStatement.setInt(1, stuDay.getDays());
                preparedStatement.setInt(2, stuDay.getLongdays());
                preparedStatement.execute();
            }
            }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    public static StuDay getdays(int id){
        try {
            preparedStatement=connection.prepareStatement("select * from  studay where id=?",ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
            preparedStatement.setInt(0,id);
            ResultSet resultSet=preparedStatement.executeQuery();
            resultSet.next();
            return new StuDay(resultSet.getInt("id"),resultSet.getInt("days"),resultSet.getInt("longdays"));

        }
        catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public static void close(){
        try {
            connection.close();
            preparedStatement.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 

标签:总结,preparedStatement,sign,static,import,今日,com,id
From: https://www.cnblogs.com/liyiyang/p/17146397.html

相关文章

  • 【3】注册功能脚本研发及总结
                  register_per_test.py#针对注册功能进行性能测试fromlocustimportHttpLocust,task,TaskSetclassUserBehavior_reg(TaskSe......
  • 每日总结2023/2/22
    今天有一上午的课程,下午进行短暂的休息之后,开始了今天的学习,进行第一个安卓程序运行,学习控件1_TextView1.文本的内容,组件id,字体颜色,风格,大小,对齐方式,背景颜色,控件的......
  • 每日总结
    Fragmentfragment可以看做APP页面跳转,可以看做一个小型的activity,其流程是在main_activity的xml里面可以定一个Fragmentlayout的布局,这个布局用于存放fragment然后给这......
  • 2023/02/21每日总结
    这两天发现自己之前学习的视频多的是苦苦坚持,没有感受到乐趣,现在找到适合自己的视频,从前面开始学一学。publicstaticvoidmain(String[]args)中args可随意更改,只是变量......
  • 每日总结(3)
    所用时间:下午5个小时,上午计网课程代码:32博客:0知识点:springboot的控制层访问数据库vue组件化开发,利用vuecli1@RestController2publicclassUserControl......
  • 【Vue Router】019-动态路由 和 学习总结
    1.19动态路由1.19.1概述添加路由到你的路由上通常是通过​​routes​​​,但是在某些情况下,你可能想在应用程序已经运行的时候添加或删除路由。具有可扩展接口(如​​Vue......
  • 今日学习总结-03-开始学习安卓-安装Genymotion虚拟机
    今天先安装一个Genymotion虚拟机,日后用起来更加方便首先去官网下载:点第二个有vbox的版本然后是简单的安装步骤 注册并登录进入主页面  新建一个虚拟设备  ......
  • 2.22学习总结
    终于实现了Javaweb对数据库的增,查功能!前期失败原因总结如下1.tomcat服务器使用版本为10.0,代码中总出现许多无法解决且不报错的问题导致无法实现正常供能,更换9.0之后问题得......
  • 2023年2月22日学习总结
    今天继续去实现一个简单的增删改查的项目,通过向同学寻求帮助,按照同学的模板根据自己的需求制作了一个简单的学生信息管理系统,实现了对信息的增删改查。接着下载安装并配置......
  • 2023.2.22每日总结
    从昨天学习json添加依赖包的时候就发现的问题——无法解alibaba的依赖包,maven的setting.xml设置里也配置了阿里的镜像仓库,于今天解决 ......