第一阶段冲刺五

忘记密码,修改密码,退出登录

一、忘记密码

1.样图

 

 

2.代码

忘记密码activity

package com.example.hotel.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

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

import java.sql.SQLException;

public class NopwdActivity extends AppCompatActivity {
    private Button Button;
    private EditText Userid,Userphone;
    private TextView Pwd;
    private String searpwd,searphone;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nopwd);
        Userid=(EditText) findViewById(R.id.userid);
        Userphone=(EditText)findViewById(R.id.userphone);
        Pwd=(TextView) findViewById(R.id.pwdresult);
        Button=findViewById(R.id.searchpwd);
        Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        MysqlUser mysqlUser=new MysqlUser();
                        try {
                            String sql="select password from userinfo where id='"+Userid.getText().toString()+"'";
                            searpwd=mysqlUser.select(sql).toString();
                            String sql2="select phone from userinfo where id='"+Userid.getText().toString()+"'";
                            searphone=mysqlUser.select(sql2).toString();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                search();
            }
        });
    }
    public void search(){
        String userid=Userid.getText().toString();
        String userphone=Userphone.getText().toString();
        if(userphone.equals(searphone)){
            Pwd.setText("您的密码为:"+searpwd);
        }
    }

}

忘记密码布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
tools:context=".Activity.NopwdActivity">

    <TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginTop="50dp"
       
android:layout_marginLeft="10dp"
       
android:textColor="#03A9F4"
       
android:text="请输入您的id"/>

    <EditText
       
android:id="@+id/userid"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_margin="10dp" />

    <TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginTop="50dp"
       
android:layout_marginLeft="10dp"
       
android:textColor="#03A9F4"
       
android:text="请输入您的手机号"/>

    <EditText
       
android:id="@+id/userphone"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_margin="10dp"
       
/>

    <TextView
       
android:id="@+id/pwdresult"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
        
android:layout_marginTop="50dp"
       
android:layout_marginLeft="10dp"
       
android:textSize="20sp"
       
android:textColor="#ED2A08"
       
android:text=""/>

    <Button
       
android:id="@+id/searchpwd"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="50dp"
       
android:background="@drawable/btnpress"
       
android:text="查询"/>
</LinearLayout>

 

 

修改密码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_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) {
                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();
                        }
                    }
                }).start();
                edit();
            }
        });
    }
    public void edit(){
        if(editsearcholdpwd!=null){
            Toast.makeText(this,"修改成功!",Toast.LENGTH_SHORT).show();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    MysqlUser mysqlUser=new MysqlUser();
                    try {
                        String sql="update user set password ="+Editpwd_newpwd.getText().toString()+"where id='"+Editpwd_id.getText().toString()+"'";
                        editsearcholdpwd=mysqlUser.select(sql).toString();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}

 

连接数据库代码

package com.example.hotel.Database;

import android.util.Log;

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 MysqlUser {

    PreparedStatement statement;
    public static Connection getConnection(String dbname)throws SQLException {
       Connection conn=null;
        try {
            Class.forName("com.mysql.jdbc.Driver"); //加载驱动
           
String ip =我的ip地址;
            conn =(Connection) DriverManager.getConnection(
                    "jdbc:mysql://" + ip + ":3306/" + dbname,
                    "user", "123456");
//            Log.e("user","连接成功!");
       
}catch (SQLException | ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        return conn;//返回Connection型变量conn用于后续连接
   
}
    public static String select(String sql) throws SQLException {//查询数据
       
String a=null;
        Connection conn=getConnection("user");
        try {
            Statement  stmt = conn.createStatement();
            //使用Connection来创建一个Statment对象
           
ResultSet rs =stmt.executeQuery(sql);//
           
//执行查询语句并且保存结果
           
rs.first();//rs行指针指向第一行
           
a=rs.getString(1);
            rs.close();//查询关闭
       
} catch (SQLException e) {
            e.printStackTrace();
        }
        return a;
    }

    public static int insert(String sql) throws SQLException {//增加数据
       
Connection  conn = null;
        conn = getConnection("user");
        //使用DriverManager获取数据库连接
       
Statement stmt = conn.createStatement();
        //使用Connection来创建一个Statment对象
       
return stmt.executeUpdate(sql);
   
}

    public static int updateData(String sql) throws SQLException {//修改数据
       
Connection  conn = null;
        conn = getConnection("user");
        //使用DriverManager获取数据库连接
       
Statement  stmt = conn.createStatement();
        //使用Connection来创建一个Statment对象//修改的sql语句
       
return stmt.executeUpdate(sql);     }

    public static int delete(String sql)throws SQLException{   //删除数据
       
Connection  conn = null;
        conn = getConnection("user");
        //使用DriverManager获取数据库连接
       
Statement  stmt = conn.createStatement();
        //使用Connection来创建一个Statment对象
       
return stmt.executeUpdate(sql);
   
}
}

二、修改密码,退出登录

一、样图

 

 

二、代码

 

package com.example.hotel.Activity.User;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.hotel.Activity.EditpwdActivity;
import com.example.hotel.MainActivity;
import com.example.hotel.R;

public class UserBackFragment extends Fragment {
    private Button Editbutton;
    private Button Outbutton;
    private Button Lookbutton;
    private TextView Nowid;
    private String str;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.userback_fragment,container,false);
        Nowid=(TextView) view.findViewById(R.id.nowid);
        Bundle bundle=getArguments();
        str=null;
        if(bundle!=null)
            str=bundle.getString("nowid");
        Nowid.setText("当前用户id为:"+str);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Editbutton=(Button)getActivity().findViewById(R.id.editbutton);
        Editbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent= new Intent(getActivity(), EditpwdActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("nowid"str);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        Outbutton=(Button) getActivity().findViewById(R.id.outbutton);
        Outbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                intent = new Intent(getActivity(),MainActivity.class);
                startActivity(intent);
            }
        });
        Lookbutton=(Button) getActivity().findViewById(R.id.lookbutton);
        Lookbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                intent = new Intent(getActivity(),HotelActivity.class);
                startActivity(intent);
            }
        });
    }
}

posted @ 2021-05-09 23:00  xjspyx  阅读(27)  评论(0编辑  收藏  举报