3. Mysql开启远程连接
参考这篇:
https://blog.csdn.net/weixin_44013967/article/details/105312455?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ETopBlog-1.topblog&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ETopBlog-1.topblog&utm_relevant_index=1https://blog.csdn.net/weixin_44013967/article/details/105312455?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2~default~CTRLIST~TopBlog-1.topblog&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2~default~CTRLIST~TopBlog-1.topblog&utm_relevant_index=1
4.在数据库创建表添加数据(这里比较简单的添加) 二、编写Android Studio的java类和xml布局文件 1.在Android Studio当中创建JDBCUtils类package com.example.login_test;
import android.util.Log;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCUtils {
private static String driver = "com.mysql.jdbc.Driver";// MySql驱动
//xxx.xxx.xxx.xxx为我电脑的ip,这里要用自己电脑的ip,login为我电脑的数据库名,也要换。
private static String url = "jdbc:mysql://xxx.xxx.xxx.xxx:3306/login?serverTimezone=UTC";
private static String user = "root";// 用户名
private static String password = "abc123";// 密码
public static Connection getConn() {
//开启连接数据库
Log.d("test", "开启连接数据库。。。。。");
Connection connection = null;
try{
Class.forName(driver);// 动态加载类
connection = DriverManager.getConnection(url, user, password); // 尝试建立到给定数据库URL的连接
}catch (Exception e){
e.printStackTrace();
}
return connection;
}
public static void close(Connection conn){//关闭数据库连接
Log.d("test", "关闭数据库连接。。。。");
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.创建User类(这里我写的比较简单,大家有要求可以添加)
package com.example.login_test;
public class User {//用户类
String user_name;
String user_password;
public User() {
}
public User(String user_name, String password) {
this.user_name = user_name;
this.user_password = password;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getPassword() {
return user_password;
}
public void setPassword(String user_password) {
this.user_password = user_password;
}
}
3.创建dao层和UserDao
package com.example.login_test;
import android.util.Log;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class UserDao {
public boolean login(String username, String password) {//判断是否登录成功
Log.d("test", "开始成功。。。。。");
String sql = "select * from user where username = ? and password = ?";//sql语句
Connection con = JDBCUtils.getConn();//连接数据库
try {
PreparedStatement pst = con.prepareStatement(sql);//调用数据库连接对象con的方法prepareStatement获取SQL语句的预编译对
pst.setString(1, username);//调用pst的方法setXXX设置?占位
pst.setString(2, password);
if (pst.executeQuery().next()) {//查数据用executeQuery() 返回的是一个集合,如果有数据就返回true
Log.d("test", "登录成功");
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(con);//关闭数据库
}
Log.d("test", "登录失败");
return false;
}
public boolean register(User user) {//注册用户密码
Log.d("test", "开始注册。。。。。");
String sql = "insert into user(username,password) values (?,?)";//sql语句
Connection con = JDBCUtils.getConn();//连接数据库
try {
PreparedStatement pst = con.prepareStatement(sql);//调用数据库连接对象con的方法prepareStatement获取SQL语句的预编译对
pst.setString(1, user.getUser_name());//调用pst的方法setXXX设置?占位
pst.setString(2, user.getPassword());
int value = pst.executeUpdate();//executeUpdate 的返回值是一个整数,指示受影响的行数(即更新计数)
if (value > 0) {
Log.d("test", "register: 注册成功");
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(con);
}
Log.d("test", "register: 注册失败");
return false;
}
public User findUser(String username) {//查询用户名
String sql = "select * from user where username = ?";
Connection con = JDBCUtils.getConn();//连接数据库
User user = null;
try {
PreparedStatement pst = con.prepareStatement(sql);//调用数据库连接对象con的方法prepareStatement获取SQL语句的预编译对
pst.setString(1, username); // 调用pst的方法setXXX设置?占位
ResultSet rs = pst.executeQuery();//查数据用executeQuery() 返回的是一个集合,如果有数据就返回true
while (rs.next()) {//.next()表示 指针先下一行,如果有数据就返回true
String name = rs.getString(1);//获取查询到的账号密码
String password = rs.getString(2);
user = new User(name, password);//传给user对象
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(con);
}
return user;//传入成功返回账号密码,失败则为null
}
public int update(String name,String password) {
String sql = "update user set password=? where username=?";
Connection con = JDBCUtils.getConn();//连接数据库
int i = 0;
Log.d("test", "开始修改。。。。。");
try {
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(2, name);//调用pst的方法setXXX设置?占位
pst.setString(1, password);
i = pst.executeUpdate();
System.out.println("resutl: " + i);
pst.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
public int updateUserData(User user){
Log.d("test"," 开始修改。。。。。");
int result=-1;
//获取链接数据库对象
Connection con= JDBCUtils.getConn();
//MySQL 语句
String sql="update user set password=? where username=?";
try {
boolean closed=con.isClosed();
if(con!=null&&(!closed)){
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1,user.getPassword());
//第一个参数state 一定要和上面SQL语句字段顺序一致
pst.setString(2, user.getUser_name());
//第二个参数 phone 一定要和上面SQL语句字段顺序一致
result=pst.executeUpdate();
//返回1 执行成功
Log.d("test"," 修改成功。。。。。");
Log.d("test","result: "+result);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(con);
}
return result;
}
}
三、编写页面和Activity相关代码(这里写的比较粗糙,有些功能不全,欢迎各位大神来指导交流)
图片资源:
1.登录页面 activity_login.xml
2.注册页面 activity_add_.xml
3.验证码 CodeUtils
package com.example.login_test;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import java.util.Random;
public class CodeUtils {//验证码工具类
private static final char[] CHARS = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
// 绘图准备
private static CodeUtils mCodeUtils;
private int mPaddingLeft, mPaddingTop;
private StringBuilder mBuilder = new StringBuilder();
private Random mRandom = new Random();
private static final int DEFAULT_CODE_LENGTH = 6;//验证码的长度 这里6位,可以简单点,简单一点
private static final int DEFAULT_FONT_SIZE = 60;//字体大小
private static final int DEFAULT_LINE_NUMBER = 3;//多少条干扰线
private static final int BASE_PADDING_LEFT = 20; //左边距
private static final int RANGE_PADDING_LEFT = 30;//左边距范围值
private static final int BASE_PADDING_TOP = 70;//上边距
private static final int RANGE_PADDING_TOP = 15;//上边距范围值
private static final int DEFAULT_WIDTH = 300;//默认宽度.图片的总宽
private static final int DEFAULT_HEIGHT = 100;//默认高度.图片的总高
private static final int DEFAULT_COLOR = 0xDF;//默认背景颜色值
private String code;
public static CodeUtils getInstance() {
if (mCodeUtils == null) {
mCodeUtils = new CodeUtils();
}
return mCodeUtils;
}
public Bitmap createBitmap() {
mPaddingLeft = 0;
mPaddingTop = 0;
Bitmap bitmap = Bitmap.createBitmap(DEFAULT_WIDTH, DEFAULT_HEIGHT, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
code = createCode();
canvas.drawColor(Color.rgb(DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_COLOR));
Paint paint = new Paint();
paint.setTextSize(DEFAULT_FONT_SIZE);
for (int i = 0; i < code.length(); i++) {
randomTextStyle(paint);
randomPadding();
canvas.drawText(code.charAt(i) + "", mPaddingLeft, mPaddingTop, paint);
}
for (int i = 0; i < DEFAULT_LINE_NUMBER; i++) {
drawLine(canvas, paint);
}
// 这段代码,由于API28版本太高不能使用,所以需要降版本,否则报错
canvas.save();
canvas.restore();
return bitmap;
}
public String getCode() {
return code;
}
public String createCode() {
mBuilder.delete(0, mBuilder.length());
for (int i = 0; i < DEFAULT_CODE_LENGTH; i++) {
mBuilder.append(CHARS[mRandom.nextInt(CHARS.length)]);
}
return mBuilder.toString();
}
private void drawLine(Canvas canvas, Paint paint) {
int color = randomColor();
int startX = mRandom.nextInt(DEFAULT_WIDTH);
int startY = mRandom.nextInt(DEFAULT_HEIGHT);
int stopX = mRandom.nextInt(DEFAULT_WIDTH);
int stopY = mRandom.nextInt(DEFAULT_HEIGHT);
paint.setStrokeWidth(1);
paint.setColor(color);
canvas.drawLine(startX, startY, stopX, stopY, paint);
}
private int randomColor() {
mBuilder.delete(0, mBuilder.length());
String haxString;
for (int i = 0; i < 3; i++) {
haxString = Integer.toHexString(mRandom.nextInt(0xFF));
if (haxString.length() == 1) {
haxString = "0" + haxString;
}
mBuilder.append(haxString);
}
return Color.parseColor("#" + mBuilder.toString());
}
private void randomTextStyle(Paint paint) {
int color = randomColor();
paint.setColor(color);
paint.setFakeBoldText(mRandom.nextBoolean());
float skewX = mRandom.nextInt(11) / 10;
skewX = mRandom.nextBoolean() ? skewX : -skewX;
paint.setTextSkewX(skewX);
}
private void randomPadding() {
mPaddingLeft += BASE_PADDING_LEFT + mRandom.nextInt(RANGE_PADDING_LEFT);
mPaddingTop = BASE_PADDING_TOP + mRandom.nextInt(RANGE_PADDING_TOP);
}
}
4.完善登录功能 Login_Activity
package com.example.login_test;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Login_Activity extends AppCompatActivity{//登录页面
private EditText editTextname;
private EditText editTextpassword;
User user;
User uu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
getSupportActionBar().hide();
intUI();//初始化页面
}
private void intUI() {
editTextname = (EditText)findViewById(R.id.user_name);
editTextpassword = (EditText)findViewById(R.id.user_passw);
}
public void login(View view) {
final String user_name=editTextname.getText().toString();//获取账号
String user_password=editTextpassword.getText().toString();//获取密码
user=new User();
user.setUser_name(user_name);//把数据传给user对象
Log.d("”test", "add_name:"+user_name);
if(TextUtils.isEmpty(user_name)){//判账号是否为空
Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
return;
}else if(TextUtils.isEmpty(user_password)){//判密码是否为空
Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
return;
}
//必须以线程形式访问,否则报错,我也是萌新不懂,照做就对了
new Thread(new Runnable() {
@Override
public void run() {
UserDao userDao = new UserDao();
uu = userDao.findUser(user.getUser_name());
Log.d("test", "uu:"+uu);
boolean aa = userDao.login(editTextname.getText().toString(),editTextpassword.getText().toString());
int msg = 0;
if(aa){
msg = 1;
}else{
msg = 2;
}
hand1.sendEmptyMessage(msg);
}
}).start();
}
final Handler hand1 = new Handler(){
public void handleMessage(Message msg) {
if(uu==null){
Toast.makeText(Login_Activity.this, "未找到用户,请注册", Toast.LENGTH_SHORT).show();
return;
}else {
if (msg.what == 1) {
Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Login_Activity.this,Login_after_Activity.class);
startActivity(intent);
} else if (msg.what == 2) {
Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_LONG).show();
}
}
}
};
public void add(View view) {
Intent intent = new Intent(Login_Activity.this,Add_Activity.class);
startActivity(intent);
finish();
}
public void amend(View view) {
Intent intent = new Intent(Login_Activity.this,Change_Activity.class);
startActivity(intent);
finish();
}
}
5.完善注册功能 Add_Activity
package com.example.login_test;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class Add_Activity extends AppCompatActivity {//注册页面
private EditText add_password1;
private EditText add_password2;
private EditText add_name;
private String realCode;
private EditText ed_yzm;
private ImageView iv_yzm;
String etCode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_);
intUI();//初始化
getSupportActionBar().hide();//隐藏标题栏
}
private void intUI() {
add_name = findViewById(R.id.add_name);
add_password1 = findViewById(R.id.add_password1);
add_password2 = findViewById(R.id.add_password2);
iv_yzm = findViewById(R.id.img_yzm);
ed_yzm = findViewById(R.id.ed_yzm);
iv_yzm.setImageBitmap(CodeUtils.getInstance().createBitmap());
realCode = CodeUtils.getInstance().getCode().toLowerCase();
}
public void cut(View view) {//验证码
iv_yzm.setImageBitmap(CodeUtils.getInstance().createBitmap());
realCode = CodeUtils.getInstance().getCode().toLowerCase();
}
public void add(View view) {//注册按钮
Log.d("”test", "add: 开始注册");
String user_name=add_name.getText().toString();
String user_password1=add_password1.getText().toString();
String user_password2=add_password2.getText().toString();
Log.d("”test", "add_name:"+user_name);
Log.d("”test", "add_password1:"+user_password1);
Log.d("”test", "add_password2:"+user_password2);
final User user = new User();//创建对象
user.setUser_name(user_name);//传值
user.setPassword(user_password1);//z
if(user_name.length() <10 ){
Toast.makeText(getApplicationContext(),"账户长度不符合要求<至少10位数>",Toast.LENGTH_LONG).show();
return;
}
if(user_password1.length()<6 ){
Toast.makeText(getApplicationContext(),"密码长度不符合要求<至少6个字符/数字>",Toast.LENGTH_LONG).show();
return;
}
if(user_password2==null){
Toast.makeText(getApplicationContext(),"请再次输入密码",Toast.LENGTH_LONG).show();
return;
}
if(user_password1.equals(user_password2)){
}else {
Toast.makeText(getApplicationContext(),"密码不一致,请重新输入",Toast.LENGTH_LONG).show();
return;
}
etCode=ed_yzm.getText().toString().toLowerCase();//获取验证码
if(etCode.equals(realCode)){
}else{
Toast.makeText(Add_Activity.this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
return;
}
new Thread(new Runnable() {
@Override
public void run() {
int msg = 0;
UserDao userDao = new UserDao();
User uu = userDao.findUser(user.getUser_name());//查询用户名
Log.d("test", "uu:"+uu);
if(uu != null){
msg = 1;
}else{
boolean flag = userDao.register(user);
Log.d("test", "user"+user);
if(flag){
msg = 2;
}
}
Log.d("test",etCode);
hand.sendEmptyMessage(msg);
}
}).start();
}
final Handler hand = new Handler(){
public void handleMessage(Message msg) {
if(msg.what == 0){
Toast.makeText(Add_Activity.this, "注册失败", Toast.LENGTH_SHORT).show();
}
if(msg.what == 1)
{
Toast.makeText(Add_Activity.this, "该账号已经存在,请换一个账号", Toast.LENGTH_SHORT).show();
}
if(msg.what == 2) {
Toast.makeText(Add_Activity.this, "注册成功", Toast.LENGTH_SHORT).show();
Log.d("”test", "add: 用户注册成功");
Intent intent = new Intent(Add_Activity.this,Login_Activity.class);
intent.putExtra("a","注冊");
startActivity(intent);
finish();
}
}
};
public void back(View view) {
Intent intent=new Intent(Add_Activity.this,Login_Activity.class);
startActivity(intent);
}
}
6.修改密码页面 Change_Activity
package com.example.login_test;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Change_Activity extends AppCompatActivity {
private EditText change_password2;
private EditText change_password1;
private EditText change_password;
private EditText change_name;
private Button btn_change;
User uu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change);
getSupportActionBar().hide();
intUI();
}
private void intUI() {
change_name = findViewById(R.id.change_name);
change_password = findViewById(R.id.change_password);
change_password1 = findViewById(R.id.change_password1);
change_password2 = findViewById(R.id.change_password2);
btn_change = findViewById(R.id.btn_change);
}
public void change(View view) {
final String Change_name=change_name.getText().toString();
final String Change_password=change_password.getText().toString();
final String Change_password1=change_password1.getText().toString();
final String Change_password2=change_password2.getText().toString();
if(Change_name.length()<6){
Toast.makeText(this, "账号长度不够。。。", Toast.LENGTH_SHORT).show();
return;
}
if(Change_password.length()<=0||Change_password1.length()<=0){
Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
if(Change_password1==null){
Toast.makeText(this, "新密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
if(!(Change_password1.equals(Change_password2))){
Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
return;
}
uu=new User();
uu.setUser_name(Change_name);
uu.setUser_name(Change_password);
new Thread(new Runnable() {
@Override
public void run() {
User user = new User(Change_name,Change_password2);
UserDao userDao = new UserDao();
uu = userDao.findUser(user.getUser_name());
Log.d("test", "uu:"+uu);
boolean aa = userDao.login(Change_name,Change_password2);
userDao.updateUserData(user);
int msg = 0;
if(aa){
msg = 1;
}else{
msg = 2;
}
hand1.sendEmptyMessage(msg);
}
}).start();
}
final Handler hand1 = new Handler(){
public void handleMessage(Message msg) {
if(uu==null){
Toast.makeText(Change_Activity.this, "未找到用户,请注册", Toast.LENGTH_SHORT).show();
return;
}else {
if (msg.what == 1) {
Toast.makeText(getApplicationContext(), "修改成功", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Change_Activity.this,Login_Activity.class);
startActivity(intent);
} else if (msg.what == 2) {
Toast.makeText(getApplicationContext(), "原密码错误", Toast.LENGTH_LONG).show();
}
}
}
};
public void back(View view) {
Intent intent = new Intent(Change_Activity.this,Login_Activity.class);
startActivity(intent);
}
}
7.登录成功页面 (这里写的很简单,各位大神自行发挥) 有更好的或者写的不对的地方评论区交流,,欢迎大神来指点。。。。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)