Android studio 自带的 LoginActivity 连接MySQL 实现登录功能

用Android studio自带的 LoginActivity连接MySQL实现登录功能

连接MySQL

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

class DBLogin {
    private static final String driver = "com.mysql.jdbc.Driver";
    private static final String url = "jdbc:mysql://192.168.1.6:3306/libraryDB";
    private static final String user = "phill";
    private static final String pwd = "123456";

    private static Connection conn=null;
    private static int jumper=111;

    static void linkMysql(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("驱动加载成功!");
        }
        catch (Exception e){
            e.printStackTrace();
        }
        try {
                conn=DriverManager.getConnection(url, user, pwd);
                System.out.println("连接数据库成功!");
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }


    static void linkLoginsql(String username, String password) {
        try {

            String logSql = "Select * from WR_user where phoneNum='"+ username+ "'and Upassword='"+ password+ "'";
            PreparedStatement stmt = conn.prepareStatement(logSql);
            ResultSet rs = stmt.executeQuery(logSql);
            
            // 获取跳转判断
            if(rs.next()){
                jumper=233;
                userid=rs.getInt("userid");
                nickname=rs.getString("nickname");
            }else{
                jumper=777;
            }
            System.out.println(jumper);
            rs.close();
            stmt.close();

        }
        catch (Exception e){
            e.printStackTrace();
        }

        //关闭数据库
        if(conn!=null){
            try {

                conn.close();

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    //传递跳转判断
    static int getjumper(){
        return jumper;
    }
    //传递用户ID
    public static int getuserid(){
        return userid;
    }
    //传递用户昵称
    public static String getnickname(){
        return nickname;
    }
}

修改LoginActivity

loginViewModel.getLoginResult().observe(this, new Observer<LoginResult>() {
      @Override
      public void onChanged(@Nullable LoginResult loginResult) {
           if (loginResult == null) {
                return;
           }
           loadingProgressBar.setVisibility(View.GONE);
           System.out.println(loginResult.getError());
           if (loginResult.getError() != null) {
                showLoginFailed(loginResult.getError());
           }
           if (loginResult.getSuccess() != null) {
                //此处添加登录跳转
                Intent intent = new Intent(LoginActivity.this, MainBody.class);
                LoginActivity.this.startActivity(intent);

                updateUiWithUser(loginResult.getSuccess());

           }
           setResult(Activity.RESULT_OK);

           //Complete and destroy login activity once successful
           finish();
      }
});
/*设置点击事件*/
loginButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
           //在线程中调用数据库
           Thread t1 = new Thread(new Runnable() {
               public void run() {

                   DBLogin.linkLoginsql(usernameEditText.getText().toString(),passwordEditText.getText().toString());
               }
           });

           t1.start();
           //在数据库连接完成之前暂停其他活动
           try {
                t1.join();
           } catch (InterruptedException e) {
                e.printStackTrace();
           }
           loadingProgressBar.setVisibility(View.VISIBLE);
           loginViewModel.login(usernameEditText.getText().toString(),passwordEditText.getText().toString());
      }
});

修改LoginViewModel

//更改判断条件
public void login(String username, String password) {
        // can be launched in a separate asynchronous job
        Result<LoggedInUser> result = loginRepository.login(username, password);

        if (DBLogin.getjumper() == 233) {
            LoggedInUser data = ((Result.Success<LoggedInUser>) result).getData();
            loginResult.setValue(new LoginResult(new LoggedInUserView(data.getDisplayName())));

        } else {
            loginResult.setValue(new LoginResult(R.string.login_failed));
        }
    }

修改data文件LoginDataSource

public class LoginDataSource {

    public Result<LoggedInUser> login(String username, String password) {

        try {
            // TODO: handle loggedInUser authentication
            LoggedInUser fakeUser =
                    new LoggedInUser(
                            //修改显示的用户昵称
                            java.util.UUID.randomUUID().toString(),DBLogin.getnickname());
            return new Result.Success<>(fakeUser);
        } catch (Exception e) {
            return new Result.Error(new IOException("Error logging in", e));
        }
    }

    public void logout() {
        // TODO: revoke authentication
    }
}

你可能感兴趣的:(代码记录,mysql,数据库,java,android,studio,android)