package Server;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import Bean.Information;
import Bean.User;
import DB.ConnectDB;
public class LoginServer {
private static Connection conn = null;
private static PreparedStatement pst = null;
private static ResultSet rs = null;
/**
*
* @param user
* @return IfSuccess
*/
public int Login(User user) {
try {
String sql = "select UserId from login where UserName = '"
+ user.GetUserName() + "' and UserPwd = '"
+ user.GetUserPwd() + "'";
conn = ConnectDB.connOpen();
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
if (rs != null) {
rs.next();// 将游标的位置移到第一行
String Id = rs.getString("UserId");// 获取第一行 字段名为"UserId"的值
if (Id != null) {
return 1;
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
return 0;
}
/**
* @param sno
* @return IfSuccess
*/
public int Del(int sno) {
try {
String sql = "Update information set Statue = 0 where Sno = " + sno + "";
conn = ConnectDB.connOpen();
pst = conn.prepareStatement(sql);
int r = pst.executeUpdate();
if (r == 1) {
return 1;
}
else{
return 0;
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
return 0;
}
/**
* @param sno
* @param sname
* @param sgender
* @param sage
* @return IfSuccess
*/
public int modify(int sno,String sname,String sgender,int sage) {
try {
String sql = "Update information set Sno = "+sno+",Sname = '"+sname+"',Sgender = '"+sgender+"',Sage = "+sage+" where Sno = "+sno+" ";
conn = ConnectDB.connOpen();
pst = conn.prepareStatement(sql);
int r = pst.executeUpdate();
if (r == 1) {
return 1;
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
return 0;
}
/**
*
*
* @param info
* @return
*/
public int Add(Information info) {
try {
int sno = info.GetSno();
String sname = info.GetSname().toString();
String sgender = info.GetSgender().toString();
int sage = info.GetSage();
String sql = "insert into information values(?,?,?,?,?)";
conn = ConnectDB.connOpen();
pst = conn.prepareStatement(sql);
pst.setInt(1, sno);
pst.setString(2, sname);
pst.setString(3, sgender);
pst.setInt(4, sage);
pst.setInt(5, 1);
int r = pst.executeUpdate();
if (r == 1) {
return 1;
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
return 0;
}
/**
*
* @return 查询的结果集
*/
public static List<Information> GetAllinfo() {
List<Information> list = new ArrayList<Information>();
try {
String sql = "select Sno,Sname,Sgender,Sage from information where Statue = 1";
conn = ConnectDB.connOpen();
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();// rs是结果集
while (rs.next()) {
Information info = new Information();
info.SetSno(rs.getInt("Sno"));
info.SetSname(rs.getString("Sname"));
info.SetSgender(rs.getString("Sgender"));
info.SetSage(rs.getInt("Sage"));
list.add(info);
}
return list;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
return list;
}
}