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

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:64日期:2022-09-18 17:07:18
目錄一、創建工程二、引入Mysql驅動包三、編寫數據庫和dao以及JDBC相關代碼四、編寫頁面和Activity相關代碼五、運行測試效果

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

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

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

一、創建工程

1、創建一個空白工程

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

2、隨便起一個名稱

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

3、設置網絡連接權限

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

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

1、切換到普通Java工程

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

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

將mysql的驅動包復制到libs當中

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

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

三、編寫數據庫和dao以及JDBC相關代碼

1、在數據庫當中創建表

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

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環保局’, ’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視圖

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

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

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

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

注意鏈接數據庫的地址是: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實體類

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

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

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

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、編寫登錄頁面

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

<?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實現登錄注冊(附源代碼)

2、編寫注冊頁面代碼

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

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

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

<?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實現登錄注冊(附源代碼)

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實現登錄注冊(附源代碼)

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(),'該賬號已經存在,請換一個賬號',Toast.LENGTH_LONG).show(); } if(msg.what == 2) {//startActivity(new Intent(getApplication(),MainActivity.class));Intent intent = new Intent();//將想要傳遞的數據用putExtra封裝在intent中intent.putExtra('a','???);setResult(RESULT_CANCELED,intent);finish(); }} };}五、運行測試效果

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

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

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

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

標簽: Android
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
日韩精品久久久久久| 蜜桃视频第一区免费观看| 极品日韩av| 国产精品三级| 午夜久久av | 黄色成人精品网站| 荡女精品导航| 中文在线а√天堂| 久久久久国产| 一区在线观看| 综合欧美精品| 国产精品中文字幕亚洲欧美| 国产欧美日韩一级| 高清av一区| 精品日韩一区| 欧美日韩尤物久久| 久久这里只有| 久久亚洲精品中文字幕蜜潮电影| 免费日韩av片| 日韩精品福利一区二区三区| 在线免费观看亚洲| 国产精品三p一区二区| 日韩欧美一区二区三区在线视频| 亚洲欧美久久久| 日韩高清不卡一区二区| 精品欧美日韩精品| 美女91精品| 97国产成人高清在线观看| 午夜精品亚洲| 久久av影院| 亚洲一区二区免费看| 视频一区中文字幕精品| 激情国产在线| 亚洲精品极品| 日韩美女国产精品| 麻豆精品少妇| 9国产精品视频| 中文在线а√在线8| 玖玖精品视频| 日韩在线第七页| 久久99性xxx老妇胖精品| 中文一区在线| 日本美女一区| 日韩欧美三区| 婷婷激情久久| 老司机免费视频一区二区| 性欧美xxxx免费岛国不卡电影| 久久精品999| 亚洲二区三区不卡| 人人草在线视频| 欧美日韩中文| 日韩高清电影一区| 日韩视频1区| 美女精品在线观看| 好看的av在线不卡观看| 亚洲国产福利| 成人国产精品一区二区免费麻豆| 亚洲精选久久| 美女91精品| 男人操女人的视频在线观看欧美 | 日韩激情一区| 日本一二区不卡| 精品国产精品久久一区免费式| 麻豆精品视频在线观看| 喷白浆一区二区| 国产偷自视频区视频一区二区| 黄色免费成人| 欧美手机在线| 波多野结衣一区| 黄色av日韩| 免费久久精品视频| 日韩一区二区三区四区五区| 一区二区国产在线| 日韩欧美美女在线观看| 欧美片网站免费| 亚州精品视频| 国产一卡不卡| 开心激情综合| 在线一区视频观看| 久久亚洲二区| 久久中文在线| 亚洲作爱视频| 日本aⅴ亚洲精品中文乱码| 四虎精品永久免费| 精品免费视频| 樱桃成人精品视频在线播放| 在线观看视频免费一区二区三区| 欧美精品aa| 欧美黑人巨大videos精品| 丝袜诱惑一区二区| 99精品视频精品精品视频| 天堂成人免费av电影一区| 国产欧美丝祙| 午夜国产精品视频| 日韩专区视频网站| 欧美国产美女| 亚洲免费专区| 久久久久国产一区二区| 亚洲精品美女91| 肉色欧美久久久久久久免费看| 国产欧美另类| 天堂中文av在线资源库| 91精品福利观看| 亚洲一区二区三区免费在线观看 | 国产乱码午夜在线视频| 日韩欧美一区二区三区免费观看| 国内精品麻豆美女在线播放视频| 激情91久久| 久久婷婷久久| 精品一区二区三区亚洲 | 日韩区欧美区| 亚洲欧美不卡| 日本va欧美va精品发布| 欧美交a欧美精品喷水| 99综合视频| 蜜桃成人av| 老牛国内精品亚洲成av人片| 亚洲影院天堂中文av色| 欧美日韩在线观看首页| 美女视频黄 久久| 国产欧美日韩一级| 日日夜夜免费精品| 亚洲精华国产欧美| 伊人精品一区| 国产 日韩 欧美一区| 国产精品啊v在线| 欧美一区激情| 999久久久精品国产| 欧美日本久久| 久久国产欧美日韩精品| 欧美中文一区| 亚洲综合专区| 亚洲综合五月| 一区二区国产精品| 蜜桃久久精品一区二区| 免播放器亚洲| 日本不卡一区二区| 欧美91在线|欧美| 狠狠久久伊人中文字幕| 精品九九在线| 精品国模一区二区三区| 久久夜夜操妹子| 久久精品国产大片免费观看| 欧美日韩激情| 日韩一二三区在线观看| 午夜在线精品偷拍| 午夜一级久久| 亚州av日韩av| 久久一区精品| 久久国产电影| 日韩中文字幕1| 欧美精品影院| 亚洲最新无码中文字幕久久 | 欧美不卡视频| 午夜一级久久| 久久a爱视频| 欧美午夜精彩| 伊人久久一区| 精品一二三区| 成人小电影网站| 亚洲免费在线| 国产精品激情电影| 久久精品中文| 涩涩涩久久久成人精品| 麻豆国产欧美一区二区三区| av资源新版天堂在线| 香蕉久久久久久久av网站| 国产精品va| 性欧美长视频| 亚洲黄色免费av| 日韩国产在线观看| 欧美成a人国产精品高清乱码在线观看片在线观看久 | 日韩一区二区三免费高清在线观看| 精品国产欧美| 亚洲男女自偷自拍| 福利视频一区| 亚洲欧洲专区| 激情久久久久久久| 国产精品久久久久久模特| 精品三级久久| 电影91久久久| 少妇高潮一区二区三区99| 尤物tv在线精品| 国产成人a视频高清在线观看| 午夜电影一区| 在线人成日本视频| 国产视频一区二区在线播放| 久久高清精品| 久久av网址| 国产日韩三级| 亚洲欧美视频| 日韩电影二区| 日韩在线视频精品| 精品不卡一区| 精品一区二区三区免费看 | av在线日韩| 69堂免费精品视频在线播放| 亚洲一区国产| 制服诱惑一区二区| 亚洲婷婷免费|