JDBC访问数据库(面向对象的方式)
public class StudentInfoUtil {
private static String forNameDriver="com.mysql.jdbc.Driver";
private static String jdbcUrl="jdbc:mysql://localhost:3306/db_school?useUnicode=true&characterEncoding=utf-8";
private static String jdbcUserName="root";
private static String jdbcPassword="123456";
/**
* @方法名: getConn
* @方法说明: 创建链接
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月7日下午11:24:30
* @return
* @return: Connection
*/
public static Connection getConn(){
Connection conn = null;
try {
Class.forName(forNameDriver);
//创建连接
conn = DriverManager.getConnection(jdbcUrl, jdbcUserName, jdbcPassword);
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
/**
* @方法名: closed
* @方法说明: 释放资源
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月7日下午11:37:05
* @param ps
* @param conn
* @return: void
*/
public static void closed(PreparedStatement ps,Connection conn){
try{
if(ps!=null){
ps.close();
if(conn!=null){
conn.close();
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
public class StudentInfo {
private Integer stuId;
private String stuCode;
private String stuName;
private int stuAge;
private String stuSex;
public Integer getStuId() {
return stuId;
}
public void setStuId(Integer stuId) {
this.stuId = stuId;
}
public String getStuCode() {
return stuCode;
}
public void setStuCode(String stuCode) {
this.stuCode = stuCode;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public String getStuSex() {
return stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}
@Override
public String toString() {
return "StudentInfo [stuId=" + stuId + ", stuCode=" + stuCode + ", stuName=" + stuName + ", stuAge=" + stuAge
+ ", stuSex=" + stuSex + "]";
}
}
public class StudentInfoDao {
/**
* @方法名: findAll
* @方法说明: 查询学生信息
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月8日上午11:30:38
* @return
* @return: List<StudentInfo>
*/
public List<StudentInfo> findAll(){
Connection conn = StudentInfoUtil.getConn();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from t_studentInfo";
//创建集合
List<StudentInfo> list = new ArrayList<StudentInfo>();
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
StudentInfo studentInfo = new StudentInfo();
//获取结果集中的数据
studentInfo.setStuCode(rs.getString("stu_code"));
studentInfo.setStuName(rs.getString("stu_name"));
studentInfo.setStuAge(rs.getInt("stu_age"));
studentInfo.setStuSex(rs.getString("stu_sex"));
/* //创建集合
List<StudentInfo> list = new ArrayList<StudentInfo>();*/
//将数据存入集合
list.add(studentInfo);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
StudentInfoUtil.closed(ps, conn);
}
return list;
}
/**
* @方法名: addStudentInfo
* @方法说明: 新增学生信息
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月8日上午11:48:57
* @param studentInfo
* @return
* @return: int
*/
public int addStudentInfo(StudentInfo studentInfo){
Connection conn = StudentInfoUtil.getConn();
PreparedStatement ps = null;
String sql = "insert into t_studentInfo(stu_code,stu_name,stu_age,stu_sex) values(?,?,?,?)";
int result = 0;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, studentInfo.getStuCode());
ps.setString(2, studentInfo.getStuName());
ps.setInt(3, studentInfo.getStuAge());
ps.setString(4, studentInfo.getStuSex());
result = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
StudentInfoUtil.closed(ps, conn);
}
return result;
}
/**
* @方法名: deleteStudentInfo
* @方法说明: 根据ID删除学生信息
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月8日上午11:58:45
* @param stuId
* @return
* @return: int
*/
public int deleteStudentInfo(int stuId){
Connection conn = StudentInfoUtil.getConn();
PreparedStatement ps = null;
String sql = "delete from t_studentInfo where stu_id = ?";
int result = 0;
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, stuId);
result = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
StudentInfoUtil.closed(ps, conn);
}
return result;
}
/**
* @方法名: updateStudentInfo
* @方法说明: 修改学生信息
* @作者: LiYuHui
* @邮箱:1327711913@qq.com
* @日期: 2020年6月8日下午12:08:01
* @param studentInfo
* @return
* @return: int
*/
public int updateStudentInfo(StudentInfo studentInfo){
Connection conn = StudentInfoUtil.getConn();
PreparedStatement ps = null;
String sql = "update t_studentInfo set stu_name=?,stu_sex=? where stu_id=?";
int result = 0;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, studentInfo.getStuName());
ps.setString(2, studentInfo.getStuSex());
ps.setInt(3, studentInfo.getStuId());
result = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
StudentInfoUtil.closed(ps, conn);
}
return result;
}
}
public class Test {
public static void main(String[] args) {
//查询
StudentInfoDao studentInfoDao = new StudentInfoDao();
List<StudentInfo> list = studentInfoDao.findAll();
Iterator<StudentInfo> it = list.iterator();
while(it.hasNext()){
StudentInfo stu = it.next();
System.out.println(stu);
}
/*//新增
StudentInfoDao studentInfoDao = new StudentInfoDao();
StudentInfo studentInfo = new StudentInfo();
studentInfo.setStuCode("10010");
studentInfo.setStuName("小丽");
studentInfo.setStuAge(18);
studentInfo.setStuSex("女");
studentInfoDao.addStudentInfo(studentInfo);*/
/*//删除
StudentInfoDao studentInfoDao = new StudentInfoDao();
studentInfoDao.deleteStudentInfo(11);*/
/*//修改
StudentInfoDao studentInfoDao = new StudentInfoDao();
StudentInfo studentInfo = new StudentInfo();
studentInfo.setStuName("春丽");
studentInfo.setStuSex("女");
studentInfo.setStuId(8);
studentInfoDao.updateStudentInfo(studentInfo);*/
}
}
当然,实现的前提是数据库和数据库驱动都要弄好