package Schema;

import android.content.DialogInterface;
import android.gesture.Prediction;

import androidx.appcompat.app.AlertDialog;

import com.example.cmknowledgegraph.LoginActivity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class OperaSchema {

    /**
     * 连接数据库，进行登录，参数是手机号（string），密码（string）
     * 用户表（UserInfo）,手机号，用户名，密码
     * 登陆成功返回true，登陆失败返回false
     */
    public static boolean longin(String tel,String password){
        Connection conn=InitSchema.getConnect();
        ResultSet rst;
        PreparedStatement pstmt;
        try {
            pstmt=conn.prepareStatement("select paw\n" +
                                            "from UserInfo\n" +
                                            "where tel='"+tel+"'");
            rst=pstmt.executeQuery();
            String paw=rst.getString("paw");
            if(password.equals(paw)){
                return true;
            }
            else return false;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 注册方法，参数是手机号，昵称，密码
     * @return 注册成功返回true，失败返回false
     */
    public static boolean register(String tel,String nickname,String password){

        Connection conn=InitSchema.getConnect();
        PreparedStatement pstmt;
        ResultSet rst;
        try {
            pstmt=conn.prepareStatement("select paw\n" +
                                            "from UserInfo\n" +
                                            "where tel='"+tel+"'");
            rst=pstmt.executeQuery();
            if(rst==null){//注册成功
                //把用户信息填入表中
                pstmt=conn.prepareStatement("insert into UserInfo(tel,nickname,paw) values(?,?,?)");
                pstmt.setString(1,tel);
                pstmt.setString(2,nickname);
                pstmt.setString(3,password);
                pstmt.executeUpdate();
                return  true;
            }else return false;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
