当前位置:   article > 正文

Android Studio 实现登录注册-源代码 (连接MySql数据库)_androidstudio注册界面代码

androidstudio注册界面代码

*/

SET FOREIGN_KEY_CHECKS=0;


– Table structure for student


DROP TABLE IF EXISTS student;

CREATE TABLE student (

sid int(11) NOT NULL AUTO_INCREMENT,

sname varchar(255) NOT NULL,

sage int(11) NOT NULL,

address varchar(255) NOT NULL,

PRIMARY KEY (sid)

) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;


– Records of student


INSERT INTO student VALUES (‘1’, ‘andi’, ‘21’, ‘21212’);

INSERT INTO student VALUES (‘2’, ‘a’, ‘2121’, ‘2121’);


– Table structure for users


DROP TABLE IF EXISTS users;

CREATE TABLE users (

uid int(11) NOT NULL AUTO_INCREMENT,

name varchar(255) NOT NULL,

username varchar(255) NOT NULL,

password varchar(255) NOT NULL,

age int(255) NOT NULL,

phone longblob NOT NULL,

PRIMARY KEY (uid)

) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;


– Records of users


INSERT INTO users VALUES (‘2’, ‘123’, ‘HBV环保局’, ‘123’, ‘33’, 0x3133333333333333333333);

INSERT INTO users VALUES (‘3’, ‘1233’, ‘反复的’, ‘1233’, ‘12’, 0x3132333333333333333333);

INSERT INTO users VALUES (‘4’, ‘1244’, ‘第三代’, ‘1244’, ‘12’, 0x3133333333333333333333);

INSERT INTO users VALUES (‘5’, ‘1255’, ‘SAS’, ‘1255’, ‘33’, 0x3133333333333333333333);

2、在Android Studio当中创建JDBCUtils类

切换会Android视图

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

注意链接数据库的地址是:jdbc:mysql://10.0.2.2:3306/test

package com.example.myapplication.utils;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class JDBCUtils {

static {

try {

Class.forName(“com.mysql.jdbc.Driver”);

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

public static Connection getConn() {

Connection conn = null;

try {

conn= DriverManager.getConnection(“jdbc:mysql://10.0.2.2:3306/test”,“root”,“root”);

}catch (Exception exception){

exception.printStackTrace();

}

return conn;

}

public static void close(Connection conn){

try {

conn.close();

} catch (SQLException throwables) {

throwables.printStackTrace();

}

}

}

3、创建User实体类

在这里插入图片描述

package com.example.myapplication.entity;

public class User {

private int id;

private String name;

private String username;

private String password;

private int age;

private String phone;

public User() {

}

public User(int id, String name, String username, String password, int age, String phone) {

this.id = id;

this.name = name;

this.username = username;

this.password = password;

this.age = age;

this.phone = phone;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public String getPhone() {

return phone;

}

public void setPhone(String phone) {

this.phone = phone;

}

}

4、创建dao层和UserDao

在这里插入图片描述

package com.example.myapplication.dao;

import com.example.myapplication.entity.User;

import com.example.myapplication.utils.JDBCUtils;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

public class UserDao {

public boolean login(String name,String password){

String sql = “select * from users where name = ? and password = ?”;

Connection con = JDBCUtils.getConn();

try {

PreparedStatement pst=con.prepareStatement(sql);

pst.setString(1,name);

pst.setString(2,password);

if(pst.executeQuery().next()){

return true;

}

} catch (SQLException throwables) {

throwables.printStackTrace();

}finally {

JDBCUtils.close(con);

}

return false;

}

public boolean register(User user){

String sql = “insert into users(name,username,password,age,phone) values (?,?,?,?,?)”;

Connection con = JDBCUtils.getConn();

try {

PreparedStatement pst=con.prepareStatement(sql);

pst.setString(1,user.getName());

pst.setString(2,user.getUsername());

pst.setString(3,user.getPassword());

pst.setInt(4,user.getAge());

pst.setString(5,user.getPhone());

int value = pst.executeUpdate();

if(value>0){

return true;

}

} catch (SQLException throwables) {

throwables.printStackTrace();

}finally {

JDBCUtils.close(con);

}

return false;

}

public User findUser(String name){

String sql = “select * from users where name = ?”;

Connection con = JDBCUtils.getConn();

User user = null;

try {

PreparedStatement pst=con.prepareStatement(sql);

pst.setString(1,name);

ResultSet rs = pst.executeQuery();

while (rs.next()){

int id = rs.getInt(0);

String namedb = rs.getString(1);

String username = rs.getString(2);

String passworddb = rs.getString(3);

int age = rs.getInt(4);

String phone = rs.getString(5);

user = new User(id,namedb,username,passworddb,age,phone);

}

} catch (SQLException throwables) {

throwables.printStackTrace();

}finally {

JDBCUtils.close(con);

}

return user;

}

}

四、编写页面和Activity相关代码


1、编写登录页面

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout 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”

tools:context=“.MainActivity”>

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical”

tools:layout_editor_absoluteX=“219dp”

tools:layout_editor_absoluteY=“207dp”

android:padding=“50dp”

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:orientation=“horizontal”>

<TextView

android:id=“@+id/textView”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:textSize=“15sp”

android:text=“账号:” />

<EditText

android:id=“@+id/name”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:ems=“10”

android:inputType=“textPersonName”

android:text=“” />

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:orientation=“horizontal”>

<TextView

android:id=“@+id/textView2”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:textSize=“15sp”

android:text=“密码:”

/>

<EditText

android:id=“@+id/password”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:ems=“10”

android:inputType=“textPersonName”

/>

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:orientation=“horizontal”>

<Button

android:layout_marginTop=“50dp”

android:id=“@+id/button2”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:text=“登录”

android:onClick=“login”

/>

<Button

android:id=“@+id/button3”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:onClick=“reg”

android:text=“注册” />

</androidx.constraintlayout.widget.ConstraintLayout>

效果

在这里插入图片描述

2、编写注册页面代码

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout 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”

tools:context=“.RegisterActivity”>

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical”

tools:layout_editor_absoluteX=“219dp”

tools:layout_editor_absoluteY=“207dp”

android:padding=“50dp”

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:orientation=“horizontal”>

<TextView

android:id=“@+id/textView”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:textSize=“15sp”

android:text=“账号:” />

<EditText

android:id=“@+id/name”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:ems=“10”

android:inputType=“textPersonName”

/>

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:orientation=“horizontal”>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:textSize=“15sp”

android:text=“昵称:” />

<EditText

android:id=“@+id/username”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:ems=“10”

android:inputType=“textPersonName”

/>

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:orientation=“horizontal”>

<TextView

android:id=“@+id/textView2”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:textSize=“15sp”

android:text=“密码:”

/>

<EditText

android:id=“@+id/password”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:ems=“10”

android:inputType=“textPassword”

/>

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:orientation=“horizontal”>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:textSize=“15sp”

android:text=“手机:”

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)
img

最后

既已说到spring cloud alibaba,那对于整个微服务架构,如果想要进一步地向上提升自己,到底应该掌握哪些核心技能呢?

就个人而言,对于整个微服务架构,像RPC、Dubbo、Spring Boot、Spring Cloud Alibaba、Docker、kubernetes、Spring Cloud Netflix、Service Mesh等这些都是最最核心的知识,架构师必经之路!下图,是自绘的微服务架构路线体系大纲,如果有还不知道自己该掌握些啥技术的朋友,可根据小编手绘的大纲进行一个参考。

image

如果觉得图片不够清晰,也可来找小编分享原件的xmind文档!

且除此份微服务体系大纲外,我也有整理与其每个专题核心知识点对应的最强学习笔记:

  • 出神入化——SpringCloudAlibaba.pdf

  • SpringCloud微服务架构笔记(一).pdf

  • SpringCloud微服务架构笔记(二).pdf

  • SpringCloud微服务架构笔记(三).pdf

  • SpringCloud微服务架构笔记(四).pdf

  • Dubbo框架RPC实现原理.pdf

  • Dubbo最新全面深度解读.pdf

  • Spring Boot学习教程.pdf

  • SpringBoo核心宝典.pdf

  • 第一本Docker书-完整版.pdf

  • 使用SpringCloud和Docker实战微服务.pdf

  • K8S(kubernetes)学习指南.pdf

image

另外,如果不知道从何下手开始学习呢,小编这边也有对每个微服务的核心知识点手绘了其对应的知识架构体系大纲,不过全是导出的xmind文件,全部的源文件也都在此!

image

大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)
[外链图片转存中…(img-TxpY1Q2f-1711897530524)]

最后

既已说到spring cloud alibaba,那对于整个微服务架构,如果想要进一步地向上提升自己,到底应该掌握哪些核心技能呢?

就个人而言,对于整个微服务架构,像RPC、Dubbo、Spring Boot、Spring Cloud Alibaba、Docker、kubernetes、Spring Cloud Netflix、Service Mesh等这些都是最最核心的知识,架构师必经之路!下图,是自绘的微服务架构路线体系大纲,如果有还不知道自己该掌握些啥技术的朋友,可根据小编手绘的大纲进行一个参考。

[外链图片转存中…(img-K06o9V9Z-1711897530524)]

如果觉得图片不够清晰,也可来找小编分享原件的xmind文档!

且除此份微服务体系大纲外,我也有整理与其每个专题核心知识点对应的最强学习笔记:

  • 出神入化——SpringCloudAlibaba.pdf

  • SpringCloud微服务架构笔记(一).pdf

  • SpringCloud微服务架构笔记(二).pdf

  • SpringCloud微服务架构笔记(三).pdf

  • SpringCloud微服务架构笔记(四).pdf

  • Dubbo框架RPC实现原理.pdf

  • Dubbo最新全面深度解读.pdf

  • Spring Boot学习教程.pdf

  • SpringBoo核心宝典.pdf

  • 第一本Docker书-完整版.pdf

  • 使用SpringCloud和Docker实战微服务.pdf

  • K8S(kubernetes)学习指南.pdf

[外链图片转存中…(img-O6gbvH2P-1711897530524)]

另外,如果不知道从何下手开始学习呢,小编这边也有对每个微服务的核心知识点手绘了其对应的知识架构体系大纲,不过全是导出的xmind文件,全部的源文件也都在此!

[外链图片转存中…(img-EHtG9AZC-1711897530525)]

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/598677
推荐阅读
相关标签
  

闽ICP备14008679号