📄 studentmanager.java
字号:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class StudentManager {
public boolean isLogin(Student student) {
boolean flag = false;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = "select * from student where id=" + student.getId()
+ " and name='" + student.getName() + "'";
try {
conn = DBConnection.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.closeConnection(conn);
}
return flag;
}
//管理员登陆
public boolean isLoginByAdmin(Admin admin) {
boolean flag = false;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = "select * from admin where name='" + admin.getName()
+ "' and password='" + admin.getPassword() + "'";
try {
conn = DBConnection.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.closeConnection(conn);
}
return flag;
}
//模糊查询
public List queryByLike(String cond) {
List list = new ArrayList();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = "select * from student where name like '%" + cond + "%'";
try {
conn = DBConnection.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setScore(rs.getInt("score"));
student.setJointime(rs.getDate("jointime"));
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.closeConnection(conn);
}
return list;
}
public boolean insertStudent(Student student) {
boolean flag = false;
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "insert into student(name,sex) values(?,?)";
try {
conn = DBConnection.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getSex());
int tmp = pstmt.executeUpdate();
if(tmp==0){
flag = false;
}else{
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.closeConnection(conn);
}
return flag;
}
public boolean insertAdmin(Admin admin) {
boolean flag = false;
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "insert into admin values(?,?,?)";
try {
conn = DBConnection.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, admin.getName());
pstmt.setString(2, admin.getPassword());
pstmt.setString(3, admin.getRight());
int tmp = pstmt.executeUpdate();
if(tmp==0){
flag = false;
}else{
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.closeConnection(conn);
}
return flag;
}
public boolean delStudent(int id) {
boolean flag = false;
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "delete from student where id=?";
try {
conn = DBConnection.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
int tmp = pstmt.executeUpdate();
if(tmp==0){
flag = false;
}else{
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.closeConnection(conn);
}
return flag;
}
public boolean delAdmin(String name) {
boolean flag = false;
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "delete from admin where name=?";
try {
conn = DBConnection.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.executeUpdate();
flag = true;
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.closeConnection(conn);
}
return flag;
}
public boolean updateStudent(Student student) {
boolean flag = false;
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "update student set name=?,sex =?,flag =? where id =?";
try {
conn = DBConnection.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getSex());
pstmt.setString(3, student.getFlag());
pstmt.setInt(4, student.getId());
pstmt.executeUpdate();
flag = true;
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.closeConnection(conn);
}
return flag;
}
public boolean updateStudentScore(Student student) {
boolean flag = false;
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "update student set score=? ,jointime = now() where id =?";
try {
conn = DBConnection.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, student.getScore());
pstmt.setInt(2, student.getId());
int tmp = pstmt.executeUpdate();
if(tmp<0){
flag = false;
}else{
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.closeConnection(conn);
}
return flag;
}
public boolean updateAdmin(Admin admin) {
boolean flag = false;
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "update admin set password = ? where name=?";
try {
conn = DBConnection.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, admin.getPassword());
pstmt.setString(2, admin.getName());
pstmt.executeUpdate();
flag = true;
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.closeConnection(conn);
}
return flag;
}
public List queryAdminByAll() {
List list = new ArrayList();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = "select * from admin";
try {
conn = DBConnection.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
Admin admin = new Admin();
admin.setId(rs.getInt("id"));
admin.setName(rs.getString("name"));
admin.setPassword(rs.getString("password"));
admin.setRight(rs.getString("right"));
list.add(admin);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.closeConnection(conn);
}
return list;
}
public Student queryStudent(int id) {
Student student = new Student();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = "select * from student where id="+id;
try {
conn = DBConnection.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
student.setId(id);
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setScore(rs.getInt("score"));
student.setJointime(rs.getDate("jointime"));
}else{
student = null;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.closeConnection(conn);
}
return student;
}
public List queryStudentByAll() {
List list = new ArrayList();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String sql = "select * from student";
try {
conn = DBConnection.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setScore(rs.getInt("score"));
student.setJointime(rs.getDate("jointime"));
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.closeConnection(conn);
}
return list;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -