📄 personservice.java
字号:
package edu.cust.service;
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 edu.cust.beans.Person;
import edu.cust.util.My_DB;
public class PersonService {
@SuppressWarnings("finally")
public static boolean savePerson(Person person) {
boolean flag = false;
String sql = "insert Person(username,password,age,sex,school,major) values(?,?,?,?,?,?)";
Connection con = null;
PreparedStatement pstmt = null;
My_DB db = new My_DB();
try {
con = db.getCon();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, person.getUsername());
pstmt.setString(2, person.getPassword());
pstmt.setInt(3, person.getAge());
pstmt.setBoolean(4, person.isSex());
pstmt.setString(5, person.getSchool());
pstmt.setString(6, person.getMajor());
pstmt.executeUpdate();
flag = true;
} catch (Exception e) {
flag = false;
e.printStackTrace();
} finally {
try {
pstmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
}
@SuppressWarnings("finally")
public static Person getPersonById(Long id) {
Person person = new Person();
String sql = "select * from Person where id=" + id;
Connection con = null;
Statement stmt = null;
ResultSet rets = null;
My_DB db = new My_DB();
try {
con = db.getCon();
stmt = con.createStatement();
rets = stmt.executeQuery(sql);
while (rets.next()) {
person.setId(rets.getLong("id"));
person.setUsername(rets.getString("username"));
person.setPassword(rets.getString("password"));
person.setAge(rets.getInt("age"));
person.setSex(rets.getBoolean("sex"));
person.setSchool(rets.getString("school"));
person.setMajor(rets.getString("major"));
}
} catch (Exception e) {
person = null;
e.printStackTrace();
} finally {
try {
rets.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
return person;
}
}
@SuppressWarnings("finally")
public static boolean updatePerson(Person person) {
boolean flag = false;
Person oldPerson = getPersonById(person.getId());
String sql = "update Person set username=?,password=?,age=?,sex=?,school=?,major=? where id="
+ person.getId();
Connection con = null;
PreparedStatement pstmt = null;
My_DB db = new My_DB();
try {
con = db.getCon();
if (person.getUsername() != null) {
oldPerson.setUsername(person.getUsername());
}
if (person.getPassword() != null) {
oldPerson.setPassword(person.getPassword());
}
if (new Integer(person.getAge()) != null) {
oldPerson.setAge(person.getAge());
}
if (new Boolean(person.isSex()) != null) {
oldPerson.setSex(person.isSex());
}
if (person.getSchool() != null) {
oldPerson.setSchool(person.getSchool());
}
if (person.getMajor() != null) {
oldPerson.setMajor(person.getMajor());
}
pstmt = con.prepareStatement(sql);
pstmt.setString(1, oldPerson.getUsername());
pstmt.setString(2, oldPerson.getPassword());
pstmt.setInt(3, oldPerson.getAge());
pstmt.setBoolean(4, oldPerson.isSex());
pstmt.setString(5, oldPerson.getSchool());
pstmt.setString(6, oldPerson.getMajor());
pstmt.executeUpdate();
flag = true;
} catch (Exception e) {
flag = false;
e.printStackTrace();
} finally {
try {
pstmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
}
@SuppressWarnings("finally")
public static boolean deletePerson(Long id) {
boolean flag = false;
String sql = "delete Person where id=" + id;
Connection con = null;
Statement stmt = null;
My_DB db = new My_DB();
try {
con = db.getCon();
stmt = con.createStatement();
stmt.executeUpdate(sql);
flag = true;
} catch (Exception e) {
flag = false;
e.printStackTrace();
} finally {
try {
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
}
@SuppressWarnings("finally")
public static ArrayList searchPerson() {
ArrayList list = new ArrayList();
String sql = "select * from Person";
Connection con = null;
Statement stmt = null;
ResultSet rets = null;
My_DB db = new My_DB();
try {
con = db.getCon();
stmt = con.createStatement();
rets = stmt.executeQuery(sql);
while (rets.next()) {
Person temp = new Person();
temp.setId(rets.getLong("id"));
temp.setUsername(rets.getString("username"));
temp.setPassword(rets.getString("password"));
temp.setAge(rets.getInt("age"));
temp.setSex(rets.getBoolean("sex"));
temp.setSchool(rets.getString("school"));
temp.setMajor(rets.getString("major"));
list.add(temp);
}
} catch (Exception e) {
list = null;
e.printStackTrace();
} finally {
try {
rets.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -