studio开发之简单的登陆

使用的工具

studio,mysql的jar包,mysql数据库

项目架构

功能描述:实现用户的登陆验证,运用老式的servlet架构JDBC的数据源没有分层架构读取数据库来判断非空验证登陆

1.新建一个action_min.xml

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

    <EditText
        android:id="@+id/editusername"
        android:hint="请输入用户名"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/edituserpwd"
        android:hint="请输入密码"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btnlogin"
        android:text="登录"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

2.新建一个action_success.xml

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

    <TextView

        android:text="登录成功"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

3.

搭建简单的分层

手写连接数据库的工具来DBConnection

package util;

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


public class DBConnection {

    public static final String driver="com.mysql.jdbc.Driver";
    public static final String username="root";
    public static final String password="root";
    public static final String url = "jdbc:mysql:///test";

       protected Connection con;
       protected PreparedStatement ps;
       protected ResultSet rs;
       

       public Connection getConnection(){
           try {
             Class.forName(driver);
             if(con==null||con.isClosed()){
                 con=DriverManager.getConnection(url,username,password);
             }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
               return con;
       }
       
       

       public void closeAll(){

           try{
               if(rs!=null){
                   rs.close();
               }
               if(ps!=null){
                   ps.close();
               }
               if(con!=null){
                   con.close();
               }
           }catch(Exception ex){
                ex.printStackTrace();
           }
       }

       public int executeUpdate(String sql,Object...objs) throws Exception{
           con=getConnection();
            ps = con.prepareStatement(sql);
            for (int i = 0; i < objs.length; i++) {
                   ps.setObject(i+1, objs[i]);
            }
            int count = ps.executeUpdate(); 
            return count;
       }

       public ResultSet executeSelect(String sql,Object...prams){
           con=getConnection();
           try {
            ps=con.prepareStatement(sql);
            for (int i = 0; i < prams.length; i++) {
                   ps.setObject(i+1, prams[i]);
               }
             rs=ps.executeQuery();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                
            }
           return rs;
       }
}

4.业务逻辑UserMapper

package mapper;


import java.sql.ResultSet;
import java.sql.SQLException;

import util.DBConnection;

/**
 * Created by FLC on 2018/3/9.
 */

public class UserMapper extends DBConnection {
    public int getusercount(String username,String userpwd) throws SQLException {
        int result=0;
        String sql="select count(*) as num from user_admin where username='"+username+"' and userpwd='"+userpwd+"'";
        ResultSet resultSet = executeSelect(sql);
        if(resultSet!=null)
        {
            while(resultSet.next())
            {
                result=resultSet.getInt("num");
            }
        }
        return result;
    }
}

5.更改启动页面并加入按钮的单机事件来获取登录名和密码到数据库中查询用户并返回结果

 

 

 如图所示新建一个MainActivity

package happy.cn.myapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.DialogInterface;
import android.os.Build;
import android.os.StrictMode;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.sql.SQLException;

import mapper.UserMapper;

public class LoginActivity extends AppCompatActivity {

    UserMapper mapper=new UserMapper();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final AlertDialog.Builder dialog = new AlertDialog.Builder(LoginActivity.this);
        super.onCreate(savedInstanceState);
        //启动某一个试图
        setContentView(R.layout.login_main);
        if (Build.VERSION.SDK_INT > 9) {
            //permitAll()禁用一切检测
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        //01.定位到Button按钮
        Button btn=(Button) findViewById(R.id.btnlogin);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //02.拿到页面布局中的文本框中的值
                EditText editname=(EditText) findViewById(R.id.editusername);
                EditText editpwd=(EditText) findViewById(R.id.edituserpwd);
                try {
                    dialog.setTitle("提示信息");
                    int getusercount = mapper.getusercount(editname.getText().toString(), editpwd.getText().toString());
                    if(getusercount>0)
                    {
                        dialog.setMessage("登录成功!");
                        dialog.setPositiveButton("确定",new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                setContentView(R.layout.activity_success);
                            }
                        });
                        dialog.show();
                        System.out.println("登录成功!");
                    }
                    else
                    {
                        dialog.setMessage("用户名或者密码不正确!");
                        dialog.show();
                        System.out.println("用户名或者密码不正确!");
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

 

 6.启动我们的项目

 

你会看到如下界面

 

控制台打印

 

 至此开发完成

 以下是项目的百度云连接地址:

链接:https://pan.baidu.com/s/1MzJfx6lwkq8Ep7u-LUyRDg 密码:4cc4

posted @ 2018-03-09 15:20  快乐的小银龙  阅读(234)  评论(0编辑  收藏  举报