parent
4686fa0122
commit
f191808d8f
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue