获取数据库连接类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.cj.xdevapi.PreparableStatement;
import com.mysql.cj.xdevapi.Result;
/**
* @author 12032
*
*/
public class DBUtils {
/** 获取数据库位置*/
private static final String SQL_CONN="jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC";
/**获取数据库用户*/
private static final String USER="work";
/**获取数据库用户密码*/
private static final String PWD="work";
/**
* 数据库连接
* @return conn数据库连接
*/
public static Connection getconn() {
Connection conn =null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");//查询库中是否拥有此资源包
conn =DriverManager.getConnection(SQL_CONN,USER,PWD); //获取连接
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;//放回连接
}
/**
* 关闭数据库流
* @param conn
* @param pstmt
* @param rset
*/
public static void releaseRec(Connection conn,PreparedStatement pstmt,ResultSet rset) {
try {
if(rset!=null) rset.close();
if(pstmt!=null) pstmt.close();
if(conn!=null)conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Dao接口
import java.util.List;
import com.ahx.damain.Student;
public interface StudentDao {
void addStudent(Student stu);
List<Student> loadAll();
void delStudent (int stuNo);
Student getStudentByNo(int stuNo);
void updateStudent(Student stu);
}
Dao实现类
/**
*
*/
package com.ahx.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.ahx.damain.Student;
import com.ahx.utils.DBUtils;
/**
* @author 12032
*
*/
public class StudentDaoJDBCImpl implements StudentDao {
/**获取数据库添加语句*/
private static final String SQL_ADD="insert tbl_student(stu_name,stu_height) values(?,?)";
private static final String LOAD_ALL="select * from tbl_student";
private static final String SQL_DEL="delete from tbl_student where stu_no=?";
private static final String SQL_BYNO="select * from tbl_student where stu_no=?";
private static final String SQL_UPDATE="update tbl_student set stu_name=?,stu_height=? where stu_no=?";
/**向数据库存入数据*/
@Override
public void addStudent(Student stu) {
Connection conn =DBUtils.getconn();//获取数据库连接
PreparedStatement pstmt=null;//处理数据
try {
pstmt=conn.prepareStatement(SQL_ADD);
pstmt.setString(1, stu.getStuName());//添加第一个问号所对应的值
pstmt.setDouble(2, stu.getStuHeight());//添加第二个问号所对应的值
pstmt.executeUpdate();//保存至数据库
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtils.releaseRec(conn, pstmt, null);//关闭数据库流
}
}
/**读取数据库中的数据*/
@Override
public List<Student> loadAll() {
Connection conn = DBUtils.getconn();//获取数据库连接
PreparedStatement pstmt = null;
ResultSet rest =null; //接受数据库的记录
List<Student> stuList = new ArrayList<>();
try {
pstmt=conn.prepareStatement(LOAD_ALL);
rest=pstmt.executeQuery(); //接受返回的数据
while (rest.next()) { //rest.next() 检测指向的位置是否拥有记录
Student stu = new Student();
stu.setStuNo(rest.getInt("stu_no"));//取出数据库中对应的数据存放入 对象中
stu.setStuName(rest.getString("stu_name"));
stu.setStuHeight(rest.getDouble("stu_height"));
stuList.add(stu);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtils.releaseRec(conn, pstmt, rest);
}
return stuList;
}
@Override
public void delStudent(int stuNo) {
// TODO Auto-generated method stub
Connection conn = DBUtils.getconn(); //获取数据库连接
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(SQL_DEL);
pstmt.setInt(1, stuNo);
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtils.releaseRec(conn, pstmt, null);
}
}
@Override
public Student getStudentByNo(int stuNo) {
Connection conn= DBUtils.getconn();
PreparedStatement pstmt = null;
ResultSet rset =null;
Student stu = null;
try {
pstmt =conn.prepareStatement(SQL_BYNO);
pstmt.setInt(1, stuNo);
rset= pstmt.executeQuery();
if(rset.next()) {
stu =new Student();
stu.setStuNo(rset.getInt("stu_no"));
stu.setStuName(rset.getString("stu_name"));
stu.setStuHeight(rset.getDouble("stu_height"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtils.releaseRec(conn, pstmt, rset);
}
return stu;
}
@Override
public void updateStudent(Student stu) {
Connection conn = DBUtils.getconn();
PreparedStatement pstmt=null;
try {
pstmt =conn.prepareStatement(SQL_UPDATE);
pstmt.setString(1, stu.getStuName());
pstmt.setDouble(2, stu.getStuHeight());
pstmt.setInt(3, stu.getStuNo());
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtils.releaseRec(conn, pstmt, null);
}
}
}