Android Studio 实现登录注册-源代码 (连接MySql数据库)Android Studio 实现登录注册-源代码 二(Servlet + 连接MySql数据库)[Android Studio 实现登录注册-源代码 三(Servlet + 连接MySql数据库)实现学生信息的查询 (JSON通信)]Android Studio 实现实现学生信息的增删改查 -源代码 四(Servlet + 连接MySql数据库)

一、创建工程
1、创建一个空白工程

2、随便起一个名称

3、设置网络连接权限

- <uses-permission android:name="android.permission.INTERNET" />
复制代码 二、引入Mysql驱动包
1、切换到普通Java工程

2、在libs当中引入MySQL的jar包
将mysql的驱动包复制到libs当中


三、编写数据库和dao以及JDBC相关代码
1、在数据库当中创建表

SQL语句
- /*Navicat MySQL Data TransferSource Server <uses-permission android:name="android.permission.INTERNET" /> : localhost_3306Source Server Version : 50562Source Host <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> : localhost:3306Source Database <uses-permission android:name="android.permission.INTERNET" /> : testTarget Server Type : MYSQLTarget Server Version : 50562File Encoding <uses-permission android:name="android.permission.INTERNET" /> : 65001Date: 2021-05-10 17:28:36*/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 { <uses-permission android:name="android.permission.INTERNET" /> try { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> Class.forName("com.mysql.jdbc.Driver"); <uses-permission android:name="android.permission.INTERNET" /> } catch (ClassNotFoundException e) { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> e.printStackTrace(); <uses-permission android:name="android.permission.INTERNET" /> } } public static Connection getConn() { <uses-permission android:name="android.permission.INTERNET" /> Connection conn = null; <uses-permission android:name="android.permission.INTERNET" /> try { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> conn= DriverManager.getConnection("jdbc:mysql://10.0.2.2:3306/test","root","root"); <uses-permission android:name="android.permission.INTERNET" /> }catch (Exception exception){ <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> exception.printStackTrace(); <uses-permission android:name="android.permission.INTERNET" /> } <uses-permission android:name="android.permission.INTERNET" /> return conn; } public static void close(Connection conn){ <uses-permission android:name="android.permission.INTERNET" /> try { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> conn.close(); <uses-permission android:name="android.permission.INTERNET" /> } catch (SQLException throwables) { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> throwables.printStackTrace(); <uses-permission android:name="android.permission.INTERNET" /> } }}
复制代码 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) { <uses-permission android:name="android.permission.INTERNET" /> this.id = id; <uses-permission android:name="android.permission.INTERNET" /> this.name = name; <uses-permission android:name="android.permission.INTERNET" /> this.username = username; <uses-permission android:name="android.permission.INTERNET" /> this.password = password; <uses-permission android:name="android.permission.INTERNET" /> this.age = age; <uses-permission android:name="android.permission.INTERNET" /> this.phone = phone; } public int getId() { <uses-permission android:name="android.permission.INTERNET" /> return id; } public void setId(int id) { <uses-permission android:name="android.permission.INTERNET" /> this.id = id; } public String getName() { <uses-permission android:name="android.permission.INTERNET" /> return name; } public void setName(String name) { <uses-permission android:name="android.permission.INTERNET" /> this.name = name; } public String getUsername() { <uses-permission android:name="android.permission.INTERNET" /> return username; } public void setUsername(String username) { <uses-permission android:name="android.permission.INTERNET" /> this.username = username; } public String getPassword() { <uses-permission android:name="android.permission.INTERNET" /> return password; } public void setPassword(String password) { <uses-permission android:name="android.permission.INTERNET" /> this.password = password; } public int getAge() { <uses-permission android:name="android.permission.INTERNET" /> return age; } public void setAge(int age) { <uses-permission android:name="android.permission.INTERNET" /> this.age = age; } public String getPhone() { <uses-permission android:name="android.permission.INTERNET" /> return phone; } public void setPhone(String phone) { <uses-permission android:name="android.permission.INTERNET" /> 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){ <uses-permission android:name="android.permission.INTERNET" /> String sql = "select * from users where name = ? and password = ?"; <uses-permission android:name="android.permission.INTERNET" /> Connection con = JDBCUtils.getConn(); <uses-permission android:name="android.permission.INTERNET" /> try { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> PreparedStatement pst=con.prepareStatement(sql); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> pst.setString(1,name); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> pst.setString(2,password); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> if(pst.executeQuery().next()){ <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> return true; <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> } <uses-permission android:name="android.permission.INTERNET" /> } catch (SQLException throwables) { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> throwables.printStackTrace(); <uses-permission android:name="android.permission.INTERNET" /> }finally { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> JDBCUtils.close(con); <uses-permission android:name="android.permission.INTERNET" /> } <uses-permission android:name="android.permission.INTERNET" /> return false; } public boolean register(User user){ <uses-permission android:name="android.permission.INTERNET" /> String sql = "insert into users(name,username,password,age,phone) values (?,?,?,?,?)"; <uses-permission android:name="android.permission.INTERNET" /> Connection con = JDBCUtils.getConn(); <uses-permission android:name="android.permission.INTERNET" /> try { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> PreparedStatement pst=con.prepareStatement(sql); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> pst.setString(1,user.getName()); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> pst.setString(2,user.getUsername()); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> pst.setString(3,user.getPassword()); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> pst.setInt(4,user.getAge()); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> pst.setString(5,user.getPhone()); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> int value = pst.executeUpdate(); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> if(value>0){ <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> return true; <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> } <uses-permission android:name="android.permission.INTERNET" /> } catch (SQLException throwables) { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> throwables.printStackTrace(); <uses-permission android:name="android.permission.INTERNET" /> }finally { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> JDBCUtils.close(con); <uses-permission android:name="android.permission.INTERNET" /> } <uses-permission android:name="android.permission.INTERNET" /> return false; } public User findUser(String name){ <uses-permission android:name="android.permission.INTERNET" /> String sql = "select * from users where name = ?"; <uses-permission android:name="android.permission.INTERNET" /> Connection con = JDBCUtils.getConn(); <uses-permission android:name="android.permission.INTERNET" /> User user = null; <uses-permission android:name="android.permission.INTERNET" /> try { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> PreparedStatement pst=con.prepareStatement(sql); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> pst.setString(1,name); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> ResultSet rs = pst.executeQuery(); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> while (rs.next()){ <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />int id = rs.getInt(0); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />String namedb = rs.getString(1); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />String username = rs.getString(2); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />String passworddb = rs.getString(3); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />int age = rs.getInt(4); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> String phone = rs.getString(5); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />user = new User(id,namedb,username,passworddb,age,phone); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> } <uses-permission android:name="android.permission.INTERNET" /> } catch (SQLException throwables) { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> throwables.printStackTrace(); <uses-permission android:name="android.permission.INTERNET" /> }finally { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> JDBCUtils.close(con); <uses-permission android:name="android.permission.INTERNET" /> } <uses-permission android:name="android.permission.INTERNET" /> return user; }}
复制代码 四、编写页面和Activity相关代码
1、编写登录页面

- <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
复制代码 效果

2、编写注册页面代码



- <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
复制代码 3、完善MainActivity

- package com.example.application01;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.EditText;import android.widget.Toast;import com.example.application01.dao.UserDao;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { <uses-permission android:name="android.permission.INTERNET" /> super.onCreate(savedInstanceState); <uses-permission android:name="android.permission.INTERNET" /> setContentView(R.layout.activity_main); } public void reg(View view){ <uses-permission android:name="android.permission.INTERNET" /> startActivity(new Intent(getApplicationContext(),RegisterActivity.class)); } public void login(View view){ <uses-permission android:name="android.permission.INTERNET" /> EditText EditTextname = (EditText)findViewById(R.id.name); <uses-permission android:name="android.permission.INTERNET" /> EditText EditTextpassword = (EditText)findViewById(R.id.password); <uses-permission android:name="android.permission.INTERNET" /> new Thread(){ <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> @Override <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> public void run() { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> UserDao userDao = new UserDao(); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> boolean aa = userDao.login(EditTextname.getText().toString(),EditTextpassword.getText().toString()); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> int msg = 0; <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> if(aa){ <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />msg = 1; <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> } <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> hand1.sendEmptyMessage(msg); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> } <uses-permission android:name="android.permission.INTERNET" /> }.start(); } final Handler hand1 = new Handler() { <uses-permission android:name="android.permission.INTERNET" /> @Override <uses-permission android:name="android.permission.INTERNET" /> public void handleMessage(Message msg) { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> if(msg.what == 1) <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_LONG).show(); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> } <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> else <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> Toast.makeText(getApplicationContext(),"登录失败",Toast.LENGTH_LONG).show(); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> } <uses-permission android:name="android.permission.INTERNET" /> } };}
复制代码 4、完善RegisterActivity

- package com.example.application01;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.EditText;import android.widget.Toast;import com.example.application01.dao.UserDao;import com.example.application01.entity.User;public class RegisterActivity extends AppCompatActivity { EditText name = null; EditText username = null; EditText password = null; EditText phone = null; EditText age = null; @Override protected void onCreate(Bundle savedInstanceState) { <uses-permission android:name="android.permission.INTERNET" /> super.onCreate(savedInstanceState); <uses-permission android:name="android.permission.INTERNET" /> setContentView(R.layout.activity_register); <uses-permission android:name="android.permission.INTERNET" /> name = findViewById(R.id.name); <uses-permission android:name="android.permission.INTERNET" /> username = findViewById(R.id.username); <uses-permission android:name="android.permission.INTERNET" /> password = findViewById(R.id.password); <uses-permission android:name="android.permission.INTERNET" /> phone = findViewById(R.id.phone); <uses-permission android:name="android.permission.INTERNET" /> age = findViewById(R.id.age); } public void register(View view){ <uses-permission android:name="android.permission.INTERNET" /> String cname = name.getText().toString(); <uses-permission android:name="android.permission.INTERNET" /> String cusername = username.getText().toString(); <uses-permission android:name="android.permission.INTERNET" /> String cpassword = password.getText().toString(); <uses-permission android:name="android.permission.INTERNET" /> System.out.println(phone.getText().toString()); <uses-permission android:name="android.permission.INTERNET" /> String cphone = phone.getText().toString(); <uses-permission android:name="android.permission.INTERNET" /> int cgae = Integer.parseInt(age.getText().toString()); <uses-permission android:name="android.permission.INTERNET" /> if(cname.length() < 2 || cusername.length() < 2 || cpassword.length() < 2 ){ <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> Toast.makeText(getApplicationContext(),"输入信息不符合要求请重新输入",Toast.LENGTH_LONG).show(); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> return; <uses-permission android:name="android.permission.INTERNET" /> } <uses-permission android:name="android.permission.INTERNET" /> User user = new User(); <uses-permission android:name="android.permission.INTERNET" /> user.setName(cname); <uses-permission android:name="android.permission.INTERNET" /> user.setUsername(cusername); <uses-permission android:name="android.permission.INTERNET" /> user.setPassword(cpassword); <uses-permission android:name="android.permission.INTERNET" /> user.setAge(cgae); <uses-permission android:name="android.permission.INTERNET" /> user.setPhone(cphone); <uses-permission android:name="android.permission.INTERNET" /> new Thread(){ <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> @Override <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> public void run() { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> int msg = 0; <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> UserDao userDao = new UserDao(); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> User uu = userDao.findUser(user.getName()); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> if(uu != null){ <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />msg = 1; <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> } <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> boolean flag = userDao.register(user); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> if(flag){ <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />msg = 2; <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> } <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> hand.sendEmptyMessage(msg); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> } <uses-permission android:name="android.permission.INTERNET" /> }.start(); } final Handler hand = new Handler() { <uses-permission android:name="android.permission.INTERNET" /> @Override <uses-permission android:name="android.permission.INTERNET" /> public void handleMessage(Message msg) { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> if(msg.what == 0) <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> Toast.makeText(getApplicationContext(),"注册失败",Toast.LENGTH_LONG).show(); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> } <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> if(msg.what == 1) <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> Toast.makeText(getApplicationContext(),"该账号已经存在,请换一个账号",Toast.LENGTH_LONG).show(); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> } <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> if(msg.what == 2) <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> { <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> //startActivity(new Intent(getApplication(),MainActivity.class)); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> Intent intent = new Intent(); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> //将想要传递的数据用putExtra封装在intent中 <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> intent.putExtra("a","註冊"); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> setResult(RESULT_CANCELED,intent); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> finish(); <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" /> } <uses-permission android:name="android.permission.INTERNET" /> } };}
复制代码 五、运行测试效果



Android Studio 实现登录注册-源代码 (连接MySql数据库)Android Studio 实现登录注册-源代码 二(Servlet + 连接MySql数据库)[Android Studio 实现登录注册-源代码 三(Servlet + 连接MySql数据库)实现学生信息的查询 (JSON通信)]Android Studio 实现实现学生信息的增删改查 -源代码 四(Servlet + 连接MySql数据库)
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |