parent
4591651369
commit
ec37334197
@ -0,0 +1,7 @@
|
||||
<component name="ProjectDictionaryState">
|
||||
<dictionary name="盛洁">
|
||||
<words>
|
||||
<w>sttmt</w>
|
||||
</words>
|
||||
</dictionary>
|
||||
</component>
|
Binary file not shown.
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in new issue