📄 studentdao.java
字号:
package project.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import project.common.DbConnection;
import project.common.DbException;
import project.common.DbSql;
import project.vo.StudentVo;
public class StudentDao {
/**
* 查找数据库中是否存在某一编号的学生
* @param studentId
* @return
*/
public boolean findStudentById(int studentId){
Connection con = null;
PreparedStatement stmt = null;
boolean flag = false;
ResultSet set = null;
try{
con = new DbConnection().getConnection();
stmt = con.prepareStatement(DbSql.SELECT_STUDENT_BY_ID);
stmt.setInt(1,studentId);
set = stmt.executeQuery();
if(set.next()){
flag = true;
}
}catch(SQLException e){
System.out.println(e.getMessage());
}
return flag;
}
/**
* 将一条学生信息插入到数据库中
* @param value
* @return
*/
public boolean InsertStudent(StudentVo value) throws DbException{
boolean flag = false;
Connection con = null;
PreparedStatement stmt = null;
try{
if(findStudentById(value.getStudent_id())){
throw new DbException("数据库中存在相同条件的记录");
}
con = new DbConnection().getConnection();
stmt = con.prepareStatement(DbSql.INSERT_STUDENT);
stmt.setInt(1,value.getStudent_id());
stmt.setString(2,value.getStudent_name());
stmt.setString(3,value.getStudent_sex());
stmt.setString(4,value.getStudent_major());
stmt.setInt(5,value.getStudent_grade());
stmt.setString(6,value.getStudent_mail());
//stmt.setString(7,value.getStudent_address());
stmt.setString(7,value.getStudent_mobile());
int count = stmt.executeUpdate();
if(count != 0){
flag = true;
}
}catch(SQLException e){
System.out.println(e.getMessage());
}
return flag;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -