📄 studentdao.java
字号:
package train.basic.project.sm.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import train.basic.project.sm.model.Student;
import train.basic.project.sm.util.DBConnection;
public class StudentDAO {
protected DBConnection conn = new DBConnection();
public List<Student> queryAll() {
List<Student> list = new ArrayList();
ResultSet rs = conn.executeQuery("select * from student");
try {
while (rs.next()) {
list.add(new Student(rs.getInt("id"), rs.getString("name"), rs
.getString("phone"), rs.getString("mobile"), rs
.getString("address"), rs.getString("cert")));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn.close();
}
return list;
}
public int saveOrUpdate(Student s) {
int num = 0;
ResultSet rs = conn.executeQuery("select * from student where id="
+ s.getNo());
try {
if (rs.next()) {
num = conn.executeUpdate("update student set name='"
+ s.getName() + "', phone='" + s.getPhone()
+ "', mobile='" + s.getMobile() + "', address='"
+ s.getAddress() + "', cert='" + s.getCert()
+ "' where id='" + s.getNo() + "'");
} else {
num = conn
.executeUpdate("insert into student(id, name, phone, mobile, address, cert) values ('"
+ s.getNo()
+ "', '"
+ s.getName()
+ "', '"
+ s.getPhone()
+ "', '"
+ s.getMobile()
+ "', '"
+ s.getAddress()
+ "', '"
+ s.getCert() + "')");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn.close();
}
return num;
}
public int save(Student s) {
String sql = "insert into student(name, phone, mobile, address, cert) values ('"
+ s.getName()
+ "', '"
+ s.getPhone()
+ "', '"
+ s.getMobile()
+ "', '" + s.getAddress() + "', '" + s.getCert() + "')";
int num = conn.insertAndGetPKKey(sql);
conn.close();
return num;
}
public Student get(int no) {
Student s = null;
ResultSet rs = conn
.executeQuery("select * from student where id=" + no);
try {
while (rs.next()) {
s = new Student(rs.getInt("id"), rs.getString("name"), rs
.getString("phone"), rs.getString("mobile"), rs
.getString("address"), rs.getString("cert"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn.close();
}
return s;
}
public void delete(int no) {
conn.executeUpdate("delete from student where id=" + no);
conn.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -