@ -4,7 +4,6 @@ import android.content.Context;
import androidx.core.content.ContextCompat ;
import androidx.core.content.ContextCompat ;
import java.io.IOException ;
import java.io.IOException ;
import java.net.MalformedURLException ;
import java.net.URL ;
import java.net.URL ;
import java.net.URLConnection ;
import java.net.URLConnection ;
import java.sql.Connection ;
import java.sql.Connection ;
@ -16,7 +15,6 @@ import java.text.DateFormat;
import java.text.SimpleDateFormat ;
import java.text.SimpleDateFormat ;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.Calendar ;
import java.util.Calendar ;
import java.util.Date ;
import hunnu.sj.raise_money.record.Record ;
import hunnu.sj.raise_money.record.Record ;
import hunnu.sj.raise_money.news.News ;
import hunnu.sj.raise_money.news.News ;
@ -32,10 +30,10 @@ public class UserService {
public int login ( String username , String password ) {
public int login ( String username , String password ) {
conn = db . getConnection ( ) ;
conn = db . getConnection ( ) ;
Statement sttmt = null ;
Statement sttmt ;
try {
try {
sttmt = conn . createStatement ( ) ;
sttmt = conn . createStatement ( ) ;
String sql = "select * from user where username = '"+ username + "'" ;
String sql = "select * from user _info where username = '"+ username + "'" ;
PreparedStatement ps = conn . prepareStatement ( sql ) ;
PreparedStatement ps = conn . prepareStatement ( sql ) ;
ResultSet rs1 = ps . executeQuery ( ) ;
ResultSet rs1 = ps . executeQuery ( ) ;
boolean flag1 = rs1 . next ( ) ;
boolean flag1 = rs1 . next ( ) ;
@ -43,7 +41,7 @@ public class UserService {
sttmt . close ( ) ;
sttmt . close ( ) ;
return 0 ; //当用户名不存在时返回0
return 0 ; //当用户名不存在时返回0
}
}
ResultSet rs2 = sttmt . executeQuery ( "select * from user where username = '"+ username + "'&&password = '" + password + "'" ) ;
ResultSet rs2 = sttmt . executeQuery ( "select * from user _info where username = '"+ username + "'&&password = '" + password + "'" ) ;
boolean flag2 = rs2 . next ( ) ;
boolean flag2 = rs2 . next ( ) ;
if ( ! flag2 ) {
if ( ! flag2 ) {
rs2 . close ( ) ;
rs2 . close ( ) ;
@ -55,13 +53,13 @@ public class UserService {
} catch ( Exception e ) {
} catch ( Exception e ) {
e . printStackTrace ( ) ;
e . printStackTrace ( ) ;
}
}
return 0 ;
return - 1 ;
}
}
public boolean register ( User user ) {
public boolean register ( User user ) {
conn = db . getConnection ( ) ; //将用户的信息上传到数据库, 上传成功返回true, 失败false
conn = db . getConnection ( ) ; //将用户的信息上传到数据库, 上传成功返回true, 失败false
try {
try {
String sql = "insert into user (username,password,role) values('"+ user . get N ame( ) + "','" + user . getPas d( ) + "','" + user . getRol e( ) + "')" ;
String sql = "insert into user _info (username,password,role,phone ) values('"+ user . get Usern ame( ) + "','" + user . getPas swor d( ) + "','" + user . getRol e( ) + "','" + user . getPhon e( ) + "')" ;
Statement sttmt = conn . createStatement ( ) ;
Statement sttmt = conn . createStatement ( ) ;
sttmt . executeUpdate ( sql ) ;
sttmt . executeUpdate ( sql ) ;
sttmt . close ( ) ;
sttmt . close ( ) ;
@ -71,14 +69,15 @@ public class UserService {
}
}
return false ;
return false ;
}
}
public boolean donate ( String username , String studentName , Long id , float money ) {
public boolean donate ( long stu_id , long user_id , float money ) {
conn = db . getConnection ( ) ; //将用户捐款信息上传到数据库, 成功返回true, 失败返回false
conn = db . getConnection ( ) ; //将用户捐款信息上传到数据库, 成功返回true, 失败返回false
try {
try {
String time = getNetTime ( ) ;
String time = getNetTime ( ) ;
// Date date = new Date();sdf.format(date)
// Date date = new Date();sdf.format(date)
//SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateTimeInstance();
//SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateTimeInstance();
String sql = "insert into record(stu_ name,donatemon,username,id,time) values('"+ studentName + "','" + money + "','" + username + "','" + id + "','" + time + "')" ;
String sql = "insert into record(stu_ id,user_id,donatemon,time) values('"+ stu_id + "','" + user_id + "','" + money + "','" + time + "')" ;
String sql1 = "update stu_info set hasdonated=hasdonated+'" + money + "' where id='"+ id+ "'" ;
String sql1 = "update stu_info set hasdonated=hasdonated+'" + money + "' where stu_ id='"+ stu_ id+ "'" ;
//stu_id,stu_name,donatemon,time,username
//stu_id,stu_name,donatemon,time,username
Statement sttmt = conn . createStatement ( ) ;
Statement sttmt = conn . createStatement ( ) ;
sttmt . executeUpdate ( sql ) ;
sttmt . executeUpdate ( sql ) ;
@ -91,17 +90,16 @@ public class UserService {
return false ;
return false ;
}
}
public String getNetTime ( ) throws IOException {
public String getNetTime ( ) throws IOException {
URL url = null ;
URL url ;
url = new URL ( "http://www.baidu.com" ) ;
url = new URL ( "http://www.baidu.com" ) ;
URLConnection baidu = url . openConnection ( ) ; //生成连接对象
URLConnection baidu = url . openConnection ( ) ; //生成连接对象
baidu . connect ( ) ; //发出连接
baidu . connect ( ) ; //发出连接
long time13 = baidu . getDate ( ) ;
long time13 = baidu . getDate ( ) ;
DateFormat formatter = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
DateFormat formatter = new SimpleDateFormat ( "yyyy年MM月dd日 HH:mm:ss" ) ;
Calendar calendar = Calendar . getInstance ( ) ;
Calendar calendar = Calendar . getInstance ( ) ;
calendar . setTimeInMillis ( time13 ) ;
calendar . setTimeInMillis ( time13 ) ;
final String format = formatter . format ( calendar . getTime ( ) ) ;
String format = formatter . format ( calendar . getTime ( ) ) ;
String time = String . valueOf ( format ) ;
return format ;
return time ;
}
}
public boolean longterm ( String name , String phone , String descri , Long ID ) {
public boolean longterm ( String name , String phone , String descri , Long ID ) {
conn = db . getConnection ( ) ; //将长期资助信息上传到数据库, 成功返回true, 失败返回false
conn = db . getConnection ( ) ; //将长期资助信息上传到数据库, 成功返回true, 失败返回false
@ -118,18 +116,23 @@ public class UserService {
}
}
return false ;
return false ;
}
}
public String getRole ( String username ) { //从数据库获取用户的角色
public User getUserInfo ( String username , String password ) { //从数据库获取用户的角色
//SQLiteDatabase sdb=dbHelper.getReadableDatabase();
//SQLiteDatabase sdb=dbHelper.getReadableDatabase();
String sql = "select role from user where username = '" + username + "'" ;
String sql = "select * from user_info where username = '" + username + "'and password = '" + password + "'" ;
User user = new User ( ) ;
conn = db . getConnection ( ) ;
conn = db . getConnection ( ) ;
try {
try {
Statement sttmt = conn . createStatement ( ) ;
Statement sttmt = conn . createStatement ( ) ;
ResultSet rs = sttmt . executeQuery ( sql ) ;
ResultSet rs = sttmt . executeQuery ( sql ) ;
rs . next ( ) ;
rs . next ( ) ;
String role = rs . getString ( 1 ) ;
user . setUser_id ( rs . getLong ( "user_id" ) ) ;
user . setUsername ( rs . getNString ( "username" ) ) ;
user . setPassword ( rs . getNString ( "password" ) ) ;
user . setPhone ( rs . getNString ( "phone" ) ) ;
user . setRole ( rs . getNString ( "role" ) ) ;
rs . close ( ) ;
rs . close ( ) ;
sttmt . close ( ) ;
sttmt . close ( ) ;
return role ;
return user ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
e . printStackTrace ( ) ;
e . printStackTrace ( ) ;
}
}
@ -137,7 +140,7 @@ public class UserService {
}
}
public boolean UploadInfo ( News news ) { //用户上传信息到数据库,服务器
public boolean UploadInfo ( News news ) { //用户上传信息到数据库,服务器
String sql = "insert into stu_info(name,descri) values('" + news . get Titl e( ) + "','" + news . getDes ( ) + "')" ;
String sql = "insert into stu_info(name,descri) values('" + news . get Nam e( ) + "','" + news . getDes cri ( ) + "')" ;
conn = db . getConnection ( ) ;
conn = db . getConnection ( ) ;
try {
try {
Statement sttmt = conn . createStatement ( ) ;
Statement sttmt = conn . createStatement ( ) ;
@ -152,16 +155,16 @@ public class UserService {
public void getAllNews ( Context context , ArrayList < News > list ) {
public void getAllNews ( Context context , ArrayList < News > list ) {
//ArrayList<News> mlist = list;
//ArrayList<News> mlist = list;
String sql = "select * from stu_info ";
String sql = "select * from stu_info where flags = 1 ";
conn = db . getConnection ( ) ;
conn = db . getConnection ( ) ;
try {
try {
Statement sttmt = conn . createStatement ( ) ;
Statement sttmt = conn . createStatement ( ) ;
ResultSet rs = sttmt . executeQuery ( sql ) ;
ResultSet rs = sttmt . executeQuery ( sql ) ;
while ( rs . next ( ) ) {
while ( rs . next ( ) ) {
News news = new News ( ) ;
News news = new News ( ) ;
news . set I d( rs . getLong ( " id") ) ;
news . set Stu_i d( rs . getLong ( " stu_ id") ) ;
news . set Titl e( rs . getNString ( "name" ) ) ;
news . set Nam e( rs . getNString ( "name" ) ) ;
news . setDes ( rs . getNString ( "descri" ) ) ;
news . setDes cri ( rs . getNString ( "descri" ) ) ;
news . setHead_icon ( ContextCompat . getDrawable ( context , R . drawable . ic_launcher_background ) ) ;
news . setHead_icon ( ContextCompat . getDrawable ( context , R . drawable . ic_launcher_background ) ) ;
list . add ( news ) ;
list . add ( news ) ;
}
}
@ -172,15 +175,17 @@ public class UserService {
}
}
}
}
public void getDonateRecords ( ArrayList < Record > recordlist , String username ) {
public void getDonateRecords ( ArrayList < Record > recordlist , long user_id ) {
String sql = "select * from record where username = '" + username + "'" ;
String sql = "select * from record,stu_info where record.stu_id = stu_info.stu_id and record.user_id='" + user_id + "'" ;
conn = db . getConnection ( ) ;
conn = db . getConnection ( ) ;
try {
try {
Statement sttmt = conn . createStatement ( ) ;
Statement sttmt = conn . createStatement ( ) ;
ResultSet rs = sttmt . executeQuery ( sql ) ;
ResultSet rs = sttmt . executeQuery ( sql ) ;
while ( rs . next ( ) ) {
while ( rs . next ( ) ) {
Record record = new Record ( ) ;
Record record = new Record ( ) ;
record . setStu_name ( rs . getNString ( " stu_ name") ) ;
record . setStu_name ( rs . getNString ( " name") ) ;
record . setTime ( rs . getNString ( "time" ) ) ;
record . setTime ( rs . getNString ( "time" ) ) ;
record . setDonatemon ( rs . getFloat ( "donatemon" ) ) ;
record . setDonatemon ( rs . getFloat ( "donatemon" ) ) ;
recordlist . add ( record ) ;
recordlist . add ( record ) ;
@ -193,14 +198,14 @@ public class UserService {
}
}
public void getDonatedRecords ( ArrayList < Record > recordlist , long id ) {
public void getDonatedRecords ( ArrayList < Record > recordlist , long id ) {
String sql = "select * from record where id= '"+ id + "'" ;
String sql = "select * from record ,user_info where record.user_ id = user_info.user_id and record.stu_id = '"+ id + "'" ;
conn = db . getConnection ( ) ;
conn = db . getConnection ( ) ;
try {
try {
Statement sttmt = conn . createStatement ( ) ;
Statement sttmt = conn . createStatement ( ) ;
ResultSet rs = sttmt . executeQuery ( sql ) ;
ResultSet rs = sttmt . executeQuery ( sql ) ;
while ( rs . next ( ) ) {
while ( rs . next ( ) ) {
Record record = new Record ( ) ;
Record record = new Record ( ) ;
record . setUser name( rs . getNString ( "username" ) ) ;
record . setUser _ name( rs . getNString ( "username" ) ) ;
record . setTime ( rs . getNString ( "time" ) ) ;
record . setTime ( rs . getNString ( "time" ) ) ;
record . setDonatemon ( rs . getFloat ( "donatemon" ) ) ;
record . setDonatemon ( rs . getFloat ( "donatemon" ) ) ;
recordlist . add ( record ) ;
recordlist . add ( record ) ;