连接数据库,但是很慢

my
盛洁 5 years ago
parent 4591651369
commit ec37334197

@ -0,0 +1,7 @@
<component name="ProjectDictionaryState">
<dictionary name="盛洁">
<words>
<w>sttmt</w>
</words>
</dictionary>
</component>

@ -32,4 +32,5 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation files('libs/mysql-connector-java-5.1.39-bin.jar')
}

@ -1,9 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<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"

@ -3,9 +3,15 @@ import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.ContactsContract;
public class DatabaseHelper extends SQLiteOpenHelper {
static String name="user.db";
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DatabaseHelper /*extends SQLiteOpenHelper*/ {
/*static String name="user.db";
static int dbVersion=1;
public DatabaseHelper(Context context) {
super(context, name, null, dbVersion);
@ -17,6 +23,33 @@ public class DatabaseHelper extends SQLiteOpenHelper {
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}*/
private static DatabaseHelper db = null;
private static Connection conn = null;
public static DatabaseHelper getDb(){
if(db ==null){
db = new DatabaseHelper();
}
return db;
}
public static Connection getConnection(){
String username = "root";
String password ="";
if(conn==null){
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://172.18.22.77:3306/dogson?useSSL=true&characterEncoding=utf-8";
conn = DriverManager.getConnection(url,username,password);
return conn;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
return conn;
}
}

@ -1,51 +1,72 @@
package hunnu.sj.raise_money.DataBase;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import hunnu.sj.raise_money.User;
public class UserService {
public DatabaseHelper dbHelper;
public UserService(Context context){
dbHelper=new DatabaseHelper(context);
public DatabaseHelper db = null;
public Connection conn = null;
public UserService(){
this.db = DatabaseHelper.getDb();
}
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;
conn = db.getConnection();
try{
Statement sttmt = conn.createStatement();
ResultSet rs1 = sttmt.executeQuery("select * from user where username = '"+username+"'");
boolean flag1 = rs1.next();
if(!flag1){
sttmt.close();
return 0;
}
ResultSet rs2 = sttmt.executeQuery("select * from user where username = '"+username+"'&&password = '"+password+"'");
boolean flag2 = rs2.next();
if(!flag2){
rs2.close();
sttmt.close();
return 1;
}
sttmt.close();
return 2;
}catch (Exception e){
e.printStackTrace();
}
if(cursor2.moveToFirst()==false){
cursor1.close();
cursor2.close();
return 1;
}
cursor1.close();
cursor2.close();
return 2;
return 0;
}
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;
conn = db.getConnection();
try{
String sql = "insert into user(username,password,role) values('"+user.getName()+"','"+user.getPasd()+"','"+user.getRole()+"')";
Statement sttmt = conn.createStatement();
sttmt.executeUpdate(sql);
sttmt.close();
return true;
}catch (Exception e){
e.printStackTrace();
}
return false;
}
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;
//SQLiteDatabase sdb=dbHelper.getReadableDatabase();
String sql="select role from user where username = '" + username+"'";
conn = db.getConnection();
try{
Statement sttmt = conn.createStatement();
ResultSet rs = sttmt.executeQuery(sql);
rs.next();
String role = rs.getString(1);
rs.close();
sttmt.close();
return role;
}catch (SQLException e){
e.printStackTrace();
}
return null;
}
}

@ -1,9 +1,12 @@
package hunnu.sj.raise_money;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.View;
import android.widget.EditText;
@ -15,7 +18,34 @@ import hunnu.sj.raise_money.DataBase.UserService;
public class RegiteActivity extends AppCompatActivity {
EditText user_name;
EditText pass_word;
EditText repass_word;
RadioButton radio1;
RadioButton radio2;
String role;
Handler rHandler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
String txt = (String) msg.obj;
switch (msg.what){
case 0:
user_name.setError(txt);
break;
case 1:
pass_word.setError(txt);
break;
case 2:
repass_word.setText("");
repass_word.setError(txt);
break;
case 3:
user_name.setError(txt);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -23,41 +53,54 @@ public class RegiteActivity extends AppCompatActivity {
}
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);
user_name = findViewById(R.id.user_name);
pass_word = findViewById(R.id.pass_word);
repass_word = findViewById(R.id.repass_word);
role = "默认角色";
radio1 = findViewById(R.id.ratio1);
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);
}
new RegiteThread().start();
}
class RegiteThread extends Thread{
@Override
public void run() {
String username = user_name.getText().toString();
String password = pass_word.getText().toString();
String repassword = repass_word.getText().toString();
UserService uService = new UserService();
Message message = rHandler.obtainMessage();
if(username.trim().equals("")){
message.what = 0;
message.obj = "请输入用户名";
rHandler.sendMessage(message);
}else if(password.trim().equals("")){
message.what = 1;
message.obj = "请输入密码";
rHandler.sendMessage(message);
}
if(!password.equals(repassword)){
message.what = 2;
message.obj = "密码验证错误";
rHandler.sendMessage(message);
}else if(uService.login(username,password)==1){
message.what = 3;
message.obj = "用户已存在";
}else{
User user = new User();
user.setName(username);
user.setPasd(password);
user.setRole(role);
uService.register(user);
Intent intent = new Intent(RegiteActivity.this,SignInActivity.class);
startActivity(intent);
finish();
}
}
}
}

@ -1,23 +1,45 @@
package hunnu.sj.raise_money;
import androidx.annotation.NonNull;
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 java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import hunnu.sj.raise_money.DataBase.UserService;
public class SignInActivity extends AppCompatActivity {
public static final String IS_SIGNIN = "is.user.sign.in";
public static final int SEND = 0;
EditText usnm;
EditText pswd;
User user;
Handler mHandler = new Handler(){
int flag = -1;
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
String txt;
switch (msg.what){
case 0:
flag = 0;
txt = (String) msg.obj;
usnm.setError(txt);
break;
case 1:
flag = 1;
txt = (String) msg.obj;
pswd.setError(txt);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -25,36 +47,46 @@ public class SignInActivity extends AppCompatActivity {
}
public void user_commit(View view){
EditText usnm = findViewById(R.id.user_name);
EditText pswd = findViewById(R.id.pass_word);
String name = usnm.getText().toString();
String pasd = pswd.getText().toString();
UserService uService = new UserService(SignInActivity.this);
if(uService.login(name,pasd)==0){
usnm.setError("用户名错误");
}else if(uService.login(name,pasd)==1){
pswd.setError("密码错误");
}else{
usnm = findViewById(R.id.user_name);
pswd = findViewById(R.id.pass_word);
new LoginThread().start();
}
public void onToRegister(View view){
Intent intent = new Intent(this,RegiteActivity.class);
startActivity(intent);
}
class LoginThread extends Thread{
@Override
public void run() {
int flag = -1;
String name = usnm.getText().toString();
String pasd = pswd.getText().toString();
UserService uService = new UserService();
flag = uService.login(name,pasd);
Message message = mHandler.obtainMessage();
if(flag==0){
message.what = 0;
message.obj = "用户名不存在";
mHandler.sendMessage(message);
}else if(flag==1){
message.what = 1;
message.obj = "密码错误";
mHandler.sendMessage(message);
}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);
Intent intent =new Intent(this,MainActivity.class);
Intent intent =new Intent(SignInActivity.this,MainActivity.class);
intent.putExtras(bundle);
startActivity(intent);
finish();
}
}
}
public void onToRegister(View view){
Intent intent = new Intent(this,RegiteActivity.class);
startActivity(intent);
}
}

Loading…
Cancel
Save