日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術(shù)文章
文章詳情頁

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

瀏覽:63日期:2022-09-18 17:07:18
目錄一、創(chuàng)建工程二、引入Mysql驅(qū)動包三、編寫數(shù)據(jù)庫和dao以及JDBC相關(guān)代碼四、編寫頁面和Activity相關(guān)代碼五、運行測試效果

本文主要介紹了Android Studio連接MySql實現(xiàn)登錄注冊,分享給大家,具體如下:

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

一、創(chuàng)建工程

1、創(chuàng)建一個空白工程

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

2、隨便起一個名稱

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

3、設(shè)置網(wǎng)絡(luò)連接權(quán)限

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

<uses-permission android:name='android.permission.INTERNET'/>二、引入Mysql驅(qū)動包

1、切換到普通Java工程

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

2、在libs當中引入MySQL的jar包

將mysql的驅(qū)動包復制到libs當中

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

三、編寫數(shù)據(jù)庫和dao以及JDBC相關(guān)代碼

1、在數(shù)據(jù)庫當中創(chuàng)建表

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

SQL語句

/*Navicat MySQL Data TransferSource Server : localhost_3306Source Server Version : 50562Source Host : localhost:3306Source Database : testTarget Server Type : MYSQLTarget Server Version : 50562File Encoding : 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環(huán)保局’, ’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當中創(chuàng)建JDBCUtils類

切換會Android視圖

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

注意鏈接數(shù)據(jù)庫的地址是: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、創(chuàng)建User實體類

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

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、創(chuàng)建dao層和UserDao

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

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相關(guān)代碼

1、編寫登錄頁面

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

<?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'> <LinearLayoutandroid: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'> <TextViewandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_weight='1'android:textSize='15sp'android:text='賬號:' /> <EditTextandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_weight='1'android:ems='10'android:inputType='textPersonName'android:text='' /></LinearLayout><LinearLayout android:layout_width='match_parent' android:layout_height='wrap_content' android:orientation='horizontal'> <TextViewandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_weight='1'android:textSize='15sp'android:text='密碼:'/> <EditTextandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_weight='1'android:ems='10'android:inputType='textPersonName' /></LinearLayout><LinearLayout android:layout_width='match_parent' android:layout_height='wrap_content' android:orientation='horizontal'></LinearLayout><Button android:layout_marginTop='50dp' android: android:layout_width='match_parent' android:layout_height='wrap_content' android:text='登錄' android:onClick='login' /><Button android: android:layout_width='match_parent' android:layout_height='wrap_content' android:onClick='reg' android:text='注冊' /> </LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>

效果

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

2、編寫注冊頁面代碼

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

<?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'> <LinearLayoutandroid: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'> <TextViewandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_weight='1'android:textSize='15sp'android:text='賬號:' /> <EditTextandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_weight='1'android:ems='10'android:inputType='textPersonName'android:text='' /></LinearLayout><LinearLayout android:layout_width='match_parent' android:layout_height='wrap_content' android:orientation='horizontal'> <TextViewandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_weight='1'android:textSize='15sp'android:text='密碼:'/> <EditTextandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_weight='1'android:ems='10'android:inputType='textPersonName'/></LinearLayout><LinearLayout android:layout_width='match_parent' android:layout_height='wrap_content' android:orientation='horizontal'></LinearLayout><Button android:layout_marginTop='50dp' android: android:layout_width='match_parent' android:layout_height='wrap_content' android:text='登錄' android:onClick='login' /><Button android: android:layout_width='match_parent' android:layout_height='wrap_content' android:onClick='reg' android:text='注冊' /> </LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>

3、完善MainActivity

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

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) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); } public void reg(View view){startActivity(new Intent(getApplicationContext(),RegisterActivity.class)); } public void login(View view){EditText EditTextname = (EditText)findViewById(R.id.name);EditText EditTextpassword = (EditText)findViewById(R.id.password);new Thread(){ @Override public void run() {UserDao userDao = new UserDao();boolean aa = userDao.login(EditTextname.getText().toString(),EditTextpassword.getText().toString());int msg = 0;if(aa){ msg = 1;}hand1.sendEmptyMessage(msg); }}.start(); } final Handler hand1 = new Handler() {@Overridepublic void handleMessage(Message msg) { if(msg.what == 1) {Toast.makeText(getApplicationContext(),'登錄成功',Toast.LENGTH_LONG).show(); } else {Toast.makeText(getApplicationContext(),'登錄失敗',Toast.LENGTH_LONG).show(); }} };}

4、完善RegisterActivity

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

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) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_register); name = findViewById(R.id.name); username = findViewById(R.id.username); password = findViewById(R.id.password); phone = findViewById(R.id.phone); age = findViewById(R.id.age); } public void register(View view){String cname = name.getText().toString();String cusername = username.getText().toString();String cpassword = password.getText().toString();System.out.println(phone.getText().toString());String cphone = phone.getText().toString();int cgae = Integer.parseInt(age.getText().toString());if(cname.length() < 2 || cusername.length() < 2 || cpassword.length() < 2 ){ Toast.makeText(getApplicationContext(),'輸入信息不符合要求請重新輸入',Toast.LENGTH_LONG).show(); return;}User user = new User();user.setName(cname);user.setUsername(cusername);user.setPassword(cpassword);user.setAge(cgae);user.setPhone(cphone);new Thread(){ @Override public void run() {int msg = 0;UserDao userDao = new UserDao();User uu = userDao.findUser(user.getName());if(uu != null){ msg = 1;}boolean flag = userDao.register(user);if(flag){ msg = 2;}hand.sendEmptyMessage(msg); }}.start(); } final Handler hand = new Handler() {@Overridepublic void handleMessage(Message msg) { if(msg.what == 0) {Toast.makeText(getApplicationContext(),'注冊失敗',Toast.LENGTH_LONG).show(); } if(msg.what == 1) {Toast.makeText(getApplicationContext(),'該賬號已經(jīng)存在,請換一個賬號',Toast.LENGTH_LONG).show(); } if(msg.what == 2) {//startActivity(new Intent(getApplication(),MainActivity.class));Intent intent = new Intent();//將想要傳遞的數(shù)據(jù)用putExtra封裝在intent中intent.putExtra('a','???);setResult(RESULT_CANCELED,intent);finish(); }} };}五、運行測試效果

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼)

到此這篇關(guān)于Android Studio連接MySql實現(xiàn)登錄注冊(附源代碼) 的文章就介紹到這了,更多相關(guān)Android Studio 登錄注冊內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Android
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
欧美二三四区| 亚洲无线一线二线三线区别av| 欧美欧美黄在线二区| 日本电影久久久| 日欧美一区二区| 欧美aaaaaa午夜精品| 水蜜桃久久夜色精品一区| 成人亚洲一区| 久久香蕉网站| 99国产一区| 清纯唯美亚洲综合一区| 国产精品专区免费| 亚洲精品国产偷自在线观看| 日韩大片免费观看| 91日韩欧美| 国产精东传媒成人av电影| 老牛影视精品| 日韩亚洲一区在线| 女同性一区二区三区人了人一| 日韩视频不卡| 婷婷成人av| 精品日本视频| 99久久精品费精品国产| 久久国内精品自在自线400部| 97成人在线| 久久精品三级| 欧美一区三区| 日韩专区视频网站| 久久av国产紧身裤| 999国产精品永久免费视频app| 不卡在线一区| 国产精品大片免费观看| 久久精品国产99国产| 国产麻豆久久| 亚洲一区二区动漫| 国产亚洲一区| 日韩高清中文字幕一区二区| 在线视频精品| 国产麻豆一区二区三区 | 日韩国产专区| 久热精品在线| 日韩精品免费一区二区在线观看 | 日本精品影院| 亚洲视频电影在线| 国产一区欧美| 综合一区av| 精品久久久久中文字幕小说| 欧洲激情综合| 国产日韩欧美一区二区三区在线观看| 日韩伦理在线一区| 亚洲视频国产| 麻豆国产91在线播放| 99亚洲视频| 国产一区调教| 国产日韩欧美一区在线| 色乱码一区二区三区网站| 91精品91| 免费在线日韩av| 天堂成人免费av电影一区| 精品入口麻豆88视频| 视频一区视频二区在线观看| 九九久久电影| 欧美一区激情| 国产一区视频在线观看免费| 欧美欧美黄在线二区| 亚洲高清二区| 久久伊人国产| 少妇精品久久久一区二区| 精品国产欧美日韩| 日韩亚洲精品在线观看| 日韩在线短视频| 欧美性www| 一本一本久久| 婷婷激情一区| 国产极品一区| 一区二区电影| 91精品国产成人观看| 国产日韩欧美三区| 久久不射中文字幕| 久久青草久久| 欧美日韩亚洲国产精品| 国产一区二区高清| 国产精品久久久久久久久久10秀| 日本va欧美va精品发布| 久久伦理在线| 久久av超碰| 日韩在线网址| 中文精品视频| 国产精品午夜av| 亚洲专区一区| 久久久噜噜噜| www在线观看黄色| 国产精品一国产精品| 亚洲v天堂v手机在线| 午夜电影亚洲| 99久久亚洲精品蜜臀| 国产成人精品一区二区三区免费| 日本少妇精品亚洲第一区| 国产精品日本| 91精品一区国产高清在线gif | 日韩av电影一区| 日韩在线一二三区| 久久久影院免费| 丝袜美腿诱惑一区二区三区 | 国产精品国码视频| 日韩一区精品字幕| 欧美日韩精品免费观看视欧美高清免费大片 | 人人爱人人干婷婷丁香亚洲| 久久国产精品毛片| 欧美午夜精品一区二区三区电影| 色婷婷综合网| 精品久久一区| 免费在线亚洲欧美| 国产精品一区高清| 欧美中文一区| 综合五月婷婷| 老司机久久99久久精品播放免费| 黄色av一区| 欧美日韩少妇| 亚洲激情黄色| 日韩一级网站| 一区福利视频| 婷婷中文字幕一区| 国产综合欧美| 婷婷激情综合| 亚洲视频播放| 亚洲欧美日韩在线观看a三区| 黄色亚洲在线| 在线一区免费| 玖玖精品视频| 亚洲一区欧美| 日本va欧美va瓶| 视频一区视频二区中文字幕| 男人的天堂久久精品| 综合亚洲自拍| 亚洲1区在线| 国产亚洲字幕| 美女国产精品久久久| 狂野欧美性猛交xxxx| 久久中文字幕一区二区三区| 久久精品一区二区三区中文字幕| 久久在线91| 亚洲伦乱视频| 日韩视频一区二区三区在线播放免费观看| re久久精品视频| 美女精品一区| 日韩欧美另类中文字幕| 婷婷精品在线观看| 国产乱人伦精品一区| 国产精品成人3p一区二区三区| 久久婷婷国产| 久久精品影视| 亚洲欧美日韩综合国产aⅴ| 三级欧美韩日大片在线看| 日韩精品视频网| 久久亚洲资源中文字| 中文另类视频| 日韩中文字幕1| 国产精品一区二区av日韩在线| 国产成人免费av一区二区午夜| 国产精品亚洲一区二区三区在线观看| 日韩精品一区二区三区免费观看| 深夜福利视频一区二区| 欧美日韩免费观看一区=区三区| 亚洲视频二区| 美女视频免费精品| 久久精品官网| 日韩一区网站| 精品视频一区二区三区在线观看| 热三久草你在线| 亚洲国产一区二区在线观看 | 日韩av一区二区三区四区| 精品久久视频| 日韩午夜av在线| 国产精品黄色| 国产在线|日韩| 亚洲另类av| 国产成人精品一区二区三区免费| 99国产精品| 麻豆精品av| 国产农村妇女精品一区二区| 国产精品毛片视频| 欧美综合另类| 青青草91久久久久久久久| 免费观看亚洲| 天堂av一区| 亚洲特色特黄| 欧美视频二区| 欧美福利专区| 国产精品成人自拍| 国产综合色产| 国产日韩亚洲欧美精品| 国户精品久久久久久久久久久不卡 | 日本成人中文字幕| av在线最新| 日韩精品视频网站| 日韩av免费| 91成人福利| 精品一区毛片| 国产一区二区三区探花|