diff --git a/app/build.gradle b/app/build.gradle
index 8244057..5e93ce6 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -17,10 +17,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
+
}
dependencies {
- implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index d556750..03888b5 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -2,6 +2,7 @@
+
-//丁望好帅
\ No newline at end of file
diff --git a/app/src/main/java/hunnu/sj/raise_money/DataBase/DataBase.java b/app/src/main/java/hunnu/sj/raise_money/DataBase/DataBase.java
deleted file mode 100644
index 0dcc3a8..0000000
--- a/app/src/main/java/hunnu/sj/raise_money/DataBase/DataBase.java
+++ /dev/null
@@ -1,28 +0,0 @@
-package hunnu.sj.raise_money.DataBase;
-
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.sql.SQLException;
-
-public class DataBase {
- private static String driver = "com.mysql.jdbc.Driver";// MySQL驱动
- private static String url = "jdbc:mysql://localhost:3306/dogson";//MYSQL数据库连接Url
- private static String user = "root";//用户名
- private static String password = "";/* 密码 */
-
- //连接数据库
-
- public static Connection getConn(){
- Connection conn = null;
- try {
- Class.forName(driver);//获取MYSQL驱动
- conn = DriverManager.getConnection(url, user, password);//获取连接
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return conn;
- }
-
-}
diff --git a/app/src/main/java/hunnu/sj/raise_money/DataBase/DatabaseHelper.java b/app/src/main/java/hunnu/sj/raise_money/DataBase/DatabaseHelper.java
new file mode 100644
index 0000000..b3ed2bc
--- /dev/null
+++ b/app/src/main/java/hunnu/sj/raise_money/DataBase/DatabaseHelper.java
@@ -0,0 +1,22 @@
+package hunnu.sj.raise_money.DataBase;
+import android.content.Context;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteDatabase.CursorFactory;
+import android.database.sqlite.SQLiteOpenHelper;
+
+public class DatabaseHelper extends SQLiteOpenHelper {
+ static String name="user.db";
+ static int dbVersion=1;
+ public DatabaseHelper(Context context) {
+ super(context, name, null, dbVersion);
+ }
+
+ public void onCreate(SQLiteDatabase db) {
+ String sql="create table user(id integer primary key autoincrement,username varchar(20),password varchar(20),role varchar(20))";
+ db.execSQL(sql);
+ }
+ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+
+ }
+
+}
diff --git a/app/src/main/java/hunnu/sj/raise_money/DataBase/UserService.java b/app/src/main/java/hunnu/sj/raise_money/DataBase/UserService.java
new file mode 100644
index 0000000..2510d25
--- /dev/null
+++ b/app/src/main/java/hunnu/sj/raise_money/DataBase/UserService.java
@@ -0,0 +1,51 @@
+package hunnu.sj.raise_money.DataBase;
+
+import android.content.Context;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+
+import hunnu.sj.raise_money.User;
+
+public class UserService {
+ public DatabaseHelper dbHelper;
+ public UserService(Context context){
+ dbHelper=new DatabaseHelper(context);
+ }
+
+ public int login(String username,String password){
+ SQLiteDatabase sdb=dbHelper.getReadableDatabase();
+ String sql1 = "select * from user where username = ?";
+ String sql2 = "select * from user where username=? and password=?";
+ Cursor cursor1=sdb.rawQuery(sql1, new String[]{username});
+ Cursor cursor2=sdb.rawQuery(sql2, new String[]{username,password});
+ if(cursor1.moveToFirst()==false){
+ cursor1.close();
+ cursor2.close();
+ return 0;
+ }
+ if(cursor2.moveToFirst()==false){
+ cursor1.close();
+ cursor2.close();
+ return 1;
+ }
+ cursor1.close();
+ cursor2.close();
+ return 2;
+ }
+ public boolean register(User user){
+ SQLiteDatabase sdb=dbHelper.getReadableDatabase();
+ String sql="insert into user(username,password,role) values(?,?,?)";
+ Object obj[]={user.getName(),user.getPasd(),user.getRole()};
+ sdb.execSQL(sql, obj);
+ return true;
+ }
+
+ public String getRole(String username){
+ SQLiteDatabase sdb=dbHelper.getReadableDatabase();
+ String sql="select role from user where username = ?";
+ Cursor cursor=sdb.rawQuery(sql, new String[]{username});
+ cursor.moveToFirst();
+ String role = cursor.getString(cursor.getColumnIndex("role"));
+ return role;
+ }
+}
diff --git a/app/src/main/java/hunnu/sj/raise_money/RegiteActivity.java b/app/src/main/java/hunnu/sj/raise_money/RegiteActivity.java
index a06d94b..6f9871a 100644
--- a/app/src/main/java/hunnu/sj/raise_money/RegiteActivity.java
+++ b/app/src/main/java/hunnu/sj/raise_money/RegiteActivity.java
@@ -2,12 +2,17 @@ package hunnu.sj.raise_money;
import androidx.appcompat.app.AppCompatActivity;
+import android.content.Intent;
import android.os.Bundle;
+import android.provider.MediaStore;
import android.view.View;
+import android.widget.EditText;
+import android.widget.RadioButton;
+import android.widget.RadioGroup;
+import android.widget.Toast;
-import java.sql.Connection;
+import hunnu.sj.raise_money.DataBase.UserService;
-import hunnu.sj.raise_money.DataBase.DataBase;
public class RegiteActivity extends AppCompatActivity {
@@ -17,8 +22,42 @@ public class RegiteActivity extends AppCompatActivity {
setContentView(R.layout.activity_registe);
}
- protected void onRegiste(View view){
- Connection conn = DataBase.getConn();
+ public void onRegister(View view){
+ EditText user_name = findViewById(R.id.user_name);
+ EditText pass_word = findViewById(R.id.pass_word);
+ EditText repass_word = findViewById(R.id.repass_word);
+ String role = "默认角色";
+ RadioButton radio1 = findViewById(R.id.ratio1);
+ RadioButton radio2 = findViewById(R.id.radio2);
+ if(radio1.isChecked()){
+ role = "爱心人士";
+ }else if(radio2.isChecked()){
+ role = "贫困学生";
+ }
+ String username = user_name.getText().toString();
+ String password = pass_word.getText().toString();
+ String repassword = repass_word.getText().toString();
+ UserService uService = new UserService(RegiteActivity.this);
+ if(username.trim().equals("")){
+ user_name.setError("请输入昵称");
+ }else if(password.trim().equals("")){
+ pass_word.setError("请输入密码");
+ }
+ if(!password.equals(repassword)){
+ repass_word.setText("");
+ repass_word.setError("密码验证错误");
+ }else if(uService.login(username,password)==1){
+ user_name.setError("用户已存在");
+ }else{
+ User user = new User();
+ user.setName(username);
+ user.setPasd(password);
+ user.setRole(role);
+ uService.register(user);
+ Intent intent = new Intent(this,SignInActivity.class);
+ startActivity(intent);
+ }
+
}
}
diff --git a/app/src/main/java/hunnu/sj/raise_money/SignInActivity.java b/app/src/main/java/hunnu/sj/raise_money/SignInActivity.java
index 1a613df..11c5046 100644
--- a/app/src/main/java/hunnu/sj/raise_money/SignInActivity.java
+++ b/app/src/main/java/hunnu/sj/raise_money/SignInActivity.java
@@ -14,7 +14,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
-import hunnu.sj.raise_money.DataBase.DataBase;
+import hunnu.sj.raise_money.DataBase.UserService;
public class SignInActivity extends AppCompatActivity {
public static final String IS_SIGNIN = "is.user.sign.in";
@@ -29,47 +29,30 @@ public class SignInActivity extends AppCompatActivity {
EditText pswd = findViewById(R.id.pass_word);
String name = usnm.getText().toString();
String pasd = pswd.getText().toString();
- String sql = "select * from User";
- Connection conn = DataBase.getConn();
- try {
- if(conn!=null){
- PreparedStatement ps = conn.prepareStatement(sql);
- if(ps!=null){
- ResultSet rs = ps.executeQuery();
- if(rs!=null&&rs.next()){
- User user = new User();
- user.setName(rs.getString("username"));
- user.setPasd(rs.getString("password"));
- user.setRole(rs.getString("role"));
- conn.commit();
- conn.close();
- Bundle bundle = new Bundle();
- bundle.putSerializable("user",user);
- Intent intent =new Intent(this,MainActivity.class);
- intent.putExtras(bundle);
- startActivity(intent);
- }
- }
- }
- }catch (SQLException e){
- e.printStackTrace();
- }
- /*if(!name.equals("admin")){
+ UserService uService = new UserService(SignInActivity.this);
+ if(uService.login(name,pasd)==0){
usnm.setError("用户名错误");
- }else if(!pasd.equals("admin")){
+ }else if(uService.login(name,pasd)==1){
pswd.setError("密码错误");
}else{
User user = new User();
user.setName(name);
- user.setPasd(pasd);
+ user.setPasd(pasd);
+ String role = uService.getRole(name);
+ user.setRole(role);
+ //String role = uService.getRole(name);
+ //user.setRole(role);
Bundle bundle = new Bundle();
- /* bundle.putSerializable("user",user);
+ bundle.putSerializable("user",user);
Intent intent =new Intent(this,MainActivity.class);
intent.putExtras(bundle);
- startActivity(intent);*/
+ startActivity(intent);
+ }
+
}
+
public void onToRegister(View view){
Intent intent = new Intent(this,RegiteActivity.class);
startActivity(intent);
diff --git a/app/src/main/java/hunnu/sj/raise_money/User.java b/app/src/main/java/hunnu/sj/raise_money/User.java
index 98733e6..d4493e5 100644
--- a/app/src/main/java/hunnu/sj/raise_money/User.java
+++ b/app/src/main/java/hunnu/sj/raise_money/User.java
@@ -6,6 +6,16 @@ public class User implements Serializable{
private String name;
private String pasd;
private String role;
+ public User(){
+ super();
+ }
+ public User(String name,String pasd,String role){
+ super();
+ this.name = name;
+ this.pasd = pasd;
+ this.role = role;
+ }
+
public void setName(String name) {
this.name = name;
}
diff --git a/app/src/main/res/layout/activity_registe.xml b/app/src/main/res/layout/activity_registe.xml
index 7d0920a..41dc5a6 100644
--- a/app/src/main/res/layout/activity_registe.xml
+++ b/app/src/main/res/layout/activity_registe.xml
@@ -49,9 +49,10 @@
app:layout_constraintVertical_bias="0.362" />