增加数据库类,使用了自带的数据库,可以在侧滑拦显示用户与角色

my
盛洁 5 years ago
parent 4686fa0122
commit f191808d8f

@ -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'

@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hunnu.sj.raise_money">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/img_2"
@ -23,5 +24,4 @@
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
//丁望好帅
</manifest>

@ -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;
}
}

@ -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) {
}
}

@ -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;
}
}

@ -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);
}
}
}

@ -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);
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);

@ -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;
}

@ -49,9 +49,10 @@
app:layout_constraintVertical_bias="0.362" />
<Button
android:id="@+id/registe"
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRegister"
android:text="注册"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
@ -61,6 +62,7 @@
app:layout_constraintVertical_bias="0.765" />
<RadioGroup
android:id="@+id/ratio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
@ -74,6 +76,7 @@
android:id="@+id/ratio1"
android:layout_width="168dp"
android:layout_height="wrap_content"
android:checked="true"
android:text="爱心人士" />
<RadioButton

Loading…
Cancel
Save