android mysql實現登錄注冊_android簡單登陸和注冊功能實現+SQLite數據庫學習

android簡單登陸和注冊功能實現+SQLite數據庫學習

發布時間:2018-07-04 17:23,

瀏覽次數:1027

, 標簽:

android

SQLite

這里我只是建立了一個用簡單的存儲用戶名和密碼的表單

MyDBHelper.java

<>public class MyDBHelper extends SQLiteOpenHelper { public static final

String CREATE_USERDATA="create table userData(" + "id integer primary key

autoincrement," +"nametext," +"password text)"; private Context mContext; public

MyDBHelper(Context context, String name, SQLiteDatabase.CursorFactory

cursorFactory,int version){ super(context,name,cursorFactory,version); mContext=

context; }public void onCreate(SQLiteDatabase db){ db.execSQL(CREATE_USERDATA);

}public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){ //

onCreate(db); } } <>

注冊:

Main2Activity.java <>public class Main2Activity extends AppCompatActivity {

private MyDBHelper dbHelper; @Override protected void onCreate(Bundle

savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main2); dbHelper = new

MyDBHelper(this,"UserStore.db",null,1); } public void logon(View view){

//SQLiteDatabase db=dbHelper.getWritableDatabase(); EditText

editText3=(EditText)findViewById(R.id.editText3); EditText

editText4=(EditText)findViewById(R.id.editText4); String newname

=editText3.getText().toString(); String

password=editText4.getText().toString(); if

(CheckIsDataAlreadyInDBorNot(newname)) {

Toast.makeText(this,"該用戶名已被注冊,注冊失敗",Toast.LENGTH_SHORT).show(); } else { if

(register(newname, password)) { Toast.makeText(this, "插入數據表成功",

Toast.LENGTH_SHORT).show(); } } } //向數據庫插入數據 public boolean register(String

username,String password){ SQLiteDatabase db= dbHelper.getWritableDatabase();

/*String sql = "insert into userData(name,password) value(?,?)"; Object

obj[]={username,password}; db.execSQL(sql,obj);*/ ContentValues values=new

ContentValues(); values.put("name",username); values.put("password",password);

db.insert("userData",null,values); db.close(); //db.execSQL("insert into

userData (name,password) values (?,?)",new String[]{username,password}); return

true; } //檢驗用戶名是否已存在 public boolean CheckIsDataAlreadyInDBorNot(String value){

SQLiteDatabase db=dbHelper.getWritableDatabase(); String Query = "Select * from

userData where name =?"; Cursor cursor = db.rawQuery(Query,new String[] { value

}); if (cursor.getCount()>0){ cursor.close(); return true; } cursor.close();

return false; } } <>

登陸:

MainActivity.java <>public class MainActivity extends AppCompatActivity {

private MyDBHelper dbHelper; private EditText username; private EditText

userpassword; @Overrideprotected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState); //

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.activity_main); dbHelper= new MyDBHelper(this

,"UserStore.db",null,1); } //點擊注冊按鈕進入注冊頁面 public void logonClicked(View view){

Intent intent= new Intent(MainActivity.this,Main2Activity.class);

startActivity(intent); }//點擊登錄按鈕 public void loginClicked(View view) { username=

(EditText)findViewById(R.id.editText2); userpassword=

(EditText)findViewById(R.id.editText); String userName=

username.getText().toString(); String passWord=

userpassword.getText().toString();if (login(userName,passWord)) {

Toast.makeText(MainActivity.this, "登陸成功(ZY,111)", Toast.LENGTH_SHORT).show(); }

else { Toast.makeText(MainActivity.this, "登陸失敗", Toast.LENGTH_SHORT).show(); } }

//驗證登錄 public boolean login(String username,String password) { SQLiteDatabase db

= dbHelper.getWritableDatabase(); String sql = "select * from userData where

name=? and password=?"; Cursor cursor = db.rawQuery(sql, new String[]

{username, password});if (cursor.moveToFirst()) { cursor.close(); return true; }

return false; } <>

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/530288.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/530288.shtml
英文地址,請注明出處:http://en.pswp.cn/news/530288.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

java web 來源頁_Java:Java Web--分頁效果

先來看一看分頁的實現原理萬能公式.jpg項目目錄.PNG首先,新建Java Web項目一. 梳理業務邏輯重定向到URL(跳轉到StudentViewAction頁面)//index.jsp頁面1.從頁面接收可變的值2.接收值有問題時,初始化為13.如果沒有問題,把String類型接收值強轉成Integer4.實例DAO方法,調用findSt…

java 瀏覽器 安全_安全策略-IE瀏覽器防黑十大秘籍

1.管理好Cookie在IE6.0中&#xff0c;打開“工具”→“Internet選項”→“隱私”對話框&#xff0c;這里設定了“阻止所有Cookie”、“高”、“中高”、“中”、“低”、“接受所有Cookie”六個級別&#xff0c;你只要拖動滑塊就可以方便地進行設定&#xff0c;而點擊下方的“編…

什么是java中的枚舉法_enum枚舉javajava,enum枚舉使用詳解+,總結

enum 的全稱為 enumeration&#xff0c; 是 JDK 1.5 中引入的新特性&#xff0c;存放在 java.lang 包中。下面是我在使用 enum 過程中的一些經驗和總結。原始的接口定義常量語法(定義)創建枚舉類型要使用 enum 關鍵字&#xff0c;隱含了所創建的類型都是 java.lang.Enum 類的子…

java 審計 漏洞函數_Java Web代碼審計流程與漏洞函數

常見框架與組合常見框架Struts2SpringMVCSpring Boot框架執行流程View層&#xff1a;視圖層Controller層&#xff1a;表現層Service層&#xff1a;業務層Dom層&#xff1a;持久層常見組合SpringStruts2HibernateSpringSpringMVCMybatisSpring BootMybatis代碼審計方法根據業務功…

java前期_【JAVA】前期環境配置

一、java的環境配置及在eclipse中如何安裝JRE或JDK環境eclipse下載地址&#xff1a;JDK下載地址&#xff1a;1)安裝JDK或JRE注&#xff1a;JDK使用與開發者運用&#xff0c;其中包含了開發環境和運行環境。而JRE只包含了java的運行環境。2)配置設置執行路徑UNiX&#xff1a;在C…

php截取指定字符串之后,php截取字符串(截取指定字符串之間的字符串)

一、PHP截取兩個指定字符后邊的字符$a "123abc#456";$b (strpos($a,""));$c (strpos($a,"#"));echo substr($a,$b1,$c-1);二、常用截取字符串技巧。//構造字符串$str "ABCDEFGHIJKLMNOPQRSTUVWXYZ";echo "原字符串&#xff1a;…

php 日志按天截取,Laravel 日志管理:按日期切割日志

日志存儲Laravel 默認的錯誤文件記錄在一個文件里&#xff0c;隨著時間的推移&#xff0c;此文件將會變得巨大&#xff0c;不方便查閱。我們可以通過修改 config/app.php 配置文件中的 log 選項來配置 Laravel 使用的存儲機制。如果你希望每天產生日志都存放在不同的文件中&…

php xcache 方法,php xcache 解密

NO.2 /index.php?actionmd5webcrack 很強大,需要登陸論壇才能解密,點擊右上角的“register”進行注冊,都是一些簡單的單詞,應該能看懂吧?...() A.PHP B.JSP D.Ajax 4.配置 ...假設$aarray(‘x’,’y’);,則$aarray_pad...xcache 15 四個模塊 Admin Common Member Article 用戶…

ecshop category.php?id=4,categoryall.php

//by 瑯琊源碼 QQ:27392236define(IN_ECS, true);require dirname(__FILE__) . /includes/init.php;if ((DEBUG_MODE & 2) ! 2) {$smarty->caching true;}require ROOT_PATH . /includes/lib_area.php;$area_info get_area_info($province_id);$area_id $area_info[r…

php 獲取key的位置,PHP獲取當前所在目錄位置的方法

本文實例講述了PHP獲取當前所在目錄位置的方法。分享給大家供大家參考。具體分析如下&#xff1a;如果要獲取腳本文件的目錄&#xff0c;要應用函數getcwd()來實現。函數聲明如下&#xff1a;string getcwd ( void ) ;成功執行后返回當前目錄字符串&#xff0c;失敗返回FALSE。…

java8收集器,Java 8中的收集器collectionAndThen()方法

collectingAndThen()Java Collectors類中的方法使Collector適應于執行其他完成轉換。它返回執行下游收集器動作的收集器&#xff0c;然后執行附加的結束步驟。語法如下。static Collector collectingAndThen(Collector downstream, Functionfinisher)在這里&#xff0c;參數T-…

php 精度運算,PHP BC 庫(任意精度數字運算) | 網游世界

留意&#xff1a;備選參數$scale以設置運算精度(保留小數位)。bcscale(設置運算精度)bool bcscale ( int $scale )說明&#xff1a;設置運算精度(保留小數位)&#xff0c;成功返回TRUE否則為FALSE。bcadd(加法運算)string bcadd ( string $left_operand , string $right_operan…

php 不允許外部訪問,[日常] 解決mysql不允許外部訪問

1.在端口已經開放的情況下,ubuntu mysql 3306允許遠程訪問vim /etc/mysql/mysql.conf.d/mysqld.cnf注釋#bind-address 127.0.0.12.給用戶授權允許遠程訪問:grant all privileges on *.* to root"%" identified by "pwd" with grant option;flush privileg…

elementary安裝Java,elementary os怎么樣安裝java

elementary os的安裝方法elementary os 的安裝鏡像文件準備好以后&#xff0c;這里以虛擬機上安裝為例&#xff0c;配置好以后啟動虛擬機進入安裝界面。在初始化安裝界面中先選擇“中文簡體”&#xff0c;再點擊“安裝elementary os”按鈕隨后系統會顯示硬件安裝需求界面&#…

java jni框架,Java JNI 簡明教程(一)——傳智播客JNI筆記(王澤佑)

package cn.itcast;public calss TestNative {public native void sayHello();public static void main(String[] arg){}}2. 用javah.exe生成包含native方法的C/C頭文件javah -jni(默認)javah cn.itcast.TestNative //由類名執行生成C/C頭文件生成的頭文件內容&#xff1a;JNIE…

java自定義變量解析,Thymeleaf內置對象、定義變量、URL參數及標簽自定義屬性

如標題所述&#xff0c;這篇文章主要講述Thymeleaf中的內置對象(list解析、日期格式化、數字格式化等)、定義變量、獲取URL的參數和在頁面標簽中自定義屬性的應用。如果對Thymeleaf的基本使用、maven依賴等不清楚的可以先閱讀我的另一篇文章《Thymeleaf 之 初步使用》。Control…

linux php curl.so,linux中php如何安裝CURL擴展方法

如果php已經在系統編譯好&#xff0c;后來又需要添加新的擴展。一種方式就是重新完全編譯php&#xff0c;另一種方式就是單獨編譯擴展庫&#xff0c;以extension的形式擴展。下面以安裝curl擴展為例&#xff1a;1、下載curl安裝包。(我的php是4.4.4的&#xff0c;下載最新的cur…

matlab線性拉伸函數,采用線性變換對圖像的每一個像素灰度作線性拉伸-Read.PPT

采用線性變換對圖像的每一個像素灰度作線性拉伸-Read第4章 圖像增強(1) 4.1 圖像增強概述 圖像增強(image enhancement)的定義&#xff1a; 在圖像的獲取過程中&#xff0c;由于多種因素的影響&#xff0c;導致圖像質量退化。圖像增強是對退化圖像的某些特征&#xff0c;如邊緣…

php js 循環對象屬性,js 遍歷對象的屬性的代碼_javascript技巧

如&#xff1a;Function.prototype.addMethodfunction(methodName,func){if(!this.prototype[methodName]){this.prototype[methodName]func;//給原型增加方法&#xff0c;此方法會影響到該類型的實例上}return this.prototype;//返回原型&#xff0c;此類型實例可以進行鏈形調…

php連接數據庫navicat,navicat數據庫如何連接php

第一步&#xff0c;打開Navicat&#xff0c;新建數據庫。第二步&#xff0c;在數據庫中新建表。相關推薦&#xff1a;《Navicat for mysql使用圖文教程》第三步&#xff0c;保存表。第四步&#xff0c;表中添加數據。第五步&#xff0c;打開ide&#xff0c;輸入以下php代碼&…