个人作业
今天建民让我们在课堂上实现安卓端的连接数据库,然后实现增删改,我在课堂上三个小时只实现了mysql本地数据库的连接,然后在回到宿舍后,弄到了晚上22点,我实现了增删改
以下是我的代码:pckage com.example.newone;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class good extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_good }
package com.example.newone;
import android.content.Inte
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.clock.db.DatabaseHelper;
import java.util.ArrayList;
public class Login extends AppCompatActivity {
private DatabaseHelper mSQLite;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button btn_login = findViewById(R.id.login);
Button btn_register = findViewById(R.id.register);
EditText ed_name = findViewById(R.id.userName);
EditText ed_password = findViewById(R.id.userpassword);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = ed_name.getText().toString().trim();
String password = ed_password.getText().toString().trim();
ArrayList<User> data = mSQLite.getAllDATA();
boolean flag = false;
for(int i = 0; i < data.size(); i++){
User userdata = data.get(i);
if(name.equals(userdata.getName())&&password.equals(userdata.getPassword())){
flag = true;
break;
}else{
flag = false;
}
}
if(!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(password)){
if(flag){
Intent intent1 = new Intent(Login.this, MainActivity.class);
startActivity(intent1);
finish();
Toast.makeText(Login.this, "登录成功", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(Login.this, "用户名或密码不正确", Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(Login.this, "用户名与密码不能为空", Toast.LENGTH_SHORT).show();
}
}
});
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent2 = new Intent(Login.this, Register.class);
startActivity(intent2);
finish();
}
});
mSQLite = new DatabaseHelper(Login.this);
}
}
package com.example.newone;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
//import com.example.newone.javabean.User;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button login, register, update;
private EditText name, password;
private MYsqliteopenhelper mYsqliteopenhelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mYsqliteopenhelper = new MYsqliteopenhelper(this);
find();
}
private void find() {
login = findViewById(R.id.login);
register = findViewById(R.id.register);
name = findViewById(R.id.edname);
password = findViewById(R.id.edpassword);
update = findViewById(R.id.update2);
login.setOnClickListener(this);
register.setOnClickListener(this);
update.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.login:
String s = name.getText().toString();
String s1 = password.getText().toString();
boolean login = mYsqliteopenhelper.login(s, s1);
if (login) {
Toast.makeText(this, "登录成功!", Toast.LENGTH_SHORT).show();
Intent i = new Intent(this, good.class);
startActivity(i);
} else {
Toast.makeText(this, "登录失败!", Toast.LENGTH_SHORT).show();
}
break;
case R.id.register:
Intent i1 = new Intent(this, com.example.newone.register.class);
startActivity(i1);
break;
case R.id.update2:
Intent i2 =new Intent(this,update_test.class);
startActivity(i2);
}
}
}
package com.example.newone;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
//import com.example.logindemo.javabean.User;
public class MYsqliteopenhelper extends SQLiteOpenHelper {
private static final String DB_NAME="MYsqlite.db";
private static final String create_users="create table users(name varchar(32),password varchar(32))";
public MYsqliteopenhelper(@Nullable Context context) {
super(context, DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(create_users);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public long register(User u){
SQLiteDatabase db = getWritableDatabase();
ContentValues cv =new ContentValues();
cv.put("name",u.getName());
cv.put("password",u.getPassword());
long users = db.insert("users", null, cv);
return users;
}
public boolean login(String name,String password){
SQLiteDatabase db1 = getWritableDatabase();
boolean result =false;
Cursor users = db1.query("users", null, "name like ?", new String[]{name}, null, null, null);
if (users !=null){
while (users.moveToNext()){
String password1 = users.getString(1);
result=password1.equals(password);
return result;
}
}
return false;
}
public int update(String name,String NEWpassword,String password){
SQLiteDatabase db2 = getWritableDatabase();
ContentValues cv1=new ContentValues();
boolean login = login(name, password);
if (login){
cv1.put("name",name);
cv1.put("password",NEWpassword);
}else {
int o=-1;
return o;
}
int i = db2.update("users", cv1, "name= ?", new String[]{name});
return i;
}
}
package com.example.newone;
import static com.example.clock.R.id.userNames;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.clock.db.DatabaseHelper;
import java.util.ArrayList;
public class Register extends AppCompatActivity {
private DatabaseHelper mSQLite;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//找到各个控件
Button btn_ready = findViewById(R.id.reday);
Button btn_back = findViewById(R.id.back);
EditText ed_name = findViewById(R.id.userName);
EditText ed_password = findViewById(R.id.userpassword);
EditText ed_name1 = findViewById(R.id.userNames);
EditText ed_tel= findViewById(R.id.usertel);
EditText ed_class = findViewById(R.id.userclass);
//注册监听事件
btn_ready.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//获取输入的用户名和密码
String name = ed_name.getText().toString().trim();
String password = ed_password.getText().toString().trim();
String name1 =ed_name1.getText().toString().trim();
String tel = ed_tel.getText().toString().trim();
String classs=ed_class.getText().toString().trim();
//获取数据库数据,判断用户名是否已存在
ArrayList<User> data = mSQLite.getAllDATA();
boolean flag = false;
for(int i = 0; i < data.size(); i++){
User userdata = data.get(i);
if(name.equals(userdata.getName())){
flag = true;
break;
}else{
flag = false;
}
}
//判断用户名和密码是否为空
if(!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(password)){
if(!flag){
mSQLite.insert(name, password,name1,tel,classs);
Intent intent1 = new Intent(Register.this, Login.class);
startActivity(intent1);
finish();
Toast.makeText(Register.this, "注册成功", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(Register.this, "用户名已被注册", Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(Register.this, "用户名与密码不能为空", Toast.LENGTH_SHORT).show();
}
}
});
//监听返回按钮
btn_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent2 = new Intent(Register.this, Login.class);
startActivity(intent2);
finish();
}
});
mSQLite = new DatabaseHelper(Register.this);
}
}
package com.example.newone;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class update_test extends AppCompatActivity {
private EditText name,password,repassword;
private Button update;
private MYsqliteopenhelper mYsqliteopenhelper2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_test);
mYsqliteopenhelper2 =new MYsqliteopenhelper(this);
find();
}
private void find() {
name =findViewById(R.id.etname2);
password =findViewById(R.id.edpassword2);
repassword =findViewById(R.id.repassword1);
}
public void xiugai(View view) {
String s = name.getText().toString();
String s1 = password.getText().toString();
String s2 = repassword.getText().toString();
int update = mYsqliteopenhelper2.update(s, s2, s1);
if (update !=-1){
Toast.makeText(this, "密码修改成功!", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "密码修改失败!", Toast.LENGTH_SHORT).show();
}
}
}
package com.example.newone;
public class User {
private String name;
private String password;
public User() {
}
public User(String name, String password) {
this.name = name;
this.password = password;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return password
*/
public String getPassword() {
return password;
}
/**
* 设置
* @param password
*/
public void setPassword(String password) {
this.password = password;
}
public String toString() {
return "User{name = " + name + ", password = " + password + "}";
}
}