第一次冲刺(三)

今天我完成了修改密码的activity。

代码为:

package com.example.hotel.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.hotel.Database.MysqlUser;
import com.example.hotel.R;

import java.sql.SQLException;

public class EditpwdActivity extends AppCompatActivity {
    private Button editButton;
    private EditText Editpwd_id,Editpwd_oldpwd,Editpwd_newpwd;
    private String editsearchid,editsearcholdpwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editpwd);
        Editpwd_id=findViewById(R.id.editpwd_id);
        Editpwd_oldpwd=findViewById(R.id.editpwd_oldpwd);
        Editpwd_newpwd=findViewById(R.id.editpwd_newpwd);
        editButton=findViewById(R.id.editpwd);
        editButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Thread thread=new Thread(new Runnable() {
                    @Override
                    public void run() {
                        MysqlUser mysqlUser=new MysqlUser();
                        try {
                            String sql="select password from userinfo where id='"+Editpwd_id.getText().toString()+"'";
                            editsearcholdpwd=mysqlUser.select(sql).toString();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                });
                thread.start();
                try {
                    thread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                edit();
            }
        });
    }
    public void edit(){
        if(editsearcholdpwd!=null){

            Toast.makeText(this,"修改成功!",Toast.LENGTH_SHORT).show();
            Thread thread=new Thread(new Runnable() {
                @Override
                public void run() {
                    MysqlUser mysqlUser=new MysqlUser();
                    try {
                        String sql="update userinfo set password ="+Editpwd_newpwd.getText().toString()+" where id='"+Editpwd_id.getText().toString()+"'";
                        mysqlUser.update(sql);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            });
            thread.start();
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

 

posted @ 2021-05-08 21:37  张笑天  阅读(27)  评论(0编辑  收藏  举报