JDBC系列流程代码

本文详细介绍了使用Java JDBC进行数据库操作的全过程,包括数据库工具类的创建,以及查询、增加、删除和修改等基本操作。
摘要由CSDN通过智能技术生成

数据库工具类

public class DBUtil {
    //在mysql8.0版本以上需要在后面加上服务器时区
    public static final String URL = "jdbc:mysql://localhost:3306/数据库名?serverTimezone=UTC";
    public static final String USER_NAME = "用户名";
    public static final String PASSWORD = "密码";

    public static Connection connectionHelper() {
        Connection conn = null;
        try {
            //加载数据库驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("驱动异常:" + e.getMessage());
        }
        try {
            //获取数据库连接
            conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
            return conn;
        } catch (Exception e) {
            System.out.println("获取连接异常:" + e.getMessage());
            return null;
        }
    }
}

查询

public List<Student> queryAll() {
            Connection conn = null;
            String sql = "select * from student";
            //使用PreparedStatement对象可以防止sql注入
            PreparedStatement psmt = null;
            //查询数据返回结果集
            ResultSet rs =null;
            //获取工具类中的连接
            conn = DBUtil.connectionHelper();
            try{
                //执行SQL语句
                psmt = conn.prepareStatement(sql);
                //查询用executeQuery方法,增删改用executeUpdate方法
                rs = psmt.executeQuery();
                //创建List集合存放学生
                List<Student> stuList = new ArrayList<Student>();
                while(rs.next()){
                    //每读取一行数据就创建一个学生并赋值
                    Student student = new Student();
                    student.setStuno(rs.getString("stuId"));
                    student.setStuname(rs.getString("stuName"));
                    student.setStuage(Integer.parseInt(rs.getString("stuAge")));
                    student.setStusex(rs.getString("stuSex"));
                    //把学生添加到集合中
                    stuList.add(student);
                }
                //返回学生集合
                return stuList;
            }
            //捕获异常
            catch (Exception e ){
                System.out.println("异常"+e.getMessage());
                return null;
            }
            //必须释放资源
            finally {
                try{
                    if(conn!=null) conn.close();
                    if(psmt!=null) psmt.close();
                    if(rs!=null) rs.close();
                }
                catch(Exception e){
                    System.out.println("异常"+e.getMessage());
                }
            }
     }

增加

public boolean add(Student student) {
        Connection conn = null;
        PreparedStatement statement = null;
        conn = DBUtil.connectionHelper();
        try{
            String sql = "insert into student values(?,?,?,?)";
            statement = conn.prepareStatement(sql);
            //设置SQL语句中的?参数
            //从第一个问号开始对应
            statement.setString(1,student.getStuId());
            statement.setString(2,student.getStuName());
            statement.setInt(3,student.getStuAge());
            statement.setInt(4,student.getStuSex());
            //查询用executeQuery方法,增删改用executeUpdate方法
            statement.executeUpdate();
            //返回执行成功
            //如果没有执行到这则跳到异常中返回失败
            return true;
        }
        catch(Exception e){
            System.out.println("异常:"+e.getMessage());
            return false;
        }
        finally {
            try {
                if (conn!=null) conn.close();
                if (statement!=null) statement.close();
            }
            catch (Exception e){
                System.out.println("异常:"+e.getMessage());
            }
        }
    }

删除

public boolean deleteNo(int id) {
        Connection conn = null;
        PreparedStatement statement = null;
        conn = DBUtil.connectionHelper();
        try{
            String sql = "delete  from student where stuId = ?";
            statement = conn.prepareStatement(sql);
            //删除传入的id的学生
            statement.setInt(1,id);
            //查询用executeQuery方法,增删改用executeUpdate方法
            statement.executeUpdate();
            //返回执行成功
            //如果没有执行到这则跳到异常中返回失败
            return true;
        }
        catch(Exception e){
            System.out.println("异常:"+e.getMessage());
            return false;
        }
        finally {
            try {
                if (conn!=null) conn.close();
                if (statement!=null) statement.close();
            }
            catch (Exception e){
                System.out.println("异常:"+e.getMessage());
            }
        }
    }

修改

public boolean updateNo(Student student) {
        Connection conn = null;
        PreparedStatement statement = null;
        conn = DBUtil.connectionHelper();
        try{
            String sql = "update student set stuName=?,stuAge=?,stuSex=? where stuId = ?";
            statement = conn.prepareStatement(sql);
            //给SQL语句赋值
            statement.setString(1,student.getStuName());
            statement.setInt(2,student.getStuAge());
            statement.setString(3,student.getStuSex());
            statement.setInt(4,student.getStuId());
            //查询用executeQuery方法,增删改用executeUpdate方法
            statement.executeUpdate();
            //返回执行成功
            //如果没有执行到这则跳到异常中返回失败
            return true;
        }
        catch(Exception e){
            System.out.println("异常:"+e.getMessage());
            return false;
        }
        finally {
            try {
                if (conn!=null) conn.close();
                if (statement!=null) statement.close();
            }
            catch (Exception e){
                System.out.println("异常:"+e.getMessage());
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胖达不胖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值