当前位置: 首页 > news >正文

昆明企业自助建站系统/seo网站推广可以自己搞吗

昆明企业自助建站系统,seo网站推广可以自己搞吗,网站的底部导航怎么做,点击图片跳转到网站怎么做链接本文主要介绍了Android Studio连接MySql实现登录注册,分享给大家,具体如下:一、创建工程1、创建一个空白工程2、随便起一个名称3、设置网络连接权限二、引入Mysql驱动包1、切换到普通Java工程2、在libs当中引入MySQL的jar包将mysql的驱动包复…

本文主要介绍了Android Studio连接MySql实现登录注册,分享给大家,具体如下:

06cae3f5e6692c936175fddb9091fc40.png

ab3ed15a56cc65835f3aa1ab33b096f4.png

一、创建工程

1、创建一个空白工程

2359928b59e4c51e2e217f1596170c58.png

2、随便起一个名称

1400961a3b2f0353cea10553a5ffb36e.png

3、设置网络连接权限

757b632851f95cc6144032831e8a6fcb.png

二、引入Mysql驱动包

1、切换到普通Java工程

647ac7cc1ae6461b73a6426e8a959477.png

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

将mysql的驱动包复制到libs当中

154f66eb1f8e058424fe6aac7da935f4.png

59f7c1d0dd58aedabc49fe33d39f759e.png

三、编写数据库和dao以及JDBC相关代码

1、在数据库当中创建表

22553c498ff24d0abe95654926ab0f8d.png

SQL语句

/*

Navicat MySQL Data Transfer

Source Server : localhost_3306

Source Server Version : 50562

Source Host : localhost:3306

Source Database : test

Target Server Type : MYSQL

Target Server Version : 50562

File Encoding : 65001

Date: 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视图

6aad364ebd312a77ed6da374622ca52b.png

5db01c2e02ed49307fc42c9d17156dd2.png

8d79aebfe16aad3a491ed03f1d7bc3b7.png

a09e5973d3b4ec05eee69ac9538ac56b.png

注意链接数据库的地址是: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实体类

0b835caecf058ed8c772a860f7d12c4d.png

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

ccde8b9946abee837217bf3b66cc8e95.png

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、编写登录页面

bda28b71ca6704bef28cb9b20f2fb06f.png

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">

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"

>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:textSize="15sp"

android:text="账号:" />

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="" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:textSize="15sp"

android:text="密码:"

/>

android:id="@+id/password"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:ems="10"

android:inputType="textPersonName"

/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:layout_marginTop="50dp"

android:id="@+id/button2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="登录"

android:onClick="login"

/>

android:id="@+id/button3"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:onClick="reg"

android:text="注册" />

效果

6ba5e8ed081ae62c0905add313286478.png

2、编写注册页面代码

8327b543a6fa3d8c5876595bbe842825.png

3de244521e7c7997e662d1e7862666f4.png

2a779c3f80c712fd5559f0a87a357ab1.png

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">

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"

>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:textSize="15sp"

android:text="账号:" />

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="" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:textSize="15sp"

android:text="密码:"

/>

android:id="@+id/password"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:ems="10"

android:inputType="textPersonName"

/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:layout_marginTop="50dp"

android:id="@+id/button2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="登录"

android:onClick="login"

/>

android:id="@+id/button3"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:onClick="reg"

android:text="注册" />

3、完善MainActivity

07c6a09473409bd6132e4d3f8f583c2a.png

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()

{

@Override

public 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

c360b4a8761d93ee8ae273702f148eb4.png

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()

{

@Override

public 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();

}

}

};

}

五、运行测试效果

14f1beef48d795afcd8ab7e5eeb9faab.png

5eb17408f37a8c5dbdbde21bb2fd1f4d.png

bcb24f932a05778ceb9fbdd990a7cd6a.png

到此这篇关于Android Studio连接MySql实现登录注册(附源代码) 的文章就介绍到这了,更多相关Android Studio 登录注册内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

http://www.jmfq.cn/news/5209975.html

相关文章:

  • 网站的整体规划怎么写/汤阴县seo快速排名有哪家好
  • 小米商城wordpress/毕节地seo
  • 做企业网站需要什么/最全磁力搜索引擎
  • 网站开发先做后台还是前台/百度老年搜索
  • 石家庄哪家网站做的好/2022小说排行榜百度风云榜
  • 网站建设seo网络推广/免费b站推广网站详情
  • tp做网站签到功能/网站备案查询工信部
  • 怎么用ppt做网站/济南做seo排名
  • 匈牙利网站后缀/郑州竞价托管代运营
  • 纯静态做企业网站/中小企业管理培训班
  • 漳州市网站建设公司/女装标题优化关键词
  • 酒店做爰视频网站/东莞网站推广优化网站
  • 宝塔wordpress建站教程/谈谈对seo的理解
  • 网站缓存优化怎么做/百度指数app官方下载
  • 建委 建设局 的官方网站/站长之家网站排行榜
  • 菏泽官方网站/牛奶软文广告营销
  • 私募网站建设/江门seo外包公司
  • 门户网站网站开发/优秀网站设计赏析
  • 天津规划设计公司/qq群怎么优化排名靠前
  • idea15网站开发/百度开发平台
  • js 曲线 网站/品牌策划公司介绍
  • 网站开发用px好还是em好/搜索引擎优化趋势
  • 北京制作公司网站/seo优化的基本流程
  • wordpress做文字站/免费的seo
  • 最简单的网站建设/2022年国际十大新闻
  • 有声阅读网站如何建设/怎样制作网页设计
  • 做环球资源网站有没有效果/百度怎么推广自己的作品
  • 二级栏目网站/晚上必备免费软件大全苹果
  • 支付宝 wordpress 插件/关键词优化的原则
  • 后台网站建设招聘/东莞seo快速排名