📄 studentdaoimpl.java
字号:
package daoImpl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import util.JdbcUtil;
import dao.StudentDAO;
import entity.Sanswer;
import entity.Student;
public class StudentDAOImpl implements StudentDAO {
public Student selectStudentByStudent(String loginName, String loginPassword) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Student student = null;
String sql = "select * from t_student s where s.name=? and s.password=?";
try {
con = JdbcUtil.getConnection();
ps = con.prepareStatement(sql);
ps.setString(1, loginName);
ps.setString(2, loginPassword);
rs = ps.executeQuery();
if(rs.next()) {
student=new Student();
student.setId(rs.getLong(1));
student.setName(rs.getString(2));
student.setPassword(rs.getString(3));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtil.release(rs, ps, con);
}
return student;
}
public void insertSanswer(Long studentid, Long questionid, String sanswer) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "insert into t_student_question values(?,?,?,?)";
try {
con = JdbcUtil.getConnection();
ps = con.prepareStatement(sql);
ps.setInt(1, findAll()+1);
ps.setLong(2, studentid);
ps.setLong(3, questionid);
ps.setString(4, sanswer);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtil.release(rs, ps, con);
}
}
public int findAll() {
int n = 0;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = JdbcUtil.getConnection();
String sql = "select max(id) from t_student_question ";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
n = rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtil.release(rs, ps, con);
}
return n;
}
public Sanswer selectSanswer(Long studentid, Long questionid) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Sanswer sanswer = new Sanswer();
String sql = "select * from t_student_question sq where sq.studentid=? and sq.questionid=?";
try {
con = JdbcUtil.getConnection();
ps = con.prepareStatement(sql);
ps.setLong(1, studentid);
ps.setLong(2, questionid);
rs = ps.executeQuery();
if(rs.next()) {
sanswer.setQuestionid(rs.getInt(3));
sanswer.setSanswer(rs.getString(4));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtil.release(rs, ps, con);
}
return sanswer;
}
public static void main(String[] args) {
StudentDAOImpl sdi = new StudentDAOImpl();
// Student student = sdi.selectStudentByStudent("chenlin", "chenlin");
// System.out.println(student);
Boolean flag=sdi.selectStudentid(1l,6l);
System.out.println(flag);
// sdi.insertSanswer(2, 1, "abc");
// Sanswer sanswer=sdi.selectSanswer(1, 1);
// System.out.println(sanswer.getSanswer());
//
}
public Boolean selectStudentid(Long studentid,Long examid) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Student student = null;
String sql = "select * from t_anlysis a where a.studentid=? and a.examid=?";
try {
con = JdbcUtil.getConnection();
ps = con.prepareStatement(sql);
ps.setLong(1, studentid);
ps.setLong(2,examid);
rs = ps.executeQuery();
if(rs.next()==false)
return false;
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtil.release(rs, ps, con);
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -